DEV Community

Discussion on: Building a Reactive Library from Scratch

Collapse
 
lukasbombach profile image
Lukas Bombach

Hey Ryan,

I love this article, thank you for this!

You write that the algorithm you use here needs more work to prevent doing too doing too much work. Can you elaborate on that? I understood that each signal you update / set will set a chain in motion where every step will only trigger the next steps that are actually required. After each „chain reaction“ the whole dependency graph will be cleaned up and we‘re up to a fresh start. Sounds like no work can be done that should not be done, what am I missing, or getting wrong?

Collapse
 
ryansolid profile image
Ryan Carniato

This is true until you move to updating multiple signals in the same transaction. Then things get more complicated because you have to start somewhere but if you notify and execute off one signal and one of the descendants also depends on the other that changed you'd end up running it twice with a naive approach. Not to mention if you allow derivations to conditionally notify, like only if their value changes, simple approaches don't work.

Collapse
 
lukasbombach profile image
Lukas Bombach

Ah, makes sense! Thank you!

Collapse
 
hugeletters profile image
Evgenii Perminov

Late reply but a simple example

const a = createSignal(1);
const b = createComputed(() => a.value * 3);
createEffect(() => logEffect(`A is ${a.value} and B is ${b.value}`));
a.value = 2;

Enter fullscreen mode Exit fullscreen mode

This effect runs three times, not two - because update to a triggers update to b which triggers effect but also update to a itself triggers effect too