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

Commitcf34796

Browse files
author
Max Heiber
authored
Merge pull request#14 from mheiber/third-argument
Third argument
2 parents1a4e9a8 +8e4d5af commitcf34796

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ const fetchUsersReducer = (state, action) => {
9898

9999
The (marginal) advantages of using redux-machine over just using the FSM pattern is that you can more clearly express intent and write slightly less code.
100100

101+
##Supporting an Extra Argument
102+
103+
redux-machine supports to passing an extra argument to state reducers, for cases where a state reducer requires[a third argument for other state it depends on](https://github.com/reactjs/redux/blob/master/docs/faq/Reducers.md#how-do-i-share-state-between-two-reducers-do-i-have-to-use-combinereducers).
104+
101105
##Asynchronous Effects
102106

103107
redux-machine 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),[redux-funk](https://github.com/mheiber/redux-funk) or[anything else](https://github.com/markerikson/redux-ecosystem-links/blob/master/side-effects.md).

‎index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use strict"
22

33
varcreateMachine=function(reducersObject){
4-
returnfunction(state,action){
4+
returnfunction(state,action,extraArgument){
55
varstatus=(state&&state.status) ?state.status :'INIT'
66
varreducer=reducersObject[status]
77
if(!reducer){
88
thrownewError('reducersObject missing reducer for status '+status)
99
}
10-
constnextState=reducer(state,action)
10+
constnextState=reducer(state,action,extraArgument)
1111
if(nextState===state){
1212
returnstate
1313
}

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"redux-machine",
3-
"version":"1.1.1",
3+
"version":"1.2.0",
44
"description":"a tiny lib for creating state machines as swappable Redux reducers",
55
"main":"index.js",
66
"scripts": {

‎test.js‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ test('should transition between states', t => {
6969
},'Should set initial status to "INIT"')
7070

7171
action('DUMMY AGAIN')
72-
7372
t.equals(state,prevState,'Should not change the state when an action is unhandled')
7473

7574
action('FETCH_USERS_RESPONSE',{users})
@@ -106,6 +105,62 @@ test('should transition between states', t => {
106105
t.end()
107106
})
108107

108+
test('should be able to pass an extraArgument to inner reducers',t=>{
109+
constconfigReducer=(state={},action)=>state;
110+
constgameReducer=(state={},action,isMuted)=>{
111+
switch(action.type){
112+
case'START':
113+
constaudio=(isMuted===true) ?'OFF' :'ON'
114+
returnObject.assign({},state,{
115+
audio:audio
116+
})
117+
default:
118+
returnstate
119+
}
120+
}
121+
constinnerReducer=(state={},action)=>{
122+
constisMuted=state.config ?state.config.isMuted :undefined
123+
return{
124+
'game':gameReducer(state.game,action,isMuted),
125+
'config':configReducer(state.config,action)
126+
}
127+
}
128+
constfsmReducer=createMachine({
129+
'INIT':innerReducer
130+
});
131+
conststore=createStore(fsmReducer,undefined)
132+
store.dispatch({
133+
type:'START'
134+
})
135+
t.deepEquals(store.getState(),{
136+
status:'INIT',
137+
game:{
138+
audio:'ON'
139+
},
140+
config:{}
141+
})
142+
143+
constsilentStore=createStore(fsmReducer,{
144+
config:{
145+
isMuted:true
146+
}
147+
})
148+
silentStore.dispatch({
149+
type:'START'
150+
})
151+
t.deepEquals(silentStore.getState(),{
152+
status:'INIT',
153+
game:{
154+
audio:'OFF'
155+
},
156+
config:{
157+
isMuted:true
158+
}
159+
})
160+
161+
t.end()
162+
})
163+
109164
test('should error on status not found',t=>{
110165
letstore={status:'STATUS_NOT_IN_CREATE_MACHINE'}
111166
constreducer=createMachine({})
@@ -124,3 +179,4 @@ test('should error on status not found', t => {
124179

125180
t.end()
126181
})
182+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp