Skip to content

Commit

Permalink
Bypass default lifecycle behavior when subscribing to a flow
Browse files Browse the repository at this point in the history
Now flows construct a cursor when they register. Calling `sub` simply
looks up this cursor, rather than building reactions at call time.
  • Loading branch information
kimo-k committed Nov 7, 2023
1 parent 36aeaea commit f6f62b6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/re_frame/flow/alpha.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
(ns re-frame.flow.alpha
(:require
[re-frame.db :as db]
[re-frame.utils :as u]
[re-frame.registrar :refer [get-handler]]
[re-frame.loggers :refer [console]]
[re-frame.interceptor :refer [->interceptor get-effect get-coeffect update-effect]]))
[re-frame.interceptor :refer [->interceptor get-effect get-coeffect update-effect]]
[reagent.core :as r]))

(def db-path? vector?)

Expand Down Expand Up @@ -68,7 +70,8 @@
([k m] (reg-flow (assoc m :id k)))
([m] (swap! flows assoc
(id m) (with-meta (->flow (merge (default (id m)) m))
{::new true}))))
{::new true
::ref (r/cursor db/app-db (:path m))}))))

(defn clear-flow
([]
Expand Down
3 changes: 3 additions & 0 deletions src/re_frame/query/alpha.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
(or (lifecycle (meta v))
:default)))

(def flow-lifecycle (comp #{:flow} ::rf/q))

(def lifecycle (some-fn legacy-lifecycle
flow-lifecycle
::rf/lifecycle
(constantly :default)))

Expand Down
25 changes: 18 additions & 7 deletions src/re_frame/subs/alpha.cljc
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
(ns re-frame.subs.alpha
(:require [re-frame.subs :refer [deref-input-signals sugar warn-when-not-reactive]]
[re-frame.registrar :refer [register-handler]]
[re-frame.register.alpha :refer [reg lifecycle->method]]
[re-frame.interop :refer [add-on-dispose! make-reaction reactive? reagent-id]]
[re-frame.query.alpha :as q]
[re-frame :as-alias rf]
[re-frame.trace :as trace :include-macros true]))
(:require
[reagent.core :as r]
[re-frame.subs :refer [deref-input-signals sugar warn-when-not-reactive]]
[re-frame.registrar :refer [register-handler]]
[re-frame.register.alpha :refer [reg lifecycle->method]]
[re-frame.interop :refer [add-on-dispose! make-reaction reactive? reagent-id]]
[re-frame.query.alpha :as q]
[re-frame :as-alias rf]
[re-frame.trace :as trace :include-macros true]
[re-frame.flow.alpha :as flow]))

(defmethod reg :sub-lifecycle [_ k f]
(swap! lifecycle->method assoc
Expand Down Expand Up @@ -98,3 +101,11 @@
(q/cache! q (q/handle q))))

(reg :sub-lifecycle :forever sub-forever)

(def nil-ref (r/atom nil))

(defn sub-flow [q]
(or (some-> q :id flow/lookup meta :re-frame.flow.alpha/ref)
nil-ref))

(reg :sub-lifecycle :flow sub-flow)

0 comments on commit f6f62b6

Please sign in to comment.