flux icon indicating copy to clipboard operation
flux copied to clipboard

Docs on dispatcher is outdated (or just wrong)

Open robrobbins opened this issue 11 years ago • 4 comments

Just noticed that the documentation located here states that a call to ...waitFor(...) takes a second argument, a callback. I am pretty sure that is not correct.

robrobbins avatar Dec 05 '14 17:12 robrobbins

Yes this is an artifact of how Flux was initially released with my own less-than-ideal Dispatcher instead of the real one that Facebook uses in production. Please feel free to correct the error with a pull request.

fisherwebdev avatar Dec 08 '14 01:12 fisherwebdev

Hope this doesn't count as a hijack, but I have a few thoughts I'd like to see clarification on as well.

I'm trying to reconcile the Promise based implementation in TodoMVC with the callback based approach in the chat example. The callback driven one is newer. Should we be aware of some pitfalls in using promises that prompted this evolution?

My own thoughts:

The for loop indicates that all callbacks are invoked synchronously. This line is invoked after iterating over dependencies - if you're using waitFor, the dispatch order "sorts itself out" as we recursively crawl dependencies, otherwise we simply invoke the callbacks in the order the stores registered themselves with the dispatcher †.

It does seem like this would simplify reasoning about the dispatch loop - there's no possibility of the dispatch loop yielding because of an asynchronous callback, only to be overtaken by a subsequent action. In this case, the callbacks all run to completion. If you need to do something asynchronous, you should dispatch an action and when the asynchronous thing you're doing resolves, have it trigger it's own dispatch loop instead of blocking the current one - something which I believe is possible with the promise implementation.

Tick ViewMessageAction DeleteMessageAction
1 MessageStore
2 MessageStore.pending MessageStore
3 .. HeaderStore
4 MessageStore.fulfilled
5 HeaderStore

† It seems like this might lead to inadvertent sequential coupling between stores. The golang folks recently randomized the the order of keys inside their map's in order to keep developers from relying on (what I assume was) insertion order. I'm wondering if similar reasoning can't be applied to the dispatcher?

jacobstr avatar Dec 12 '14 04:12 jacobstr

One of the major problems with the Promise-based solution was that I could not figure out how to detect a circular dependency. In the FB production implementation, with the synchronous loop, this error is detected and displayed to the programmer: https://github.com/facebook/flux/blob/master/src/Dispatcher.js#L162-L167

fisherwebdev avatar Dec 21 '14 06:12 fisherwebdev

The Flux overview page has been updated. Among other changes, it now accurately reflects the API of the FB dispatcher available on npm.

fisherwebdev avatar Jan 09 '15 18:01 fisherwebdev