DEV Community

Discussion on: Building a Reactive Library from Scratch

Collapse
 
ryansolid profile image
Ryan Carniato

You wouldn't want to cleanup yet. Like picture it is a DOM widget like a jQuery Chart.. you wouldn't cleanup until the end. The cleanup being run is the previously registered one. It goes something like this:

// run 1
cleanup(); // no-op
fn(); // execute 1

// run 2
cleanup(); // cleanup 1
fn(); // execute 2

// run 3
cleanup(); // cleanup 2
fn(); // execute 3

// parent removes child
cleanup(); // cleanup 3
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mehuge profile image
Mehuge

The dependencies need to remain so that if a signal happens it will trigger a reaction?

Thread Thread
 
ryansolid profile image
Ryan Carniato

Yes that too. That's probably the more foundational answer. The reason we do cleanup each time is it lets our dependencies be dynamic. There are optimizations to be done around this but this is the core mechanism that you will find in MobX or Vue, along with Solid.