Skip to content

Commit

Permalink
fix(core): CHECKOUT-3462 Execute thunk actions sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchin committed Nov 14, 2018
1 parent 7ab3169 commit 5224e69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/data-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@ describe('DataStore', () => {
]);
});

it('execute thunk actions sequentially', async () => {
const initialState = { count: 0 };
const store = new DataStore((state = initialState, action) => {
if (action.type === 'UPDATE') {
return { ...state, count: action.payload };
}

return state;
}, initialState);

const thunk = (readableStore: ReadableDataStore<{ count: number }>) => {
const { count } = readableStore.getState();

return Observable.of({ type: 'UPDATE', payload: count + 1 })
.delay(10);
};

await Promise.all([
store.dispatch(thunk),
store.dispatch(thunk),
store.dispatch(thunk),
]);

expect(store.getState()).toEqual({ count: 3 });
});

it('resolves promises sequentially', async () => {
const store = new DataStore((state = {}) => state);
const callback = jest.fn();
Expand Down
3 changes: 2 additions & 1 deletion src/data-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isEqual, merge } from 'lodash';
import { defer } from 'rxjs/observable/defer';
import { from } from 'rxjs/observable/from';
import { of } from 'rxjs/observable/of';
import { _throw } from 'rxjs/observable/throw';
Expand Down Expand Up @@ -202,7 +203,7 @@ export default class DataStore<TState, TAction extends Action = Action, TTransfo
thunkAction: ThunkAction<TDispatchAction, TTransformedState>,
options: DispatchOptions = {}
): Promise<TTransformedState> {
return this._dispatchObservableAction(thunkAction(this), options);
return this._dispatchObservableAction(defer(() => thunkAction(this)), options);
}

private _getDispatcher(queueId: string = 'default'): Dispatcher<TAction> {
Expand Down

0 comments on commit 5224e69

Please sign in to comment.