DEV Community

Discussion on: Building a Reactive Library from Scratch

Collapse
 
goldeli profile image
缪宇 • Edited

Hey Ryan, great article, I benefit a lot from it, thank you so much.

Q: Why use context stack?

I replaced the context stack, and it words fine. full code

let context = ""
Enter fullscreen mode Exit fullscreen mode
const read = () => {
    const running = context;
    if (running) subscribe(running, subscriptions);
    return value;
};
Enter fullscreen mode Exit fullscreen mode
const execute = () => {
    cleanup(running);
    context = running;
    try {
      fn();
    } finally {
      context = "";
    }
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ryansolid profile image
Ryan Carniato

Nested reactivity. Generally you can nest effects or derivations inside themselves. This is important to how Solid renders but I suppose wasn't needed for this simple example.

Collapse
 
goldeli profile image
缪宇

I nested effects, but it also works fine. full code

Thread Thread
 
ryansolid profile image
Ryan Carniato • Edited

If you try to subscribe after the inner effect it doesn't track. Change the example to:

createEffect(() => {
  createEffect(() => {
    log("value is ", value());
  });
  log("name is ", name());
});
Enter fullscreen mode Exit fullscreen mode

Keep in mind this is just a simple approximation. I don't actually use an array in my libraries and instead just store the out context and then restore it.