milomg / reactively

MIT License
415 stars 23 forks source link

Stabilize never called and other questions #10

Open delaneyj opened 1 year ago

delaneyj commented 1 year ago

Ported your logic to a Go project that needs reactivity.

All test pass and the results are very promising. I ported benchmark as well. Used it with no threading to try to be as close to 1:1 with the current numbers.

On my machine image

Port image

My question is more of understanding some of the details

  1. It appears stale() puts things in the EffectQueue and stabilize() takes them out. But I don't see stabilize() called anyway in the repository.
  2. When creating a reactive there is the option effect boolean. When and why should that be set? I'm familiar with Vue & Solid's computed vs effect but is it the same model here?
  3. Thank you for the blog post and the work on this algorithm!
milomg commented 1 year ago
  1. Yep, we wanted a fully lazy system which allows flexibility for running all effects in an event loop in a game. However, you could pretty quickly add a batching system by adding a line after EffectQueue.push(this); to schedule a stabilize (i.e.
    if (!stabilizeQueued) {
    stabilizeQueued = true;
    requestAnimationFrame(stabilize);
    }
  2. Yep, I was trying to fuse signals, computeds, and effects into one Reactive primitive, but I didn't entirely succeed in abstracting away here. Passing in a function into the first argument makes it a computed or effect, and setting the effect flag will make it automatically run when dependencies change. Otherwise it is a memo, and lazily runs but efficiently caches. In other words, just wrap all reactive values and functions in reactive, and when the functions should be rerun, pass in effect=true. (The reason this isn't the default is because sometimes you have many different outputs and you only want to sample one and not compute the others unless they are used)
  3. Thanks! I'm glad to hear it was helpful
milomg commented 1 year ago

Update: we've added an API to do automatic stabilization: https://github.com/modderme123/reactively/blob/main/packages/core/src/core.ts#L324.

Just calling autoStabilize will automatically schedule microtasks which call stabilize