wavesoft / dot-dom

.dom is a tiny (512 byte) template engine that uses virtual DOM and some of react principles
Apache License 2.0
809 stars 53 forks source link

Release 0.4.0 #57

Open wavesoft opened 4 years ago

wavesoft commented 4 years ago

This is the preview of the 0.4.0 release

Changelog

mindplay-dk commented 4 years ago

Regarding this bit...

https://github.com/wavesoft/dot-dom/blob/ddd6d50f9609b97f79ee9af10387598cfa19e7a5/src/dotdom.js#L235-L241

First of all, if this expression is running, don't we already know that the key didn't match, and thus the DOM node must be new?

Secondly, it looks like _prevnode isn't the "previous node", as I first thought - it looks like this is actually the "previous vnode", so if _prevnode did evaluate as truthy, you would actually end up passing a vnode object, rather than a DOM node, to insertBefore, which wouldn't work.

My suspicioun is that _vnode always evaluates as falsy and the second expression after the || operator always executes - so I think you can actually just delete the _prevnode || part of the expression.

Am I wrong??

wavesoft commented 4 years ago

Not true.

1) _prevnode is the previous DOM element. It will be falsy in any of the following conditions:

2) Note that a few properties from the VDom element are copied onto the DOM element in order to save some space by not carrying around the state variables. These are assigned here and defined here. That's why some time it's confusing to find out which is a DOM and which is a VDom element.

3) This particular branch does the following:

mindplay-dk commented 4 years ago

Oh man, unpacking this code is really tricky... I wish like hell there was a tool that would automate these spooky compression patterns so no one would need to write code like this. 😐

Anyway, thanks for the explanation. 🙂

mindplay-dk commented 4 years ago

Oh, one thought about keyed updates: I noticed you use the user-supplied key and then fall back to a generated key, e.g.:

key = userKey || " " + elementType + nextIndexFor(elementType)

In my own experiments, the element type is always part of the key - something like:

key = elementType + " " + (userKey || nextIndexFor(elementType))

Otherwise, if we were to diff between, say, <div key="foo"> and <img key="foo">, these would be seen as the same element, wouldn't they?

mindplay-dk commented 3 years ago

Any reason this got stranded here?

I think the only issue was the element-type missing from the key?

Other than that, it looks good to go?