Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add createReducer #10

Merged
merged 2 commits into from
Mar 4, 2018
Merged

Conversation

AmaranthineCodices
Copy link
Contributor

This fixes #7.

It adds a createReducer function that works effectively identically to the redux-create-reducer library. Example usage (taken from the tests):

local reducer = createReducer({
    a = 0,
    b = 0,
}, {
    a = function(state, action)
        return {
            a = state.a + 1,
            b = state.b
        }
    end,
    b = function(state, action)
        return {
            a = state.a,
            b = state.b + 2,
        }
    end,
})

The first argument is initialState, which will be returned if and only if the state provided to the reducer is nil. The second argument is a map of action types to reducer functions; the reducer corresponding to the action type will be used. If the action type is not in the map, the composite reducer will return the same value of state that it received.

@coveralls
Copy link

coveralls commented Mar 3, 2018

Coverage Status

Coverage increased (+0.08%) to 99.029% when pulling 3b4c525 on AmaranthineCodices:createReducer into 9efe558 on Roblox:master.

@LPGhatguy
Copy link
Contributor

An interesting side-effect of createReducer is that it assumes the shape of your actions (that they have a type field) -- is that something we should be comfortable nailing down?

@LPGhatguy LPGhatguy self-requested a review March 3, 2018 23:31
@AmaranthineCodices
Copy link
Contributor Author

Redux has an API requirement that actions have a type field. I think this is a reasonable API constraint to impose - without a field to denote the type of action, there isn't much of a point to actions. Naming the field type seems logical enough.

It might be helpful to throw an error in dispatch if the action does not have a type field.

@LPGhatguy
Copy link
Contributor

Oh wow, I didn't realize that or notice it the last time I skimmed the documentation. Cool!

That check can come in a later PR if you're up for it, that would be a nice lint to have especially with middleware.

@LPGhatguy LPGhatguy merged commit b77f135 into Roblox:master Mar 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose createReducer utility?
3 participants