@@ -19,10 +19,10 @@ const createState = state => Map({
19
19
current : state
20
20
} ) ;
21
21
22
- const applyCommit = ( state , commitId , reducer ) => {
22
+ const applyCommit = ( state , targetActionIndex , reducer ) => {
23
23
const history = state . get ( 'history' ) ;
24
24
// If the action to commit is the first in the queue (most common scenario)
25
- if ( history . first ( ) . meta . optimistic . id === commitId ) {
25
+ if ( targetActionIndex === 0 ) {
26
26
const historyWithoutCommit = history . shift ( ) ;
27
27
const nextOptimisticIndex = historyWithoutCommit . findIndex ( action => action . meta && action . meta . optimistic && ! action . meta . optimistic . isNotOptimistic && action . meta . optimistic . id ) ;
28
28
// If this is the only optimistic item in the queue, we're done!
@@ -46,22 +46,22 @@ const applyCommit = (state, commitId, reducer) => {
46
46
} ) ;
47
47
} else {
48
48
// If the committed action isn't the first in the queue, find out where it is
49
- const actionToCommit = history . findEntry ( action => action . meta && action . meta . optimistic && action . meta . optimistic . id === commitId ) ;
49
+ const actionToCommit = history . get ( targetActionIndex ) ;
50
50
// Make it a regular non-optimistic action
51
- const newAction = Object . assign ( { } , actionToCommit [ 1 ] , {
52
- meta : Object . assign ( { } , actionToCommit [ 1 ] . meta ,
51
+ const newAction = Object . assign ( { } , actionToCommit , {
52
+ meta : Object . assign ( { } , actionToCommit . meta ,
53
53
{ optimistic : null } )
54
54
} ) ;
55
- return state . set ( 'history' , state . get ( 'history' ) . set ( actionToCommit [ 0 ] , newAction ) )
55
+ return state . set ( 'history' , state . get ( 'history' ) . set ( targetActionIndex , newAction ) )
56
56
}
57
57
} ;
58
58
59
- const applyRevert = ( state , revertId , reducer ) => {
59
+ const applyRevert = ( state , targetActionIndex , reducer ) => {
60
60
const history = state . get ( 'history' ) ;
61
61
const beforeState = state . get ( 'beforeState' ) ;
62
62
let newHistory ;
63
63
// If the action to revert is the first in the queue (most common scenario)
64
- if ( history . first ( ) . meta . optimistic . id === revertId ) {
64
+ if ( targetActionIndex === 0 ) {
65
65
const historyWithoutRevert = history . shift ( ) ;
66
66
const nextOptimisticIndex = historyWithoutRevert . findIndex ( action => action . meta && action . meta . optimistic && ! action . meta . optimistic . isNotOptimistic && action . meta . optimistic . id ) ;
67
67
// If this is the only optimistic action in the queue, we're done!
@@ -75,8 +75,7 @@ const applyRevert = (state, revertId, reducer) => {
75
75
}
76
76
newHistory = historyWithoutRevert . skip ( nextOptimisticIndex ) ;
77
77
} else {
78
- const indexToRevert = history . findIndex ( action => action . meta && action . meta . optimistic && action . meta . optimistic . id === revertId ) ;
79
- newHistory = history . delete ( indexToRevert ) ;
78
+ newHistory = history . delete ( targetActionIndex ) ;
80
79
}
81
80
const newCurrent = newHistory . reduce ( ( mutState , action ) => {
82
81
return reducer ( mutState , action )
@@ -133,7 +132,7 @@ export const optimistic = (reducer, rawConfig = {}) => {
133
132
} ) ;
134
133
135
134
const applyFunc = type === COMMIT ? applyCommit : applyRevert ;
136
- return applyFunc ( nextState , id , reducer ) ;
135
+ return applyFunc ( nextState , targetActionIndex , reducer ) ;
137
136
}
138
137
// create a beforeState since one doesn't already exist
139
138
if ( type === BEGIN ) {
0 commit comments