Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
cleanup seq API, now keyed-seq and simple-seq
Browse files Browse the repository at this point in the history
as it will be very important in apps to choose the correct variant
the function call should be explicit instead of relying on
whether the second argument is nil or not.

now just need to write three pages of explanation about when to
use which. TL;DR: simple for things that don't change order and only
remove/add at end. keyed for pretty much all other cases.
  • Loading branch information
thheller committed Mar 1, 2021
1 parent 55c0794 commit 1afbb96
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
9 changes: 7 additions & 2 deletions src/main/shadow/experiments/arborist.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@
(defn fragment [& body]
(throw (ex-info "fragment can only be used a macro" {})))

(defn simple-seq [coll render-fn]
(coll/simple-seq coll render-fn))

(defn render-seq [coll key-fn render-fn]
(when (some? coll)
(coll/node coll key-fn render-fn)))
(coll/keyed-seq coll key-fn render-fn))

(defn keyed-seq [coll key-fn render-fn]
(coll/keyed-seq coll key-fn render-fn))

(defn update! [x next]
(p/update! x next))
Expand Down
40 changes: 21 additions & 19 deletions src/main/shadow/experiments/arborist/collections.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
(fn [^KeyedItem item]
(if (contains? new-keys (.-key item))
true
(do (p/destroy! (.-managed item) true)
(do (p/destroy! ^not-native (.-managed item) true)
false))))]

;; old-items now matches what is in the DOM and only contains items still present in new coll
Expand All @@ -109,7 +109,7 @@
;; item does not exist in old coll, just create and insert
(not old-item)
(let [rendered (rfn (.-data new-item) idx (.-key new-item))
managed (p/as-managed rendered env)]
^not-native managed (p/as-managed rendered env)]

(p/dom-insert managed dom-parent anchor)

Expand Down Expand Up @@ -164,7 +164,7 @@
;; not updatable, swap.
;; unlikely given that key was the same, result should be the same.
;; still possible though
(let [new-managed (p/as-managed rendered env)]
(let [^not-native new-managed (p/as-managed rendered env)]
(p/dom-insert new-managed dom-parent anchor)
(when dom-entered?
(p/dom-entered! new-managed))
Expand Down Expand Up @@ -207,7 +207,7 @@
;; not updatable, swap.
;; unlikely given that key was the same, result should be the same.
;; still possible though
(let [new-managed (p/as-managed rendered env)]
(let [^not-native new-managed (p/as-managed rendered env)]
(set! new-item -managed new-managed)
(p/dom-insert new-managed dom-parent anchor)
(when dom-entered?
Expand Down Expand Up @@ -282,6 +282,20 @@
;; compare coll last since its pointless if the others changed and typically more expensive to compare
(= coll (.-coll other)))))

(defn keyed-seq [coll key-fn render-fn]
{:pre [(sequential? coll)
(ifn? key-fn)
(ifn? render-fn)]}

;; we always need compatible collections, it should already be a vector in most cases
;; it must not allow lazy sequences since the sequence may not be used immediately
;; some item may suspend and whatever the lazy seq did will happen in totally different phases
;; cannot guarantee that some other data it previously may have relied upon is still valid
(let [coll (vec coll)]
(if (zero? (count coll))
nil ;; can skip much unneeded work for empty colls
(KeyedCollectionInit. coll key-fn render-fn))))

(declare SimpleCollectionInit)

(deftype SimpleItem [data managed])
Expand Down Expand Up @@ -421,22 +435,10 @@
;; compare coll last since its pointless if the others changed and typically more expensive to compare
(= coll (.-coll other)))))

(defn node [coll key-fn render-fn]
(defn simple-seq [coll render-fn]
{:pre [(sequential? coll)
(ifn? render-fn)]}
;; we always need compatible collections, it should already be a vector in most cases
;; it must not allow lazy sequences since the sequence may not be used immediately
;; some item may suspend and whatever the lazy seq did will happen in totally different phases
;; cannot guarantee that some other data it previously may have relied upon is still valid
(let [coll (vec coll)]
(cond
(zero? (count coll))
(if (zero? (count coll))
nil ;; can skip much unneeded work for empty colls

;; FIXME: should likely use simple path for really small colls
;; or maybe some other metrics we can infer here?
(some? key-fn)
(KeyedCollectionInit. coll key-fn render-fn)

:else
(SimpleCollectionInit. coll render-fn))))
(SimpleCollectionInit. coll render-fn))))
7 changes: 5 additions & 2 deletions src/main/shadow/experiments/grove.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[shadow.experiments.arborist.common :as common]
[shadow.experiments.arborist.fragments] ;; util macro references this
[shadow.experiments.arborist :as sa]
[shadow.experiments.arborist.collections :as sc]
[goog.async.nextTick]
[shadow.experiments.grove.protocols :as gp]
[shadow.experiments.grove.components :as comp]
Expand Down Expand Up @@ -221,11 +222,13 @@
(suspense/SuspenseInit. opts vnode))

(defn simple-seq [coll render-fn]
(sa/render-seq coll nil render-fn))
(sc/simple-seq coll render-fn))

(defn render-seq [coll key-fn render-fn]
(sa/render-seq coll key-fn render-fn))
(sc/keyed-seq coll key-fn render-fn))

(defn keyed-seq [coll key-fn render-fn]
(sc/keyed-seq coll key-fn render-fn))

(deftype TrackChange [^:mutable val ^:mutable trigger-fn ^:mutable result env component idx]
gp/IBuildHook
Expand Down

0 comments on commit 1afbb96

Please sign in to comment.