Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8da2d13

Browse files
committed
update API
1 parent3cba3b6 commit8da2d13

File tree

1 file changed

+23
-89
lines changed

1 file changed

+23
-89
lines changed

‎README.md‎

Lines changed: 23 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,153 +2,87 @@
22

33
![redux-machine](http://i63.tinypic.com/2igdbus_th.jpg)
44

5-
*A tiny library for creating state machines in Redux apps.*
6-
5+
*A tiny library (12 lines) for creating state machines as swappable Redux reducers*
76

87
redux-machine enables you to create[reducers](http://redux.js.org/docs/basics/Reducers.html) that can transition between different "statuses." These are likes states in a[finite state machine](https://en.wikipedia.org/wiki/Finite-state_machine). The goal is for redux-machine to support complex workflows simply while keeping all state in the redux store. Keeping all state in the store is good because:
98

109
- redux-machine works with time-travel debugging. Time-travel debugging was the main[motivation for building redux itself](https://www.youtube.com/watch?v=xsSnOQynTHs).
1110
- Debugging is easy because information is in one place (the store).
1211
- Statuses such are queryable by the user interface. This is helpful if you want to show things to the user such as loading spinners to indicate status
1312

14-
redux-saga and redux-observable also make it easy to create workflows in redux apps. They are more beautiful and powerful than redux-machine, but can break the "all state is in the store" paradigm.
15-
1613
##Install
1714

1815
`npm install redux-machine --save`
1916

20-
>redux-machine internally uses Object.assign and Symbol, whichareES2015features. If you need to support older browsers, you can use a polyfill such as[core-js](https://github.com/zloirock/core-js#basic).
17+
>redux-machine internally uses Object.assign, whichis anES2015feature. If you need to support older browsers, you can use a polyfill such as[core-js](https://github.com/zloirock/core-js#basic).
2118
22-
##API
19+
##How to Use
2320

24-
-**`createMachine(reducerObject)`** returns a machine, which is itself a reducer
25-
26-
`reducerObject` is an object where the keys are statuses and the values are sub-reducers. For example, this code creates a reducer that acts like`initReducer` when the status is`INIT` and acts like`inProgressReducer` when the status is`IN_PROGRESS`:
21+
This is the entire API for redux-machine:
2722

2823
```js
24+
// entire API, no middleware required
25+
import {createMachine } =from'./index.js'
26+
2927
constfetchUsersReducer=createMachine({
3028
'INIT': initReducer,
3129
'IN_PROGRESS': inProgressReducer
3230
})
3331
```
3432

35-
`reducerObject` must have an`INIT` key. The value for the`INIT` key is the reducer for the starting status of the machine/reducer.
36-
37-
The reducer returned by`createMachine` adds a`STATUS` key to the store. That is, when the status is`INIT`, the value of`STATUS` is`INIT` and when the status is`IN_PROGRESS`, the value of`STATUS` is`IN_PROGRESS`. This is useful for debugging or for acting on the current status in the UI or in your API callers.
38-
39-
-**`become`** (symbol)
40-
The reducers that are the values of`reducersObject` can use`become` to transition the machine/reducer to a different status. For example, the following code in`initReducer` transitions`fetchUsersReducer` to the`IN_PROGRESS` status when the`` action is dispatched. At that point,`fetchUsersReducer` will act like`inProgressReducer` and the value of`STATUS` changes to`IN_PROGRESS`:
41-
42-
```js
43-
import {become }from'redux-machine'
44-
45-
...
46-
switch (action.type) {
47-
case'FETCH_USERS':
48-
returnObject.assign({}, state, {
49-
error:null,
50-
[become]:'IN_PROGRESS'
51-
})
52-
case default:
53-
return state
54-
}
55-
```
56-
57-
>redux-machine's`become` is influenced by[Akka's`become`](http://doc.akka.io/docs/akka/snapshot/scala/actors.html#become-unbecome).
58-
59-
##Full Example
60-
61-
Suppose you are making an API call to fetch users, and want only one instance of this API call to happen at a time. You also want to communicate to the user the status of the API call:`INIT` (initial status, no call in progress) and`IN_PROGRESS`.
62-
63-
You can use redux-machine to have the following status transitions in your reducer:
64-
![status machine for the api-calling example](http://oi67.tinypic.com/qz57qd.jpg)
65-
- When the status is`INIT` and the action type is`FETCH_USERS`, the machine transitions to`IN_PROGRESS` status.
66-
- When the status is`IN_PROGRESS` and the action type is`FETCH_USERS_RESPONSE` or`FETCH_USERS_FAIL`, the machine transitions to the`INIT` (initial) status.
67-
68-
I'll walk through how to create the machine in this example. If you prefer to see all the code in one place, it's in`./test.js`.
69-
70-
###`import` redux-machine
71-
72-
```js
73-
import {createMachine,become }from'./index.js'
74-
```
75-
76-
###Create Reducers
33+
The reducer returned by`createMachine` will act like`initReducer` when its status is`INIT` and will act like`inProgressReducer` when the status is`IN_PROGRESS`. If the store's`state.status` is undefined, the reducer for`INIT` is used (so it's a good idea to provide a reducer for the`INIT` status).
34+
>>>>>>>update API
7735
78-
Next, define a reducer for eachstatusin the machine ('INIT' and 'IN_PROGRESS'):
36+
`initReducer` and`inProgressReducer` can dostatustransitions by setting`state.status`:
7937

8038
```js
8139
constinitReducer= (state= {error:null, users: []},action)=> {
82-
8340
switch (action.type) {
8441
case'FETCH_USERS':
8542
returnObject.assign({}, state, {
8643
error:null,
87-
[become]:'IN_PROGRESS'
44+
// transition to a different status!
45+
status:'IN_PROGRESS'
8846
})
8947
default:
9048
return state
9149
}
9250
}
9351

9452
constinProgressReducer= (state= {},action)=> {
95-
9653
switch (action.type) {
9754
case'FETCH_USERS_RESPONSE':
9855
returnObject.assign({}, state, {
9956
error:null,
10057
users:action.payload.users,
101-
[become]:'INIT'
58+
// transition to a different status!
59+
status:'INIT'
10260
})
10361
case'FETCH_USERS_FAIL':
10462
returnObject.assign({}, state, {
105-
error:'fail',
106-
[become]:'INIT'
63+
error:action.payload.error,
64+
// transition to a different status!
65+
status:'INIT'
10766
})
10867
default:
10968
return state
11069
}
11170
}
11271
```
11372

114-
>The only special part of these reducers is how they use the`become` symbol, which I'll explain in the next section.
115-
116-
###Create the Machine
117-
118-
Here's how you can use the`createMachine` function to create a reducer from the`initReducer` and`inProgressReducer`:
119-
120-
```js
121-
constfetchUsersReducer=createMachine({
122-
'INIT': initReducer,
123-
'IN_PROGRESS': inProgressReducer
124-
})
125-
```
126-
127-
>`fetchUsersReducer` is a normal[reducer](http://redux.js.org/docs/basics/Reducers.html) that you can use directly in your Redux app, with no need for special middleware.
73+
The example above defines the following state machine:
12874

129-
`initReducer` is the value for`INIT`, so`createMachine` knows to start off acting like`initReducer`.
130-
131-
In the object provided to`createMachine`, the names of the keys matter because they are used within reducers for transitioning the machine/reducer between statuses. For example, the following code in`initReducer` says that when the action is of type`FETCH_USERS`, transition the machine to the`IN_PROGRESS` status:
132-
133-
```js
134-
switch (action.type) {
135-
case'FETCH_USERS':
136-
returnObject.assign({}, state, {
137-
error:null,
138-
[become]:'IN_PROGRESS'
139-
})
140-
case default:
141-
return state
142-
}
143-
```
75+
![status machine for the api-calling example](http://oi67.tinypic.com/qz57qd.jpg)
14476

145-
>`become` is a symbol imported from redux-machine, not a string. See the`import` statement in the example above.
77+
In words:
78+
- When the status is`INIT` and the action type is`FETCH_USERS`, the machine transitions to`IN_PROGRESS` status.
79+
- When the status is`IN_PROGRESS` and the action type is`FETCH_USERS_RESPONSE` or`FETCH_USERS_FAIL`, the machine transitions to the`INIT` (initial) status.
14680

14781
##Asynchronous Effects
14882

14983
redux-machine does only one thing: help you model explicit status transitions in reducers. It doesn't prescribe a way of handling asynchronous effects such as API calls. This leaves it open for you to use[no async effects library](http://stackoverflow.com/a/34599594/2482570),[redux-loop](https://github.com/redux-loop/redux-loop),[redux-thunk](https://github.com/gaearon/redux-thunk),[redux-saga](https://github.com/yelouafi/redux-saga), or anything else.
15084

151-
That said, redux-machine fits very naturally withredux-loop, since both enhancehow you can usereducers. Here's how you could use redux-machine with redux-loop:
85+
That said, redux-machine fits very naturally withother tools which enhancethe expressiveness ofreducers, such as redux-loop and redux-side-effect. Here's how you could use redux-machine with redux-loop:
15286

15387
```js
15488
import {loop,Effects }from'redux-loop'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp