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

Commit3cba3b6

Browse files
committed
Simplify API, make state machine stateless
Before, the initial state was **ALWAYS** init,which means that the reducer returned by `createMachine`was not pure, violating the reducer contract.Fixed that, and simplified the API. No point in havingeffectively two ways to transition state (`become` symbol and`status` key).
1 parent8241434 commit3cba3b6

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

‎index.js‎

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

3-
varbecome=Symbol('become')
4-
53
varcreateMachine=function(reducersObject){
6-
varstatus='INIT'
7-
varcurrentReducer=reducersObject['INIT']
8-
if(!currentReducer){
9-
thrownewError('reducersObject must have INIT reducer')
10-
}
114
returnfunction(state,action){
12-
varnextState=currentReducer(state,action)
13-
if(nextState[become]){
14-
status=nextState[become]
15-
currentReducer=reducersObject[status]
5+
constnextState=state ?Object.assign({},state) :{}
6+
nextState.status=nextState.status||'INIT'
7+
varcurrentReducer=reducersObject[nextState.status]
8+
if(!currentReducer){
9+
thrownewError('reducersObject missing reducer for status '+nextState.status)
1610
}
17-
varnextStateWithStatus=Object.assign({},state,nextState,{status:status})
18-
returnnextStateWithStatus
11+
returncurrentReducer(nextState,action)
1912
}
2013
}
2114

22-
module.exports={createMachine:createMachine,become:become}
15+
module.exports={createMachine:createMachine}

‎test.js‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
"use strict"
22

3-
const{ createMachine, become}=require('./index.js')
3+
const{ createMachine}=require('./index.js')
44
constassert=require('assert')
55

66
// BEGIN FIXTURES
77

88
constusers=['userFoo','userBar','userBaz']
99

10-
constinitReducer=(state={error:null,users:[]},action)=>{
11-
10+
constinitReducer=(state={},action)=>{
1211
switch(action.type){
1312
case'FETCH_USERS':
1413
returnObject.assign({},state,{
1514
error:null,
16-
[become]:'IN_PROGRESS'
15+
status:'IN_PROGRESS'
1716
})
1817
default:
1918
returnstate
@@ -27,12 +26,12 @@ const inProgressReducer = (state = {}, action) => {
2726
returnObject.assign({},state,{
2827
error:null,
2928
users:action.payload.users,
30-
[become]:'INIT'
29+
status:'INIT'
3130
})
3231
case'FETCH_USERS_FAIL':
3332
returnObject.assign({},state,{
3433
error:action.payload,
35-
[become]:'INIT'
34+
status:'INIT'
3635
})
3736
default:
3837
returnstate
@@ -65,9 +64,7 @@ const expect = (expected, maybeMessage) => assert.deepEqual(state, expected, may
6564

6665
action('DUMMY')
6766
expect({
68-
error:null,
6967
status:'INIT',
70-
users:[]
7168
},'Should set initial status to "INIT"')
7269

7370
action('FETCH_USERS_RESPONSE',{users})
@@ -76,22 +73,19 @@ expect(prevState, 'Should ignore messages when not handled by current status')
7673
action('FETCH_USERS')
7774
expect({
7875
error:null,
79-
status:'IN_PROGRESS',
80-
users:[]
76+
status:'IN_PROGRESS'
8177
})
8278

8379
action('FETCH_USERS_FAIL','timeout')
8480
expect({
8581
error:'timeout',
86-
status:'INIT',
87-
users:[]
82+
status:'INIT'
8883
})
8984

9085
action('FETCH_USERS')
9186
expect({
9287
error:null,
93-
status:'IN_PROGRESS',
94-
users:[]
88+
status:'IN_PROGRESS'
9589
})
9690

9791
action('FETCH_USERS')
@@ -105,9 +99,14 @@ expect({
10599
})
106100

107101
assert.throws(
108-
()=>createMachine({}),
109-
err=>err.message==='reducersObject must have INIT reducer',
110-
'should error when reducersObject missing "INIT"'
102+
()=>{
103+
letstore={status:'STATUS_NOT_IN_CREATE_MACHINE'}
104+
constreducer=createMachine({})
105+
store=reducer(store,{type:'DUMMY'})
106+
107+
},
108+
err=>err.message==='reducersObject missing reducer for status STATUS_NOT_IN_CREATE_MACHINE',
109+
'should error when status not found'
111110
)
112111

113112
console.log('success')

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp