-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace class with a factory function
- Loading branch information
Showing
4 changed files
with
59 additions
and
66 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,63 @@ | ||
import Store from './Store'; | ||
import invariant from 'invariant'; | ||
import isPlainObject from './utils/isPlainObject'; | ||
|
||
// Don't ever try to handle these action types in your code. They are private. | ||
// For any unknown actions, you must return the current state. | ||
// If the current state is undefined, you must return the initial state. | ||
export const ActionTypes = { | ||
INIT: '@@redux/INIT' | ||
}; | ||
|
||
export default function createStore(reducer, initialState) { | ||
const store = new Store(reducer, initialState); | ||
invariant( | ||
typeof reducer === 'function', | ||
'Expected the reducer to be a function.' | ||
); | ||
|
||
let currentReducer = null; | ||
let currentState = initialState; | ||
let listeners = []; | ||
|
||
function getState() { | ||
return currentState; | ||
} | ||
|
||
function subscribe(listener) { | ||
listeners.push(listener); | ||
|
||
return function unsubscribe() { | ||
const index = listeners.indexOf(listener); | ||
listeners.splice(index, 1); | ||
}; | ||
} | ||
|
||
function dispatch(action) { | ||
invariant( | ||
isPlainObject(action), | ||
'Actions must be plain objects. Use custom middleware for async actions.' | ||
); | ||
|
||
currentState = currentReducer(currentState, action); | ||
listeners.forEach(listener => listener()); | ||
return action; | ||
} | ||
|
||
function getReducer() { | ||
return currentReducer; | ||
} | ||
|
||
function replaceReducer(nextReducer) { | ||
currentReducer = nextReducer; | ||
dispatch({ type: ActionTypes.INIT }); | ||
} | ||
|
||
replaceReducer(reducer); | ||
|
||
return { | ||
dispatch: ::store.dispatch, | ||
subscribe: ::store.subscribe, | ||
getState: ::store.getState, | ||
getReducer: ::store.getReducer, | ||
replaceReducer: ::store.replaceReducer | ||
dispatch, | ||
subscribe, | ||
getState, | ||
getReducer, | ||
replaceReducer | ||
}; | ||
} |
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
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