-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Breaking API changes for 1.0 #195
Changes from 2 commits
e426039
34d90b9
8c8e429
72aa974
667d8f5
069d641
6a2730d
9a1a0cd
b72f909
b8e7e1e
c6312df
97d90d0
de685de
1791e2a
039c30f
7245b58
e621c79
152c886
406ca62
038cd25
5d5dbcc
4c68696
e854617
0daeec9
51f798c
c46494b
ab347bf
a38e4ad
c80a77a
dc44972
66ec814
f7e92d9
18aceee
0b5207f
60b2119
650f002
6fe0625
5eaa7b6
749b4ab
e3ec501
afd4260
9045a0c
99a9cee
e39afbe
2aa83ff
baede74
b4766b3
3481fdb
2b4d416
0623cf4
eaa20fa
ac8a184
98ab1de
e355ccd
a4b8aff
7ad209a
adbfbdd
55776ad
3d252c3
91acd42
ed3edea
b51338f
9321dea
305f142
100ce3c
af474ba
8bb94c7
27752e6
259c6bf
7418203
941a2e1
be4b589
0c87ead
5734779
0fc5802
d92f08e
3cfdd73
fca60c8
9a1ad4f
3dfbb1f
5e87896
848bb8e
de301ce
1879d00
4358fc4
46276c0
94d0944
b619853
a467c41
fbf06c5
8cbd405
2a08009
c1b8acd
4efcfcb
ae1c2e2
22ca4be
f614bf6
9985ac8
2798abb
199a5bc
573a84b
09b3b1d
6e8127c
dc922d6
79f90bc
06134f7
9db0c57
27fa07c
f0132a0
2ddfd71
d594232
b94f9a8
df01a1f
41e609d
6446dfd
dd2de74
2ea278e
8b5298c
889a7be
4fba03f
30122f6
bf349bb
8b1c075
b9bb28f
ed33ec1
4de6324
f179ba8
01c3cf0
fd7f884
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The listener will probably turn around and call getState right away. Why not pass the state to the listener? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbrans In fact this is how it used to be, but I felt it was redundant because the components would reach into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really think state should be passed to the 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 was deleted.
This file was deleted.
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m curious why a second There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overlooked this :-). No real reason |
||
getReducer: ::store.getReducer, | ||
replaceReducer: ::store.replaceReducer | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
// Core | ||
import createRedux from './createRedux'; | ||
import createDispatcher from './createDispatcher'; | ||
import createStore from './createStore'; | ||
|
||
// Utilities | ||
import composeMiddleware from './utils/composeMiddleware'; | ||
import composeStores from './utils/composeStores'; | ||
import composeReducers from './utils/composeReducers'; | ||
import bindActionCreators from './utils/bindActionCreators'; | ||
|
||
export { | ||
createRedux, | ||
createDispatcher, | ||
createStore, | ||
composeMiddleware, | ||
composeStores, | ||
composeReducers, | ||
bindActionCreators | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point this method is more like a "performAction" than a "dispatch".
The code reads like "perform an action which modifies state. Then notify listeners that state has changed".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree in a way. I'm not sure it's worth changing though. It doesn't seem to cause much confusion (unlike “Stores” naming which did, so we're changing it to “Reducers”).