Skip to content

Commit 7418203

Browse files
committed
Merge pull request reduxjs#300 from gaearon/jsdoc
Add JSDoc annotations to the public API
2 parents 27752e6 + 259c6bf commit 7418203

File tree

10 files changed

+147
-13
lines changed

10 files changed

+147
-13
lines changed

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"node": true
77
},
88
"rules": {
9+
"valid-jsdoc": 2,
10+
911
"react/jsx-uses-react": 2,
1012
"react/jsx-uses-vars": 2,
1113
"react/react-in-jsx-scope": 2,

src/createStore.js

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
import invariant from 'invariant';
22
import isPlainObject from './utils/isPlainObject';
33

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+
*/
710
export var ActionTypes = {
811
INIT: '@@redux/INIT'
912
};
1013

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+
*/
1132
export default function createStore(reducer, initialState) {
1233
invariant(
1334
typeof reducer === 'function',
@@ -18,10 +39,23 @@ export default function createStore(reducer, initialState) {
1839
var currentState = initialState;
1940
var listeners = [];
2041

42+
/**
43+
* Reads the state tree managed by the store.
44+
*
45+
* @returns {any} The current state tree of your application.
46+
*/
2147
function getState() {
2248
return currentState;
2349
}
2450

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+
*/
2559
function subscribe(listener) {
2660
listeners.push(listener);
2761

@@ -31,6 +65,28 @@ export default function createStore(reducer, initialState) {
3165
};
3266
}
3367

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+
*/
3490
function dispatch(action) {
3591
invariant(
3692
isPlainObject(action),
@@ -42,10 +98,28 @@ export default function createStore(reducer, initialState) {
4298
return action;
4399
}
44100

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+
*/
45109
function getReducer() {
46110
return currentReducer;
47111
}
48112

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+
*/
49123
function replaceReducer(nextReducer) {
50124
currentReducer = nextReducer;
51125
dispatch({ type: ActionTypes.INIT });

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import createStore from './createStore';
2-
import compose from './utils/compose';
32
import combineReducers from './utils/combineReducers';
43
import bindActionCreators from './utils/bindActionCreators';
54
import applyMiddleware from './utils/applyMiddleware';
5+
import compose from './utils/compose';
66

77
export {
88
createStore,
9-
compose,
109
combineReducers,
1110
bindActionCreators,
12-
applyMiddleware
11+
applyMiddleware,
12+
compose
1313
};

src/utils/applyMiddleware.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import compose from './compose';
22

33
/**
4-
* Creates a higher-order store that applies middleware to a store's dispatch.
4+
* Creates a higher-order store that applies middleware to the dispatch method
5+
* of the Redux store. This is handy for a variety of tasks, such as expressing
6+
* asynchronous actions in a concise manner, or logging every action payload.
7+
*
8+
* See `redux-thunk` package as an example of the Redux middleware.
9+
*
510
* Because middleware is potentially asynchronous, this should be the first
611
* higher-order store in the composition chain.
7-
* @param {...Function} ...middlewares
8-
* @return {Function} A higher-order store
12+
*
13+
* @param {...Function} middlewares The middleware chain to be applied.
14+
* @returns {Function} A higher-order store.
915
*/
1016
export default function applyMiddleware(...middlewares) {
1117
return (next) => (reducer, initialState) => {

src/utils/bindActionCreators.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import mapValues from '../utils/mapValues';
22

3+
/**
4+
* Turns an object whose values are action creators, into an object with the
5+
* same keys, but with every function wrapped into a `dispatch` call so they
6+
* may be invoked directly. This is just a convenience method, as you can call
7+
* `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
8+
*
9+
* @param {Object} actionCreators An object whose values are action creator
10+
* functions. One handy way to obtain it is to use ES6 `import * as` syntax.
11+
*
12+
* @param {Function} dispatch The `dispatch` function available on your Redux
13+
* store.
14+
*
15+
* @returns {Object} The object mimicking the original object, but with every
16+
* action creator wrapped into the `dispatch` call.
17+
*/
318
export default function bindActionCreators(actionCreators, dispatch) {
419
return mapValues(actionCreators, actionCreator =>
520
(...args) => dispatch(actionCreator(...args))

src/utils/combineReducers.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ function getErrorMessage(key, action) {
1313
);
1414
}
1515

16+
/**
17+
* Turns an object whose values are different reducer functions, into a single
18+
* reducer function. It will call every child reducer, and gather their results
19+
* into a single state object, whose keys correspond to the keys of the passed
20+
* reducer functions.
21+
*
22+
* @param {Object} reducers An object whose values correspond to different
23+
* reducer functions that need to be combined into one. One handy way to obtain
24+
* it is to use ES6 `import * as reducers` syntax. The reducers may never return
25+
* undefined for any action. Instead, they should return their initial state
26+
* if the state passed to them was undefined, and the current state for any
27+
* unrecognized action.
28+
*
29+
* @returns {Function} A reducer function that invokes every reducer inside the
30+
* passed object, and builds a state object with the same shape.
31+
*/
1632
export default function combineReducers(reducers) {
1733
var finalReducers = pick(reducers, (val) => typeof val === 'function');
1834

@@ -38,7 +54,7 @@ export default function combineReducers(reducers) {
3854
);
3955
});
4056

41-
return function composition(state = {}, action) {
57+
return function combination(state = {}, action) {
4258
return mapValues(finalReducers, (reducer, key) => {
4359
var newState = reducer(state[key], action);
4460
invariant(

src/utils/compose.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/**
2-
* Composes functions from left to right
3-
* @param {...Function} funcs - Functions to compose
4-
* @return {Function}
2+
* Composes functions from left to right.
3+
*
4+
* @param {...Function} funcs - The functions to compose.
5+
* @returns {Function} A function that passes its only argument to the first of
6+
* the `funcs`, then pipes its return value to the second one, and so on, until
7+
* the last of the `funcs` is called, and its result is returned.
58
*/
69
export default function compose(...funcs) {
710
return funcs.reduceRight((composed, f) => f(composed));

src/utils/isPlainObject.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @param {any} obj The object to inspect.
3+
* @returns {boolean} True if the argument appears to be a plain object.
4+
*/
15
export default function isPlainObject(obj) {
26
if (!obj) {
37
return false;

src/utils/mapValues.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Applies a function to every key-value pair inside an object.
3+
*
4+
* @param {Object} obj The source object.
5+
* @param {Function} fn The mapper function taht receives the value and the key.
6+
* @returns {Object} A new object that contains the mapped values for the keys.
7+
*/
18
export default function mapValues(obj, fn) {
29
return Object.keys(obj).reduce((result, key) => {
310
result[key] = fn(obj[key], key);

src/utils/pick.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Picks key-value pairs from an object where values satisfy a predicate.
3+
*
4+
* @param {Object} obj The object to pick from.
5+
* @param {Function} fn The predicate the values must satisfy to be copied.
6+
* @returns {Object} The object with the values that satisfied the predicate.
7+
*/
18
export default function pick(obj, fn) {
29
return Object.keys(obj).reduce((result, key) => {
310
if (fn(obj[key])) {

0 commit comments

Comments
 (0)