-
-
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.
* “Stateless Stores” are now called reducers. (#137 (comment)) * The “Redux instance” is now called “The Store”. (#137 (comment)) * The dispatcher is removed completely. (#166 (comment)) * <s>`composeStores`</s> is now `composeReducers`. * <s>`createDispatcher`</s> is gone. * <s>`createRedux`</s> is now `createStore`. * `<Provider>` now accepts `store` prop instead of <s>`redux`</s>. * The new `createStore` signature is `createStore(reducer: Function | Object, initialState: any, middlewares: Array | ({ getState, dispatch }) => Array)`. * If the first argument to `createStore` is an object, `composeReducers` is automatically applied to it. * The “smart” middleware signature changed. It now accepts an object instead of a single `getState` function. The `dispatch` function lets you “recurse” the middleware chain and is useful for async: #113 (comment). * The `dispatch` provided by the default thunk middleware now walks the whole middleware chain. * It is enforced now that Actions have to be plain object. Use middleware for transforming anything else into the actions. * The object in React context is renamed from <s>`redux`</s> to `store`. * Some tests are rewritten for clarity, focus and edge cases. * Redux in examples is now aliased to the source code for easier work on master.
- Loading branch information
Showing
44 changed files
with
791 additions
and
475 deletions.
There are no files selected for viewing
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
File renamed without changes.
File renamed without changes.
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
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
File renamed without changes.
File renamed without changes.
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 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import invariant from 'invariant'; | ||
import isPlainObject from './utils/isPlainObject'; | ||
|
||
export default class Store { | ||
constructor(reducer, initialState) { | ||
invariant( | ||
typeof reducer === 'function', | ||
'Expected the reducer to be a function.' | ||
); | ||
|
||
this.state = initialState; | ||
this.listeners = []; | ||
this.replaceReducer(reducer); | ||
} | ||
|
||
getReducer() { | ||
return this.reducer; | ||
} | ||
|
||
replaceReducer(nextReducer) { | ||
this.reducer = nextReducer; | ||
this.dispatch({ type: '@@INIT' }); | ||
} | ||
|
||
dispatch(action) { | ||
invariant( | ||
isPlainObject(action), | ||
'Actions must be plain objects. Use custom middleware for async actions.' | ||
); | ||
|
||
const { reducer } = this; | ||
this.state = reducer(this.state, action); | ||
this.listeners.forEach(listener => listener()); | ||
return action; | ||
} | ||
|
||
getState() { | ||
return this.state; | ||
} | ||
|
||
subscribe(listener) { | ||
const { listeners } = this; | ||
listeners.push(listener); | ||
|
||
return function unsubscribe() { | ||
const index = listeners.indexOf(listener); | ||
listeners.splice(index, 1); | ||
}; | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Store from './Store'; | ||
import composeReducers from './utils/composeReducers'; | ||
import composeMiddleware from './utils/composeMiddleware'; | ||
import thunkMiddleware from './middleware/thunk'; | ||
|
||
const defaultMiddlewares = ({ dispatch, getState }) => [ | ||
thunkMiddleware({ dispatch, getState }) | ||
]; | ||
|
||
export default function createStore( | ||
reducer, | ||
initialState, | ||
middlewares = defaultMiddlewares | ||
) { | ||
const finalReducer = typeof reducer === 'function' ? | ||
reducer : | ||
composeReducers(reducer); | ||
|
||
const store = new Store(finalReducer, initialState); | ||
const getState = ::store.getState; | ||
|
||
const rawDispatch = ::store.dispatch; | ||
let cookedDispatch = null; | ||
|
||
function dispatch(action) { | ||
return cookedDispatch(action); | ||
} | ||
|
||
const finalMiddlewares = typeof middlewares === 'function' ? | ||
middlewares({ dispatch, getState }) : | ||
middlewares; | ||
|
||
cookedDispatch = composeMiddleware( | ||
...finalMiddlewares, | ||
rawDispatch | ||
); | ||
|
||
return { | ||
dispatch: cookedDispatch, | ||
subscribe: ::store.subscribe, | ||
getState: ::store.getState, | ||
getReducer: ::store.getReducer, | ||
replaceReducer: ::store.replaceReducer | ||
}; | ||
} |
Oops, something went wrong.