flux icon indicating copy to clipboard operation
flux copied to clipboard

Dependency tracking (abusing the waitFor API)

Open dzrw opened this issue 11 years ago • 3 comments

It looks like there is some code to prevent nested dispatch and some cycle-detection logic in https://github.com/facebook/flux/blob/master/src/Dispatcher.js. However, it looks like that defense can be subverted by combining waitFor with named function expressions.

dispatcher.register(function handler () {
    var tok = dispatcher.register(handler); // appends a new handler to _callbacks, increments id
    dispatcher.waitFor([tok]); // directly invokes the new handler
    // without a base case, this doesn't terminate (stack overflow)
});

dispatcher.dispatch({}); 

This contrived example reveals a couple of problems. First, _callbacks is not locked for updates during the scope of a dispatch operation. Second, the way dependencies are expressed using this API is open to confusion and error. A quick fix might be to assert invariant(!_isDispatching) within register and unregister, but consider redesigning the register function to accept an array of tokens as a parameter. Then you could resolve a proper dependency graph. See RequireJS for an example of this sort of API.

dzrw avatar Feb 02 '15 08:02 dzrw

Interesting point about the potential for abuse, but I disagree that we want to pass an array of tokens to register(). This would create the same dependency graph for every action, and we want different dependency graphs per action.

I like the idea of the invariants in register and unregister, though. I will float that idea internally at FB to @yungsters and others, and if the idea is accepted by those folks, I'll ask you for a pull request.

fisherwebdev avatar Feb 04 '15 06:02 fisherwebdev

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

ghost avatar Aug 05 '15 18:08 ghost

Opened #296 to track adding in these invariants.

kyldvs avatar Nov 18 '15 21:11 kyldvs