Skip to content

Instantly share code, notes, and snippets.

@inooid
Created December 16, 2016 14:53
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 inooid/c9e3709da0098605b3edb68b278099e3 to your computer and use it in GitHub Desktop.
Save inooid/c9e3709da0098605b3edb68b278099e3 to your computer and use it in GitHub Desktop.
Simple Redux source code... kinda
function createStore(reducer, initialState) {
let currentState = initialState;
let listeners = [];
function getState() {
return currentState;
}
function dispatch(action) {
// Create the state from the reducer
currentState = reducer(currentState, action);
// Notify listeners
listeners.forEach(listener => {
listener();
});
return action;
}
function subscribe(callback) {
listeners.push(callback);
}
return {
getState,
dispatch,
subscribe,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment