Skip to content

Commit 66302e8

Browse files
committed
re-use found index instead of searching for action again
1 parent 911c33b commit 66302e8

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const createState = state => Map({
1919
current: state
2020
});
2121

22-
const applyCommit = (state, commitId, reducer) => {
22+
const applyCommit = (state, targetActionIndex, reducer) => {
2323
const history = state.get('history');
2424
// 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) {
2626
const historyWithoutCommit = history.shift();
2727
const nextOptimisticIndex = historyWithoutCommit.findIndex(action => action.meta && action.meta.optimistic && !action.meta.optimistic.isNotOptimistic && action.meta.optimistic.id);
2828
// If this is the only optimistic item in the queue, we're done!
@@ -46,22 +46,22 @@ const applyCommit = (state, commitId, reducer) => {
4646
});
4747
} else {
4848
// 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);
5050
// 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,
5353
{optimistic: null})
5454
});
55-
return state.set('history', state.get('history').set(actionToCommit[0], newAction))
55+
return state.set('history', state.get('history').set(targetActionIndex, newAction))
5656
}
5757
};
5858

59-
const applyRevert = (state, revertId, reducer) => {
59+
const applyRevert = (state, targetActionIndex, reducer) => {
6060
const history = state.get('history');
6161
const beforeState = state.get('beforeState');
6262
let newHistory;
6363
// 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) {
6565
const historyWithoutRevert = history.shift();
6666
const nextOptimisticIndex = historyWithoutRevert.findIndex(action => action.meta && action.meta.optimistic && !action.meta.optimistic.isNotOptimistic && action.meta.optimistic.id);
6767
// If this is the only optimistic action in the queue, we're done!
@@ -75,8 +75,7 @@ const applyRevert = (state, revertId, reducer) => {
7575
}
7676
newHistory = historyWithoutRevert.skip(nextOptimisticIndex);
7777
} 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);
8079
}
8180
const newCurrent = newHistory.reduce((mutState, action) => {
8281
return reducer(mutState, action)
@@ -133,7 +132,7 @@ export const optimistic = (reducer, rawConfig = {}) => {
133132
});
134133

135134
const applyFunc = type === COMMIT ? applyCommit : applyRevert;
136-
return applyFunc(nextState, id, reducer);
135+
return applyFunc(nextState, targetActionIndex, reducer);
137136
}
138137
// create a beforeState since one doesn't already exist
139138
if (type === BEGIN) {

0 commit comments

Comments
 (0)