Created
March 27, 2018 06:40
-
-
Save factoryhr/b6d23eb76b1ddc39717b4f065c1f1673 to your computer and use it in GitHub Desktop.
This file contains 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
function createStore(reducer, preloadedState) { | |
var currentState = preloadedState; | |
// Keep track of all listeners | |
var listeners = []; | |
function getState() { | |
return currentState; | |
} | |
// Subscribe new listeners to the store | |
function subscribe(listener) { | |
listeners.push(listener); | |
// Provide method for removing the subscription | |
return function unsubscribe() { | |
var index = listeners.indexOf(listener); | |
listeners.splice(index, 1); | |
}; | |
} | |
return { | |
getState: getState, | |
// Expose the API | |
subscribe: subscribe | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment