Created
September 8, 2020 07:36
-
-
Save perjo927/3a5ad39933a34717101ef0558e177364 to your computer and use it in GitHub Desktop.
Create Redux Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { makeSubscriber } from "./subscribe"; | |
import { makeStateHandlers } from "./state"; | |
import { makeDispatcher } from "./dispatch"; | |
export const createStore = (reducer, initialState = {}) => { | |
const stateContainer = [initialState]; | |
const subscribers = []; | |
const stateHandlers = { | |
...makeStateHandlers(stateContainer), | |
}; | |
const onStateChange = () => { | |
subscribers.forEach((subscription) => subscription()); | |
}; | |
const { dispatch } = makeDispatcher(stateHandlers, reducer, onStateChange); | |
const { subscribe } = makeSubscriber(subscribers); | |
return { | |
getState: stateHandlers.getState, | |
dispatch, | |
subscribe, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment