From f6f62b60cf597de82dd255526f4de78858c7a918 Mon Sep 17 00:00:00 2001 From: Kimo Knowles Date: Tue, 7 Nov 2023 23:36:29 +0100 Subject: [PATCH] Bypass default lifecycle behavior when subscribing to a flow Now flows construct a cursor when they register. Calling `sub` simply looks up this cursor, rather than building reactions at call time. --- src/re_frame/flow/alpha.cljc | 7 +++++-- src/re_frame/query/alpha.cljc | 3 +++ src/re_frame/subs/alpha.cljc | 25 ++++++++++++++++++------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/re_frame/flow/alpha.cljc b/src/re_frame/flow/alpha.cljc index 0b2805fb4..6ef6d3408 100644 --- a/src/re_frame/flow/alpha.cljc +++ b/src/re_frame/flow/alpha.cljc @@ -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?) @@ -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 ([] diff --git a/src/re_frame/query/alpha.cljc b/src/re_frame/query/alpha.cljc index df7793890..e34a49e52 100644 --- a/src/re_frame/query/alpha.cljc +++ b/src/re_frame/query/alpha.cljc @@ -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))) diff --git a/src/re_frame/subs/alpha.cljc b/src/re_frame/subs/alpha.cljc index cec908b77..ccbd59213 100644 --- a/src/re_frame/subs/alpha.cljc +++ b/src/re_frame/subs/alpha.cljc @@ -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 @@ -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)