Skip to content

Instantly share code, notes, and snippets.

@AsaAyers
Created April 27, 2015 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsaAyers/d25f1bf0ce3394412670 to your computer and use it in GitHub Desktop.
Save AsaAyers/d25f1bf0ce3394412670 to your computer and use it in GitHub Desktop.
Should dispatchers transport Immutable objects or plain data?

Rubber Duck Debugging

Since my Flux stores have Immutable data I'm wondering if it's better to construct those Immutable objects from the raw data before or after sending it through the dispatcher.

Example: I'm building a Twitter clone and I have a MessageStore. One of my views calls messageActions.create("Hello World").

create: function(content) {
    dispatcher.handleViewAction({
        type: ACTIONS.NEW_MESSAGE,
        message: { // convert inside the store
            owner: ...,
            content: content,
        }
    })
}

create: function(content) {
    dispatcher.handleViewAction({
        type: ACTIONS.NEW_MESSAGE,
        message: Immutable.Map({ // convert inside the action
            owner: ...,
            content: content,
        })
    })
}

This is the Store's responsibility

Constructing the Immutable objects action side seems nice at first because it seems like rollbacks would be easier. If you optimistically edit something in the store and the request fails you just send an unmodified version back through the store to reset.

If you continue this further (past simple object creation) you find that your Actions would need to fetch objects from the store, generate a modified version and send it through the dispatcher. This just turns your Action into a glorified setter. If the action manipulates multiple stores it would send multiple items through the dispatcher instead of sending the information about an action through the dispatcher and letting the stores handle updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment