Developing a reactivity system with Lamport clocks and incremental computation
v5.chriskrycho.com
v5.chriskrycho.com
Not having to maintain complex data structures to keep track of property dependencies as opposed to using light weight integer comparison to track changes. I can imagine @tracked being many times faster than @computed
<script>
let maxLength = 10;
let name = "", remaining, showError;
$: showError = remaining < 0;
$: remaining = maxLength - name.length;
</script>
<div>
<input bind:value={name} />
<p class:showError>{remaining}</p>
</div>
And the gist of the runtime produced is this: if (dirty & 1 && input.value !== ctx[0]) set_input_value(input, ctx[0]);
if (dirty & 2) set_data(t1, ctx[1]);
if (dirty & 4) toggle_class(p, "showError", ctx[2]);
wired up directly to the input event which computes the dirty flags. No VM in the middle. It is quite literally impossible to get more efficient than that. Are there still any advantages to this approach?BTW) I would say that writing a whole compiler to add UI update logic is much ommore over engineering than this solution, or just about any JS reactivity system.
I'm surprised you'd think of that as a leaky abstraction, but not a `@tracked` decorator. They are both 'different dialects of JS', and both use a compiler.
In terms of the tradeoffs here, I haven’t dug deeply enough into Svelte to have a reasonable opinion beyond the first blush “I really like this!” The way it uses compilation and repurposes unused JS features to build a reactivity DSL is very clever and interesting and the compilation output is smart and well-optimized, even if I find the use of exports to be a bit quirky.