|
| 1 | +(ns re-frame.subs.alpha |
| 2 | + (:require [re-frame.utils :refer [first-in-vector common-key]] |
| 3 | + [re-frame.subs :refer [kind deref-input-signals]] |
| 4 | + [re-frame.registrar :refer [register-handler get-handler]] |
| 5 | + [re-frame.interop :refer [add-on-dispose! make-reaction reactive?]] |
| 6 | + [re-frame.db :refer [app-db]] |
| 7 | + [re-frame :as-alias rf])) |
| 8 | + |
| 9 | +(def strategy->method (atom {})) ;; should we use a clojure.core multimethod? |
| 10 | +(reset! strategy->method {}) |
| 11 | + |
| 12 | +(defn legacy-strategy [v] |
| 13 | + (when (vector? v) |
| 14 | + (let [value (first-in-vector v)] |
| 15 | + (if (map? value) |
| 16 | + (common-key value @strategy->method) |
| 17 | + (or (common-key (meta v) @strategy->method) |
| 18 | + ::rf/sub-default))))) |
| 19 | + |
| 20 | +(defn strategy [q] (or (legacy-strategy q) |
| 21 | + (common-key q @strategy->method))) |
| 22 | + |
| 23 | +(defn legacy-query-id [v] |
| 24 | + (when (vector? v) |
| 25 | + (let [value (first-in-vector v)] |
| 26 | + (if (map? value) |
| 27 | + (some-> value strategy value) |
| 28 | + value)))) |
| 29 | + |
| 30 | +(defn query-id [q] (or (legacy-query-id q) |
| 31 | + (some-> q strategy q))) |
| 32 | + |
| 33 | +(defn method [q] (->> q strategy (get @strategy->method))) |
| 34 | + |
| 35 | +(defn handle [q] ((get-handler kind (query-id q)) app-db q)) |
| 36 | + |
| 37 | +(def cache (atom {})) |
| 38 | +(defn cached [q] (get-in @cache [(strategy q) q])) |
| 39 | +(defn cache! [q r] (swap! cache assoc-in [(strategy q) q] r) r) |
| 40 | + |
| 41 | +(defn clear! |
| 42 | + ([] (reset! cache {})) |
| 43 | + ([q] (clear! q (strategy q))) |
| 44 | + ([q strat] (swap! cache update strat dissoc q))) |
| 45 | + |
| 46 | +(defmulti reg (fn [dv & _] dv)) |
| 47 | +(reset! (.-method-table reg) {}) |
| 48 | + |
| 49 | +(defmethod reg :sub-method [_ k f] |
| 50 | + (swap! strategy->method assoc k f)) |
| 51 | + |
| 52 | +(defmethod reg :sub [k id computation-fn] |
| 53 | + (register-handler |
| 54 | + kind |
| 55 | + id |
| 56 | + (fn [_ q] |
| 57 | + (make-reaction |
| 58 | + #(computation-fn |
| 59 | + (deref-input-signals app-db id) |
| 60 | + (if (vector? q) |
| 61 | + q |
| 62 | + (into [q] (::rf/legacy-args q)))))))) |
| 63 | + |
| 64 | +(defn sub [q] |
| 65 | + (let [md (method q)] |
| 66 | + (cond (map? q) (md q) |
| 67 | + (get q 2) (apply md q) ;; this discards the meta of q :( |
| 68 | + (vector? q) (md {(strategy q) (query-id q)})))) |
| 69 | + |
| 70 | +(defn sub-reactive |
| 71 | + ([m] |
| 72 | + (or (cached m) |
| 73 | + (let [md (strategy m) |
| 74 | + r (handle m)] |
| 75 | + (add-on-dispose! r #(clear! m md)) |
| 76 | + (cache! m r)))) |
| 77 | + ([id & args] |
| 78 | + (let [v (into [id] args)] |
| 79 | + (or (cached v) |
| 80 | + (let [md (strategy v) |
| 81 | + r (handle v)] |
| 82 | + (add-on-dispose! r #(clear! v md)) |
| 83 | + (cache! v r)))))) |
| 84 | + |
| 85 | +(reg :sub-method ::rf/sub-reactive sub-reactive) |
| 86 | + |
| 87 | +(defn sub-safe |
| 88 | + ([m] |
| 89 | + (if (reactive?) |
| 90 | + (sub-reactive m) |
| 91 | + (or (cached m) |
| 92 | + (handle m)))) |
| 93 | + ([id & args] |
| 94 | + (let [v (into [id] args)] |
| 95 | + (if (reactive?) |
| 96 | + (apply sub-reactive v) |
| 97 | + (or (cached v) |
| 98 | + (handle v)))))) |
| 99 | + |
| 100 | +(reg :sub-method ::rf/sub-safe sub-safe) |
| 101 | +(reg :sub-method ::rf/sub-default sub-safe) |
| 102 | + |
| 103 | +#_(do |
| 104 | + (def qid! (comp keyword gensym)) |
| 105 | + |
| 106 | + (defn report [_db query-v] |
| 107 | + {:query query-v |
| 108 | + :strategy (strategy query-v) |
| 109 | + :query-id (query-id query-v) |
| 110 | + :method (method query-v) |
| 111 | + :legacy-args (::rf/legacy-args query-v)}) |
| 112 | + |
| 113 | + (def test-queries |
| 114 | + (list |
| 115 | + {::rf/sub-safe (qid!)} |
| 116 | + {::rf/sub-reactive (qid!)} |
| 117 | + {::rf/sub-safe (qid!) |
| 118 | + ::rf/legacy-args [1 2 3]} |
| 119 | + [{::rf/sub-reactive (qid!)} 1 2 3] |
| 120 | + [(qid!)] |
| 121 | + [(qid!) 1 2 3] |
| 122 | + ^::rf/sub-reactive [(qid!)] |
| 123 | + ;; the computation-fn can't know the strategy in this case: |
| 124 | + ^::rf/sub-reactive [(qid!) 1 2 3])) |
| 125 | + |
| 126 | + (doseq [q test-queries |
| 127 | + :let [qid (query-id q) |
| 128 | + _ (reg :sub qid report) |
| 129 | + result @(sub q)]] |
| 130 | + (cljs.pprint/pprint result) |
| 131 | + (println) |
| 132 | + (assert result))) |
0 commit comments