Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: action order with synchronous effects #26

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true
"bracketSpacing": true,
"arrowParens": "avoid"
}
9 changes: 6 additions & 3 deletions packages/core/src/creators/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { scan, mergeMap, shareReplay } from 'rxjs/operators';
import { BehaviorSubject, Subject } from 'rxjs';
import { scan, mergeMap, shareReplay, observeOn } from 'rxjs/operators';
import { asyncScheduler, BehaviorSubject, Subject } from 'rxjs';

import { createAction } from './createAction';
import { Action, Reducer, Store, Effect, witness } from '../types';
Expand All @@ -21,7 +21,10 @@ export function createStore<A extends Action, S>(
);

effect$
.pipe(mergeMap(effect => effect(action$, state$)))
.pipe(
mergeMap(effect => effect(action$, state$)),
observeOn(asyncScheduler),
)
.subscribe(action => action$.next(action));

if (rootEffect) {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/tests/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const awaitNMockCalls = (fn: jest.Mock, numberOfCalls: number): Promise<void> =>
new Promise(resolve => {
const intervalId = setInterval(() => {
if (fn.mock.calls.length < numberOfCalls) return;

clearInterval(intervalId);
resolve();
}, 50);
});
28 changes: 26 additions & 2 deletions packages/core/src/tests/createStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TestScheduler } from 'rxjs/testing';
import { createStore } from '../creators/createStore';
import { Reducer, Effect, Action } from '../types';
import { ofType } from '../operators';
import { awaitNMockCalls } from './_utils';

/* eslint-disable @typescript-eslint/no-unused-vars */

Expand Down Expand Up @@ -109,7 +110,7 @@ describe('createStore', () => {
expect(effectMock).toHaveBeenCalledWith(action$, state$);
});

it('should send actions back to the action$ stream', () => {
it('should send actions back to the action$ stream', async () => {
const someOtherAction = { type: 'Other' };
const someEffect: Effect<Action, {}> = action$ =>
action$.pipe(ofType(someOtherAction.type), mapTo(someAction));
Expand All @@ -120,8 +121,31 @@ describe('createStore', () => {
store.state$.subscribe();
store.action$.next(someOtherAction);

await awaitNMockCalls(reducerMock, 4);

expect(reducerMock).toBeCalledTimes(4);
expect(reducerMock).toHaveBeenLastCalledWith(initialState, someOtherAction);
expect(reducerMock).toHaveBeenLastCalledWith(initialState, someAction);
});

it('should send actions back to the action$ stream respecting dispatch order', async () => {
const aAction = { type: 'A' };
const bAction = { type: 'B' };
const someEffect: Effect<Action, {}> = action$ =>
action$.pipe(ofType(aAction.type), mapTo(bAction));

const reducerMock = jest.fn((state = initialState, _action) => state);

const store = createStore(reducerMock, someEffect);

store.state$.subscribe();
store.action$.next(aAction);

await awaitNMockCalls(reducerMock, 4);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I wanted to avoid, asyncScheduler makes everything async which might be not desirable if you are operating on sync effects. I was also experimenting with queueScheduler but I couldn't get it working properly.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the majority of cases, It probably won't matter though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, had the same reflection... But it is definitely the simplest fix 🤷


expect(reducerMock).toHaveBeenNthCalledWith(1, undefined, initStateAction);
expect(reducerMock).toHaveBeenNthCalledWith(2, initialState, initAction);
expect(reducerMock).toHaveBeenNthCalledWith(3, initialState, aAction);
expect(reducerMock).toHaveBeenNthCalledWith(4, initialState, bAction);
});
});
});