leonoel / cloroutine

Coroutine support for clojure
Eclipse Public License 2.0
228 stars 10 forks source link

Use of undeclared Var cloroutine.impl/clojure #5

Closed Wonko7 closed 5 years ago

Wonko7 commented 5 years ago

Hi!

In a cljs project with shadow-cljs:

------ WARNING #1 - :undeclared-var --------------------------------------------
 Resource: cloroutine/impl.cljc:291:66
 Use of undeclared Var cloroutine.impl/clojure
--------------------------------------------------------------------------------

With: utils.asyncawait.clj:

(ns utils.asyncawait
  (:refer-clojure :exclude [await])
  (:require [cloroutine.core :refer [cr]]))

(defmacro async [& body]
  `(js/Promise. (fn [s# f#]
                  (spawn (cr {await thunk}
                           (try (s# (do ~@body))
                            (catch :default e# (f# e#))))))))

utils.asyncawait.cljs:

(ns utils.asyncawait
  (:require [cloroutine.impl]))

(def ^:dynamic *success*)
(def ^:dynamic *failure*)
(def ^:dynamic *status*)
(def ^:dynamic *result*)

(defn await [p]
  (.then p *success* *failure*))

(defn thunk []
  (if *status* *result* (throw *result*)))

(defn spawn [c]
  (letfn [(run []
            (binding [*success* success
                      *failure* failure]
              (c)))
          (success [x]
            (binding [*status* true
                      *result* x] (run)))
          (failure [x]
            (binding [*status* false
                      *result* x] (run)))]
   (run)))

I'm using [cloroutine "2"].

This works, I'd just like to get rid of the warning, maybe I'm not requiring a needed namespace?

leonoel commented 5 years ago

You should not require cloroutine.impl directly, require cloroutine.core instead. I don't think it's related to this warning, however.

This can certainly be fixed but I'm failing to reproduce it, could you post more information about your compilation process ?

Wonko7 commented 5 years ago

https://gist.github.com/Wonko7/361aefb37f31172f31e0c65278d76612 I use npm i to set it up, and shadow-cljs watch main to compile it, let me know if I forgot something!

leonoel commented 5 years ago

Thanks. My shadow-cljs setup was outdated, I'm getting the warning now.

Wonko7 commented 5 years ago

Is (async (loop ... expected to work?

leonoel commented 5 years ago

Yes, see the test namespace. If you encounter problems with loops, please open another issue.

leonoel commented 5 years ago

Fixed in version 4.