Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve thread bindings when running fetchers #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/urania/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,25 @@
[{:keys [executor env]} muse]
(prom/create
(fn [resolve reject]
(-execute executor #(-> (try (-fetch muse env)
;; fast fail if datasource throws accidentally
(catch #?(:clj Exception :cljs :default) e
(prom/rejected e)))
(prom/then resolve)
(prom/catch reject))))))
(-execute executor (bound-fn []
(-> (try (-fetch muse env)
;; fast fail if datasource throws accidentally
(catch #?(:clj Exception :cljs :default) e
(prom/rejected e)))
(prom/then resolve)
(prom/catch reject)))))))

(defn- run-fetch-multi
[{:keys [executor env]} muse muses]
(prom/create
(fn [resolve reject]
(-execute executor #(-> (try (-fetch-multi muse muses env)
;; fast fail if datasource throws accidentally
(catch #?(:clj Exception :cljs :default) e
(prom/rejected e)))
(prom/then resolve)
(prom/catch reject))))))
(-execute executor (bound-fn []
(-> (try (-fetch-multi muse muses env)
;; fast fail if datasource throws accidentally
(catch #?(:clj Exception :cljs :default) e
(prom/rejected e)))
(prom/then resolve)
(prom/catch reject)))))))

(defn- fetch-many-caching
[opts sources]
Expand Down
14 changes: 14 additions & 0 deletions test/urania/core_spec_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
(-identity [_] seed)
(-fetch [_ _] (throw (ex-info "Uncaught" {:seed seed}))))

(def ^:dynamic dynamic-value 1)

(deftype Dynamic []
u/DataSource
(-identity [_] dynamic-value)
(-fetch [_ _] (prom/resolved dynamic-value)))

(defn- mk-pair [seed] (Pair. seed))

(defn- sum-pair [[a b]] (+ a b))
Expand Down Expand Up @@ -415,3 +422,10 @@
(u/collect [(Environment. 42) (Environment. 99)])
identity
{:env :the-environment})))

#?(:clj
(deftest thread-bindings-test
(assert-ast [1] (u/collect [(Dynamic.)]))

(binding [dynamic-value 2]
(assert-ast [2] (u/collect [(Dynamic.)])))))