Skip to content

Add createReducer - #10

Merged
LPGhatguy merged 2 commits into
Roblox:masterfrom
AmaranthineCodices:createReducer
Mar 4, 2018
Merged

Add createReducer#10
LPGhatguy merged 2 commits into
Roblox:masterfrom
AmaranthineCodices:createReducer

Conversation

@AmaranthineCodices

Copy link
Copy Markdown
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

coveralls commented Mar 3, 2018

Copy link
Copy Markdown

Coverage Status

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

@LPGhatguy

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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