|
31 | 31 | return-value) |
32 | 32 |
|
33 | 33 | (defn changed? [entities cached-entities track-count?] |
34 | | - #_(js/console.log ">>" |
35 | | - track-count? |
36 | | - #js {:entities (clj->js entities) |
37 | | - :cache (clj->js cached-entities)}) |
38 | | - (if (and track-count? (not= (count entities) (count cached-entities))) |
| 34 | + (cond |
| 35 | + (and track-count? |
| 36 | + (not= (count entities) (count cached-entities))) |
39 | 37 | (debug-msg true "cache:miss" "count of entities != cache" |
40 | 38 | #js {:entities (clj->js entities) |
41 | 39 | :cache (clj->js cached-entities)}) |
42 | | - (reduce (fn [_ e] |
43 | | - (when (let [cached-e (get cached-entities (get e "id"))] |
44 | | - (if (nil? cached-e) |
45 | | - (reduced (debug-msg true "cache:miss" "not in cache" |
46 | | - #js {:entity-id (get e "id") |
47 | | - :entities (clj->js entities) |
48 | | - :cache (clj->js cached-entities)})) |
49 | | - (reduce (fn [_ [ks old-v]] |
50 | | - (let [new-v (get-in e ks)] |
51 | | - (when (and (not= 0 (compare old-v new-v)) |
52 | | - ;; Ignore Entities and arrays of Entities |
53 | | - (not (or (instance? hbjs/Entity new-v) |
54 | | - (and (array? new-v) |
55 | | - (= (count new-v) (count old-v)) |
56 | | - (instance? hbjs/Entity (nth new-v 0)))))) |
57 | | - (reduced (debug-msg true "cache:miss" "value changed" |
58 | | - #js {:entity-id (get e "id") |
59 | | - :attr-path (clj->js ks) |
60 | | - :e e |
61 | | - :old-v old-v |
62 | | - :new-v new-v |
63 | | - :entities (clj->js entities) |
64 | | - :cache (clj->js cached-entities)}))))) |
65 | | - nil cached-e))) |
66 | | - (reduced true))) |
67 | | - nil entities))) |
| 40 | + |
| 41 | + (and track-count? |
| 42 | + (not (clojure.set/superset? |
| 43 | + (set (keys cached-entities)) |
| 44 | + (set (map #(get % "id") entities))))) |
| 45 | + (debug-msg true "cache:miss" "cache not superset of entities" |
| 46 | + #js {:entities (clj->js entities) |
| 47 | + :cache (clj->js cached-entities)}) |
| 48 | + |
| 49 | + :else |
| 50 | + (reduce |
| 51 | + (fn [_ e] |
| 52 | + (when (let [id (get e "id") |
| 53 | + cached-e (get cached-entities id)] |
| 54 | + (if (nil? cached-e) |
| 55 | + (if-not id |
| 56 | + (reduced false) ; This entity has probably been removed, do not force a rerender |
| 57 | + (reduced (debug-msg true "cache:miss" "not in cache" |
| 58 | + #js {:entity-id id |
| 59 | + :entities (clj->js entities) |
| 60 | + :cache (clj->js cached-entities)}))) |
| 61 | + (reduce (fn [_ [ks old-v]] |
| 62 | + (let [e-without-cache (hbjs/Entity. ^de/Entity (.-_entity e) nil nil nil nil) |
| 63 | + new-v (.apply (.-get e-without-cache) e-without-cache (into-array ks))] |
| 64 | + (when (and (not= 0 (compare old-v new-v)) |
| 65 | + ;; Ignore Entities and arrays of Entities |
| 66 | + (not (or (instance? hbjs/Entity new-v) |
| 67 | + (and (array? new-v) |
| 68 | + (= (count new-v) (count old-v)) |
| 69 | + (instance? hbjs/Entity (nth new-v 0)))))) |
| 70 | + (reduced (debug-msg true "cache:miss" "value changed" |
| 71 | + #js {:entity-id id |
| 72 | + :attr-path (clj->js ks) |
| 73 | + :e e |
| 74 | + :old-v old-v |
| 75 | + :new-v new-v |
| 76 | + :entities (clj->js entities) |
| 77 | + :cache (clj->js cached-entities)}))))) |
| 78 | + nil cached-e))) |
| 79 | + (reduced true))) |
| 80 | + nil entities))) |
68 | 81 |
|
69 | 82 | (defn cache->js [entity cached-entities] |
70 | 83 | (reduce |
|
173 | 186 | run-query (react/useCallback |
174 | 187 | (fn run-query [] |
175 | 188 | (let [result (try-hook "useQuery" #(apply hbjs/q query conn args))] |
176 | | - (when (not= (count result) (count @cached-entities)) |
| 189 | + (when (and (not= (count result) (count @cached-entities)) |
| 190 | + (not= 0 (count result))) |
177 | 191 | (reset! cached-entities {})) |
178 | 192 | (.map result (fn [e] (touch-entity-cache e cached-entities))))) |
179 | 193 | #js [query args]) |
|
0 commit comments