Coder Social home page Coder Social logo

Dealing with re-entrant updates about fusion HOT 2 CLOSED

dphfox avatar dphfox commented on June 3, 2024
Dealing with re-entrant updates

from fusion.

Comments (2)

dphfox avatar dphfox commented on June 3, 2024

The new execution model should help solve for this. Invalidation is a completely controlled process; no state objects are invoked during that stage. Eager objects are only revalidated after all invalidation is complete.

Revalidation is a little more interesting. It's conceivable that, during revalidation, an object can cause state objects to become invalidated. In particular, an object might invalidate itself, or it might be transitively invalidated if it invalidates a state object it transitively depends on.

For example:

local foo = scope:Value(2)

local bar = scope:Computed(function(use)
   return use(foo) * 2
end)

-- imagine this ran synchronously
local baz = scope:Observer(bar):onBind(function()
    foo:set(peek(foo) + 1)
end)

The specific question we should ask here is: what should happen when a state object becomes invalid during revalidation?. Seemingly, the answer would be "revalidate itself again", but this could easily lead to infinite loops. Really, what we're dealing with here is a "hidden cycle" in the reactive graph.

So, I would propose we add an error for this case:

Detected an infinite loop. Consider adding an explicit breakpoint to your code to prevent a cyclic dependency.

Notably, this can happen during invalidation too, if the graph has an explicitly-modelled cycle in it. However, we should be careful - unlike revalidation, objects being invalid is not itself an invalid state for the invalidation process, because objects can be invalidated multiple times by design. We'd likely need to track state inside the invalidation process to catch this.

However - if you noticed the 'synchronous' stipulation above - this would only really catch errors in the implementations of state objects. We'd need something more sophisticated to solve for user-introduced loops from asynchronous sources like Observers.

Overall, I think that tracking infinite loops during invalidation is a bit of a distraction, since the process can be implemented in a way that trivially supports cyclic graphs by merely terminating when encountering an already-transitively-invalidated graph object, which the invalidation process is marking out anyway. So revalidation is the only part I think we need to care about, which is good because it makes all the scary questions go away.

from fusion.

dphfox avatar dphfox commented on June 3, 2024

Solved in #323

from fusion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.