Skip to content

Commit

Permalink
Bump promesa (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Aug 16, 2023
1 parent 6434279 commit 1728308
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 59 deletions.
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
:git/sha "987910fb38fdd166865458c3fd4b468a22fb9992"}
org.clojure/clojurescript {:mvn/version "1.11.51"}
applied-science/js-interop {:mvn/version "0.3.3"}
funcool/promesa {:mvn/version "9.0.494"}
funcool/promesa {:git/url "https://github.com/funcool/promesa"
:git/sha "765e51fefdd23d775e6233edd838ffc1c116419d"}
reagent/reagent {:mvn/version "1.1.0"}
re-frame/re-frame {:mvn/version "1.3.0"}}}
:cljs-test-runner
Expand Down
124 changes: 70 additions & 54 deletions src/sci/configs/funcool/promesa.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(:require [clojure.core :as c]
[promesa.core :as p]
[promesa.exec :as exec]
[promesa.impl :as impl]
[promesa.protocols :as pt]
[sci.core :as sci]))

Expand All @@ -18,28 +19,33 @@
to the last expression. Always awaiting the result of each
expression."
[_ _ & exprs]
`(pt/-bind
(pt/-promise nil)
(fn [_#]
~(condp = (count exprs)
0 `(pt/-promise nil)
1 `(pt/-promise ~(first exprs))
(reduce (fn [acc e]
`(pt/-bind (pt/-promise ~e) (fn [_#] ~acc)))
`(pt/-promise ~(last exprs))
(reverse (butlast exprs)))))))
(condp = (count exprs)
0 `(impl/resolved nil)
1 `(pt/-promise ~(first exprs))
(reduce (fn [acc e]
`(pt/-mcat (pt/-promise ~e) (fn [_#] ~acc)))
`(pt/-promise ~(last exprs))
(reverse (butlast exprs)))))

(defn ^:macro let*
"An exception unsafe let-like macro. Supposes that we are already
wrapped in promise context so avoids defensive wrapping."
[_ _ bindings & body]
(assert (even? (count bindings)) (str "Uneven binding vector: " bindings))
(c/->> (reverse (partition 2 bindings))
(reduce (fn [acc [l r]]
`(pt/-mcat (pt/-promise ~r) (fn [~l] ~acc)))
`(promesa.core/do ~@body))))

(defn ^:macro let
"A `let` alternative that always returns promise and waits for all the
promises on the bindings."
[_ _ bindings & body]
`(pt/-bind
(pt/-promise nil)
(fn [_#]
~(c/->> (reverse (partition 2 bindings))
(reduce (fn [acc [l r]]
`(pt/-bind (pt/-promise ~r) (fn [~l] ~acc)))
`(promesa.core/do! ~@body))))))
(if (seq bindings)
`(pt/-mcat
(pt/-promise nil)
(fn [_#] (promesa.core/let* ~bindings ~@body)))
`(promesa.core/do ~@body)))

(defn ^:macro ->
"Like the clojure.core/->, but it will handle promises in values
Expand Down Expand Up @@ -161,47 +167,57 @@

(def promesa-namespace
{'*loop-run-fn* loop-run-fn
'-> (sci/copy-var -> pns)
'->> (sci/copy-var ->> pns)
'all (sci/copy-var p/all pns)
'any (sci/copy-var p/any pns)
'catch (sci/copy-var p/catch pns)
'chain (sci/copy-var p/chain pns)
'create (sci/copy-var p/create pns)
'deferred (sci/copy-var p/deferred pns)
'delay (sci/copy-var p/delay pns)
'do (sci/copy-var do! pns)
'do! (sci/copy-var do! pns)
'error (sci/copy-var p/error pns)
'finally (sci/copy-var p/finally pns)
'future (sci/copy-var future pns)
'thread-call (sci/copy-var p/thread-call pns)
'handle (sci/copy-var p/handle pns)
'let (sci/copy-var let pns)
'loop (sci/copy-var loop pns)
'map (sci/copy-var p/map pns)
'mapcat (sci/copy-var p/mapcat pns)
'promise (sci/copy-var p/promise pns)
'promise? (sci/copy-var p/promise? pns)
'race (sci/copy-var p/race pns)
'recur (sci/copy-var recur pns)
'reject! (sci/copy-var p/reject! pns)
'rejected (sci/copy-var p/rejected pns)
'resolve! (sci/copy-var p/resolve! pns)
'resolved (sci/copy-var p/resolved pns)
'run! (sci/copy-var p/run! pns)
'then (sci/copy-var p/then pns)
'thenable? (sci/copy-var p/thenable? pns)
'timeout (sci/copy-var p/timeout pns)
'with-redefs (sci/copy-var with-redefs pns)
'wrap (sci/copy-var p/wrap pns)
'doseq (sci/copy-var doseq pns)})
'-> (sci/copy-var -> pns)
'->> (sci/copy-var ->> pns)
'all (sci/copy-var p/all pns)
'any (sci/copy-var p/any pns)
'catch (sci/copy-var p/catch pns)
'chain (sci/copy-var p/chain pns)
'create (sci/copy-var p/create pns)
'deferred (sci/copy-var p/deferred pns)
'delay (sci/copy-var p/delay pns)
'do (sci/copy-var do! pns)
'do* (sci/copy-var do! pns)
'do! (sci/copy-var do! pns)
'error (sci/copy-var p/error pns)
'finally (sci/copy-var p/finally pns)
'future (sci/copy-var future pns)
'thread-call (sci/copy-var p/thread-call pns)
'handle (sci/copy-var p/handle pns)
'let (sci/copy-var let pns)
'let* (sci/copy-var let* pns)
'loop (sci/copy-var loop pns)
'map (sci/copy-var p/map pns)
'mapcat (sci/copy-var p/mapcat pns)
'promise (sci/copy-var p/promise pns)
'promise? (sci/copy-var p/promise? pns)
'race (sci/copy-var p/race pns)
'recur (sci/copy-var recur pns)
'reject! (sci/copy-var p/reject! pns)
'rejected (sci/copy-var p/rejected pns)
'resolve! (sci/copy-var p/resolve! pns)
'resolved (sci/copy-var p/resolved pns)
'run! (sci/copy-var p/run! pns)
'then (sci/copy-var p/then pns)
'thenable? (sci/copy-var p/thenable? pns)
'timeout (sci/copy-var p/timeout pns)
'with-redefs (sci/copy-var with-redefs pns)
'wrap (sci/copy-var p/wrap pns)
'doseq (sci/copy-var doseq pns)})

(def pims (sci/create-ns 'promesa.impl nil))

(def promesa-protocols-namespace
{'-bind (sci/copy-var pt/-bind ptns)
{'-mcat (sci/copy-var pt/-mcat ptns)
'-promise (sci/copy-var pt/-promise ptns)})

(def promesa-impl-namespace
{'resolved (sci/copy-var impl/resolved ptns)})

(def namespaces {'promesa.core promesa-namespace
'promesa.protocols promesa-protocols-namespace})
;; exposed for macros that expand to it
'promesa.protocols promesa-protocols-namespace
;; exposed for macros that expand to it
'promesa.impl promesa-impl-namespace})

(def config {:namespaces namespaces})
14 changes: 10 additions & 4 deletions test/funcool/promesa_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require
[cljs.test :refer [deftest is async]]
[sci.configs.funcool.promesa :as promesa-config]
[sci.core :as sci]))
[sci.core :as sci]
[promesa.core :as p]))

(defn ctx-fn [] (sci/init {:namespaces promesa-config/namespaces}))

Expand All @@ -13,8 +14,13 @@
(:require
[promesa.core :as p]))
(p/future (inc 2))")]
(p/do
1 2 3
(p/let [x (p/resolved (inc 2))]
(inc x)))")]
(async done
(-> (.then p (fn [v] (is (= 3 v))))
(-> p
(.then (fn [v]
(is (= 4 v))))
(.catch (fn [_] (is false)))
(.finally (done))))))
(p/finally done)))))

0 comments on commit 1728308

Please sign in to comment.