1.3.0: More robust dependency detection in binding functions #275
Tao-VanJS
announced in
Announcements
Replies: 2 comments
-
Amazing, thank you for the fast update! |
Beta Was this translation helpful? Give feedback.
0 replies
-
Great news! I had some workarounds when first used it here but glad that I can remove these ugly hacks with 1.3.0. Keep inspiring us (: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi fellow VanJSers,
I'm happy to announce the release of VanJS
1.3.0
. 🎉🎉🎉This release improves the dependency detection mechanism in binding functions. Specifically, before the
1.3.0
release, things could be very tricky if we define a self-referencing side effect (i.e.: setting theval
of a state while reading the stateval
in the same side effect). To illustrate the problem, let's consider this side effect defined withvan.derive
:The intention of the side effect is to increment state
numChecked
whenever statechecked
istrue
. However, this definition is problematic prior to1.3.0
release. The problem is:++numChecked.val
, which de-sugars tonumChecked.val = numChecked.val + 1
, makes the side effect depend onnumChecked
state (as it reads theval
ofnumChecked
in the binding function). Thus, whenevernumChecked
is set elsewhere, it will trigger the side effect defined above, which will increment thenumChecked
state, and then trigger the side effect again, and increment thenumChecked
again, ... - an endless loop. Eventually a stack overflow error will occur to stop the loop, leavingnumChecked
state ending in an arbitrary number.VanJS
1.3.0
adjusts the dependency detection mechanism in this situation to avoid the problem. That is, if we're setting theval
property of some state inside a binding function (be it invan.derive
, for state-derived properties, or for state-derived child nodes), that state will not be considered as a dependency of the binding function, even if itsval
property is being read there. The adjustment is aimed to avoid the self-referencing problem discussed above, making it impossible to trigger an side effect to update a state that re-triggers the same side effect again. Thus in VanJS1.3.0
or later, settingnumChecked
will always lead to deterministic behavior, without triggering the self-referencing side effect.You can read more about the new dependency detection mechanism here: https://vanjs.org/advanced#self-referencing-in-side-effects.
The new dependency detection mechanism also fixes a problem in VanX.
vanX.replace
in a side effect causes an infinite loop in prior VanJS versions but now works perfectly in VanJS1.3.0
. The piece of code below will have the correct behavior in VanJS1.3.0
:Try on jsfiddle (problematic with VanJS 1.2.8)
Try on jsfiddle (perfect with VanJS 1.3.0)
With the improvement, the bundle size increases slightly. Gzipped bundle increases to
1049 bytes
(1.0kB) from1021 bytes
(1.0kB) (28 bytes
increase), while minified bundle increases to1923 bytes
(1.9kB) from1831 bytes
(1.8kB) (92 bytes
increase) - still being the smallest reactive UI framework in the world.❤️ Hope you can enjoy!
Beta Was this translation helpful? Give feedback.
All reactions