What is the difference between (e/for [x table] <body>) and (let [x table] <body>) ?
I know the practical difference of
;; Renders [:div "foo"] and [:div "bar"]
(e/for [x (e/amb "foo" "bar" )]
(dom/div (dom/text x)))
;; Only renders [:div "foo"]
(let [x (e/amb "foo" "bar")]
(dom/div (dom/text x)))
but I don't understand how to think about it :^)Official answer: don't use e/amb directly, to render collections use the new virtual scroll patterns documented in the new tutorial
I am happy to respond to your actual question but https://xyproblem.info/ what are you trying to actually do
(dom/div (dom/text (e/amb "foo" "bar"))) is racing a "foo" and a "bar" into the same textnode, because the dom macros use the "mount point" infrastructure to reflect their location in the DAG to a corresponding location in the DOM
I'm not xy-ing. I'm trying to improve my understanding of Electric 馃檪
ah ok. Was my answer sufficient?
Yeah, I think I got it after thinking now 馃槃
Is this right:
With let, dom/div is only mounted once and then dom/text is mounted (e/Count (e/amb "foo" "bar")) times in the same environment, I.e with dom/node bound to the same div meaning the text of the div is being continually replaced rather than accreting new text nodes / element nodes in the dom.
;; Surgically mounts dom/text which each value of x, always
;; in the context of dom/node being the same div
(let [x (e/amb "foo" "bar")]
(dom/div (dom/text x)))
;; Creates parallel branches for each superposed value
(e/for [x (e/amb "foo" "bar")]
(dom/div (dom/text x)))I will study the new virtual scroll patterns tonight 馃
yes
and if we implemented dom/text with e/for inside you'd get a third picture
@xifi now I'm curious :^)
if I remove the unnecessary parts for this discussion, dom/text boils down to
(e/defn Text [arg]
(let [e (.createTextNode js/document "")]
(set! (.-textContent e) arg)))
Notice e doesn't depend on arg so we only create 1 text node, but set! does and therefore we get 2 set!s for (dom/div (dom/text (e/amb "foo" "bar"))).
But we could decide to support e/amb argument
(e/defn Text [arg]
(e/for [v arg]
(let [e (.createTextNode js/document "")]
(set! (.-textContent e) v))))
Now the same code would mount 1 div but 2 text nodes.Ohh right, very cool!