1
1
import invariant from 'invariant' ;
2
2
import isPlainObject from './utils/isPlainObject' ;
3
3
4
- // Don't ever try to handle these action types in your code. They are private.
5
- // For any unknown actions, you must return the current state.
6
- // If the current state is undefined, you must return the initial state.
4
+ /**
5
+ * These are private action types reserved by Redux.
6
+ * For any unknown actions, you must return the current state.
7
+ * If the current state is undefined, you must return the initial state.
8
+ * Do not reference these action types directly in your code.
9
+ */
7
10
export var ActionTypes = {
8
11
INIT : '@@redux/INIT'
9
12
} ;
10
13
14
+ /**
15
+ * Creates a Redux store that holds the state tree.
16
+ * The only way to change the data in the store is to call `dispatch()` on it.
17
+ *
18
+ * There should only be a single store in your app. To specify how different
19
+ * parts of the state tree respond to actions, you may combine several reducers
20
+ * into a single reducer function by using `combineReducers`.
21
+ *
22
+ * @param {Function } reducer A function that returns the next state tree, given
23
+ * the current state tree and the action to handle.
24
+ *
25
+ * @param {any } initialState The initial state. You may optionally specify it
26
+ * to hydrate the state from the server in universal apps, or to restore a
27
+ * previously serialized user session.
28
+ *
29
+ * @returns {Store } A Redux store that lets you read the state, dispatch actions
30
+ * and subscribe to changes.
31
+ */
11
32
export default function createStore ( reducer , initialState ) {
12
33
invariant (
13
34
typeof reducer === 'function' ,
@@ -18,10 +39,23 @@ export default function createStore(reducer, initialState) {
18
39
var currentState = initialState ;
19
40
var listeners = [ ] ;
20
41
42
+ /**
43
+ * Reads the state tree managed by the store.
44
+ *
45
+ * @returns {any } The current state tree of your application.
46
+ */
21
47
function getState ( ) {
22
48
return currentState ;
23
49
}
24
50
51
+ /**
52
+ * Adds a change listener. It will be called any time an action is dispatched,
53
+ * and some part of the state tree may potentially have changed. You may then
54
+ * call `getState()` to read the current state tree inside the callback.
55
+ *
56
+ * @param {Function } listener A callback to be invoked on every dispatch.
57
+ * @returns {Function } A function to remove this change listener.
58
+ */
25
59
function subscribe ( listener ) {
26
60
listeners . push ( listener ) ;
27
61
@@ -31,6 +65,28 @@ export default function createStore(reducer, initialState) {
31
65
} ;
32
66
}
33
67
68
+ /**
69
+ * Dispatches an action. It is the only way to trigger a state change.
70
+ *
71
+ * The `reducer` function the store was created with will be called with the
72
+ * current state tree and the and the given `action`. Its return value will
73
+ * be considered the next state of the tree, and the change listeners will be
74
+ * notified.
75
+ *
76
+ * The base implementation only supports plain object actions. If you want to
77
+ * dispatch a promise, an observable, a thunk, or something else, you need to
78
+ * wrap your store creating function into the corresponding middleware. For
79
+ * example, see the documentation for the `redux-thunk` package. Even the
80
+ * middleware will eventually dispatch plain object actions using this method.
81
+ *
82
+ * @param {Object } action A plain object representing “what changed”. It is
83
+ * a good idea to keep actions serializable so you can record and replay user
84
+ * sessions, or use the time travelling Redux developer tools.
85
+ *
86
+ * @returns {Object } For convenience, the same action object you dispatched.
87
+ * Note that, if you use a custom middleware, it may wrap `dispatch()` to
88
+ * return something else (for example, a Promise you can await).
89
+ */
34
90
function dispatch ( action ) {
35
91
invariant (
36
92
isPlainObject ( action ) ,
@@ -42,10 +98,28 @@ export default function createStore(reducer, initialState) {
42
98
return action ;
43
99
}
44
100
101
+ /**
102
+ * Returns the reducer currently used by the store to calculate the state.
103
+ *
104
+ * It is likely that you will only need this function if you implement a hot
105
+ * reloading mechanism for Redux.
106
+ *
107
+ * @returns {Function } The reducer used by the current store.
108
+ */
45
109
function getReducer ( ) {
46
110
return currentReducer ;
47
111
}
48
112
113
+ /**
114
+ * Replaces the reducer currently used by the store to calculate the state.
115
+ *
116
+ * You might need this if your app implements code splitting and you want to
117
+ * load some of the reducers dynamically. You might also need this if you
118
+ * implement a hot reloading mechanism for Redux.
119
+ *
120
+ * @param {Function } nextReducer The reducer for the store to use instead.
121
+ * @returns {void }
122
+ */
49
123
function replaceReducer ( nextReducer ) {
50
124
currentReducer = nextReducer ;
51
125
dispatch ( { type : ActionTypes . INIT } ) ;
0 commit comments