DEV Community

Discussion on: Building a Reactive Library from Scratch

Collapse
 
ryansolid profile image
Ryan Carniato

I mean I have a working example in the article. I'm basically backlinking the signal because on effect re-run we have to unsubscribe from all the signals that it appears in their subscription lists. So the link has to work both ways. The Signal here doesn't have an instance really and I just needed the subscriptions so I added that. I could have made it a callback maybe for clarity. This demo isn't how Solid actually works completely. It's just a way of doing the basics with next to no code.

Collapse
 
mindplay profile image
Rasmus Schultz

Yeah, the example works - I guess I have trust issues with plain JS without types. I've seen too much JS that "works", but not for the right reason, or not the way it appeared to work, if you know what I mean?

I think types would better explain the data structures and concepts here - rather than making the reader reverse engineer the code to try to infer what's what. What's running, what's in dependencies, and so on.

It's challenging even after watching the video and reading the article.

I still can't make sense of the types.

Thread Thread
 
peerreynders profile image
peerreynders

I still can't make sense of the types.

Sometimes clarity comes from the chosen names.

Change #1:

// A Dependency has many different Subscribers depending on it
// A particular Subscriber has many Dependencies it depends on
type Dependency = Set<Subscriber>;
type Subscriber = {
  execute(): void;
  dependencies: Set<Dependency>;
};
Enter fullscreen mode Exit fullscreen mode

Change #2:

  const subscriptions: Dependency = new Set();
Enter fullscreen mode Exit fullscreen mode

Change #3:

function cleanup(running: Subscriber) {
Enter fullscreen mode Exit fullscreen mode

Change #4:

  const running: Subscriber = {
    execute,
    dependencies: new Set(),
  };
Enter fullscreen mode Exit fullscreen mode

TypeScipt Playground

Thread Thread
 
mindplay profile image
Rasmus Schultz
// A Dependency has many different Subscribers depending on it
// A particular Subscriber has many Dependencies it depends on
Enter fullscreen mode Exit fullscreen mode

Yes! This is what the article/video/example was missing. Thank you! 😀🙏