Skip to content

Commit 03eb886

Browse files
author
Péter Hauszknecht
committed
add api reference
1 parent 27f82f2 commit 03eb886

4 files changed

Lines changed: 210 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ store.dispatch(increment(42));
2424
## Installation
2525

2626
```
27-
npm install repatch
27+
npm install --save repatch
2828
```
2929

30+
## [API Reference](https://github.com/jaystack/repatch/blob/master/docs/index.md)
31+
3032
## Examples
3133

3234
- [JavaScript example](https://github.com/jaystack/repatch-example-electron-app)

docs/index.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Repatch API Reference
2+
3+
## Top-Level Exports
4+
5+
- Default export: [Store](store.md)
6+
- [thunk](thunk.md)
7+
8+
## Importing
9+
10+
### ES6
11+
12+
```javascript
13+
import Store, { thunk } from 'repatch'
14+
```
15+
16+
### ES5
17+
18+
```javascript
19+
var Store = require('repatch').default
20+
var thunk = require('repatch').thunk
21+
```

docs/store.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Store
2+
3+
The `Store` holds the whole state tree of your application. You need to instantiate it.
4+
5+
## Constructor
6+
7+
#### Arguments
8+
9+
1) **initialState** (*any*): The initial state of your application. Usually it is an object that represents the state tree.
10+
11+
#### Returns
12+
13+
(*Store*): The new `Store` instance.
14+
15+
#### Example
16+
17+
```javascript
18+
import Store from 'repatch'
19+
20+
const store = new Store({ counter: 0 })
21+
```
22+
23+
## Methods
24+
25+
### `getState()`
26+
27+
This method returns the current state of the store.
28+
29+
#### Returns
30+
31+
(*any*): The current state of the store.
32+
33+
#### Example
34+
35+
```javascript
36+
const store = new Store({ counter: 0 })
37+
38+
const state = store.getState()
39+
40+
console.log(state.counter === 0) // true
41+
```
42+
43+
### `dispatch(reducer)`
44+
45+
Dispatches a reducer.
46+
47+
#### Arguments
48+
49+
1) **reducer** (*ReducerFunction: State -> State*): That reducer will reduce the state of the store. This takes the current state and returns the next state. The reducer will be run synchronously after applying the middlewares - if they are given.
50+
51+
#### Returns
52+
53+
(*ReducerFunction*): The final reducer that is made by applying the middlewares - if they are given. If the store does not have middlewares, the `dispatch` method returns the same reducer that was taken as argument.
54+
55+
#### Example
56+
57+
```javascript
58+
const store = new Store({ counter: 0 })
59+
60+
const increment = state => ({ counter: state.counter + 1 })
61+
62+
const result = store.dispatch(increment)
63+
64+
console.log(result === increment) // true
65+
```
66+
67+
#### Notes
68+
69+
1) Middlewares can modify the given reducer, so that is not guaranteed that the `dispatch` returns the original reducer that was taken as argument.
70+
2) The `dispatch` only runs the reducer, when the final reducer (after applying middlewares) is still a function. If the final reducer is a function, then the dispatch modifies the state by its returned value. This behaviour is strongly used by the [thunk](thunk.md) middleware, that returns the returned value of the delegate.
71+
72+
### `subscribe(listener)`
73+
74+
Adds a state change listener.
75+
76+
#### Arguments
77+
78+
1) **listener** (*ListenerFunction: void -> void*): The listener that will be synchronously run after the state was modified by dispatching a reducer.
79+
80+
#### Returns
81+
82+
(*UnsubscribeFunction: void -> void*): The unsubscribe function, that you can use to unsubscribe the given listener.
83+
84+
#### Example
85+
86+
```javascript
87+
const store = new Store({ counter: 0 })
88+
89+
const increment = state => ({ counter: state.counter + 1 })
90+
91+
const unsubscribe = store.subscribe(() => console.log(store.getState()))
92+
93+
store.dispatch(increment) // listener logs the new state
94+
95+
unsubscribe()
96+
97+
store.dispatch(increment) // listener won't be fired
98+
```
99+
100+
### `addMiddleware(...middlewares)`
101+
102+
Adds middleware(s) to the store.
103+
104+
Middlewares will be run at dispatching before the store applies the new state of the reducer. The added middlewares are composed by order of addition.
105+
106+
#### Arguments
107+
108+
1) **...middlewares** (*MiddlewareFunction: (Store, Reducer) -> Reducer*): Middlewares as variadic arguments. Middleware functions take the `Store` instance and the dispatched reducer and return a new reducer.
109+
110+
#### Returns
111+
112+
(*Store: this*): Returns the same `Store` instance for chaining.
113+
114+
#### Example
115+
116+
```javascript
117+
const store = new Store({ counter: 0 })
118+
119+
const logger = (store, reducer) => {
120+
const nextState = reducer(store.getState())
121+
console.log(nextState)
122+
return _ => nextState
123+
}
124+
125+
store.addMiddleware(logger)
126+
127+
store.dispatch(state => ({ counter: state.counter + 1 }))
128+
// logger logs { counter: 1 }
129+
```
130+
131+
## Static members
132+
133+
### `thunk`
134+
135+
The [thunk](thunk.md) middleware.

docs/thunk.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Thunk middleware
2+
3+
Thunk is a repatch middleware to handle async actions. It is very similar to [redux-thunk](https://www.npmjs.com/package/redux-thunk). Thunk middleware allows you to create reducers that returns a function (delegate). The delegate will be fired at dispatching.
4+
5+
## ThunkMiddleware
6+
7+
`State -> Delegate`
8+
9+
#### Example
10+
11+
```javascript
12+
import Store, { thunk } from 'repatch'
13+
14+
const store = new Store({ items: [] }).addMiddleware(thunk)
15+
16+
store.dispatch(_ => async (dispatch, getState) => {
17+
const items = await fetch('/items')
18+
dispatch(state => ({ ...state, items }))
19+
})
20+
```
21+
22+
## Extra arguments
23+
24+
#### Example
25+
26+
```javascript
27+
import Store, { thunk } from 'repatch'
28+
import api from './api'
29+
30+
const store = new Store({ items: [] })
31+
.addMiddleware(thunk.withExtraArgument(api))
32+
33+
store.dispatch(_ => async (dispatch, getState, api) => {
34+
const items = await api.get('/items')
35+
dispatch(state => ({ ...state, items }))
36+
})
37+
```
38+
39+
## Delegate
40+
41+
#### Arguments
42+
43+
1) **dispatch** (*Store.dispatch*): The `dispatch` method of the `Store` instance. With this you can dispatch any reducer to the store.
44+
45+
2) **getState** (*Store.getState*): The `getState` method of the `Store` instance. With this you can get the current state of the store.
46+
47+
3) **extraArgument** (*any*): An extra argument that you can provide at adding the thunk middleware. The `extraArgument` could be anything what you want. It is useful to keep the delegates side-effect-less.
48+
49+
#### Returns
50+
51+
(*any*): Delegate can return anything. If your delegate returns a `Promise`, you can chain your async actions to each other.

0 commit comments

Comments
 (0)