Skip to content

Commit aaa032c

Browse files
committed
add async callbacks support
1 parent 8b37332 commit aaa032c

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-controller-middleware",
3-
"version": "2.0.0-rc.1",
3+
"version": "2.0.0-rc.2",
44
"description": "Adjust Redux middleware to be able to use controllers with Dependency Injection to handle actions",
55
"contributors": [
66
"Artem Ignatev <[email protected]>"

lib/src/controller/controllerMiddleware.test.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,26 @@ describe('many actions with callbacks', () => {
312312
nextCalled.push(action.type);
313313
}) as Dispatch;
314314

315-
let mockFn1 = jest.fn();
316-
let mockFn2 = jest.fn();
315+
const mockFn1 = jest.fn(() => {
316+
calledMethods.push('mock fn 1');
317+
});
318+
const mockFn2 = jest.fn(() => {
319+
calledMethods.push('mock fn 2');
320+
});
321+
const mockAsyncFn = jest.fn(async () => {
322+
calledMethods.push('mock async fn is called');
323+
await new Promise<void>((resolve) => {
324+
setTimeout(resolve, 50);
325+
});
326+
calledMethods.push('mock async fn has done');
327+
});
317328

318329
beforeEach(() => {
319330
calledMethods.splice(0, calledMethods.length);
320331
nextCalled.splice(0, nextCalled.length);
321332
mockFn1.mockClear();
322333
mockFn2.mockClear();
334+
mockAsyncFn.mockClear();
323335
});
324336

325337
const handleAction = makeMiddleware(calledMethods, next);
@@ -331,6 +343,7 @@ describe('many actions with callbacks', () => {
331343
//
332344
createAction(ACTIONS.actionA2),
333345
mockFn1,
346+
mockAsyncFn,
334347
() => createAction(ACTIONS.actionA3),
335348
mockFn2
336349
);
@@ -347,19 +360,14 @@ describe('many actions with callbacks', () => {
347360
test('if order of called methods is correct', async () => {
348361
const action = prepareAction();
349362
await handleAction(action);
350-
expect(calledMethods).toStrictEqual(['A1', 'A2', 'A3']);
351-
});
352-
353-
describe('if callbacks were called', () => {
354-
test('if first callback was called one time', async () => {
355-
const action = prepareAction();
356-
await handleAction(action);
357-
expect(mockFn1).toBeCalledTimes(1);
358-
});
359-
test('if second callback was called one time', async () => {
360-
const action = prepareAction();
361-
await handleAction(action);
362-
expect(mockFn2).toBeCalledTimes(1);
363-
});
363+
expect(calledMethods).toStrictEqual([
364+
'A1',
365+
'A2',
366+
'mock fn 1',
367+
'mock async fn is called',
368+
'mock async fn has done',
369+
'A3',
370+
'mock fn 2',
371+
]);
364372
});
365373
});

lib/src/controller/controllerMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async function handleAction<State>(
8282
if (typeof nextActionOrFactory === 'function') {
8383
try {
8484
// callback or action factory
85-
nextAction = nextActionOrFactory();
85+
nextAction = await nextActionOrFactory();
8686
} catch (error) {
8787
console.error('Unhandled exception in callback or action factory', error);
8888
// if there is something went wrong, we cannot proceed as normal,

lib/src/types/Action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export interface Action<Payload = undefined> extends AnyAction {
3333
handled?: () => void;
3434
}
3535

36-
export type ActionFactory = () => Action<any> | void;
36+
export type ActionFactory = () => Action<any> | void | Promise<void>;

0 commit comments

Comments
 (0)