Skip to content

Commit 1ade85f

Browse files
author
Robin Bühler
committed
Enhance middleware utils
1 parent 39660b6 commit 1ade85f

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Within this section additional types are explained.
7676
| Property | Type | Description |
7777
|:----------------|:-----------------------------|:------------------------------------------------------------|
7878
| `message` | `string` | Error message |
79-
| `contraint` | `{ unique: boolean } | { constraint: `[Constraint](#constraint)`}` | Constraint for this rule |
79+
| `contraint` | `{ unique: boolean } \| { constraint: `[Constraint](#constraint)`}` | Constraint for this rule |
8080

8181
#### Constraint
8282
`(value: string) => boolean`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"react"
88
],
99
"homepage": "https://openscript.github.io/react-dsv-import/",
10-
"version": "0.3.2",
10+
"version": "0.3.3",
1111
"main": "dist/index.js",
1212
"module": "dist/es/index.js",
1313
"types": "dist/index.d.ts",

src/middlewares/middleware.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,31 @@ describe('middleware', () => {
5252
expect(middlewareBMock).toHaveBeenNthCalledWith(2, defaultState, expect.any(Function), { type: 'initialCall' });
5353
expect(middlewareCMock).toBeCalledTimes(2);
5454
});
55+
56+
it('should not call a middleware twice', () => {
57+
const dispatchMock = jest.fn();
58+
const middlewareAMock = jest.fn((_state, dispatch) => {
59+
dispatch({ type: 'sequentCall' });
60+
});
61+
const middlewareBMock = jest.fn((_state, dispatch) => {
62+
dispatch({ type: 'sequentCall' });
63+
});
64+
const middlewareCMock = jest.fn((_state, dispatch) => {
65+
dispatch({ type: 'sequentCall' });
66+
});
67+
const enhancedDispatch = applyMiddlewares(
68+
defaultState,
69+
dispatchMock,
70+
middlewareAMock,
71+
middlewareBMock,
72+
middlewareCMock
73+
);
74+
enhancedDispatch({ type: 'initialCall' });
75+
76+
expect(middlewareAMock).toBeCalledTimes(5);
77+
expect(middlewareBMock).toBeCalledTimes(5);
78+
expect(middlewareBMock).toHaveBeenNthCalledWith(1, defaultState, expect.any(Function), { type: 'sequentCall' });
79+
expect(middlewareBMock).toHaveBeenNthCalledWith(2, defaultState, expect.any(Function), { type: 'sequentCall' });
80+
expect(middlewareCMock).toBeCalledTimes(5);
81+
});
5582
});

src/middlewares/middleware.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import { Dispatch } from 'react';
22
import { State } from '../models/state';
3-
4-
type Middleware<T, A> = (state: State<T>, dispatch: Dispatch<A>, action: A) => void;
3+
import { Middleware } from '../models/middleware';
54

65
export const applyMiddlewares = <T, A>(state: State<T>, dispatch: Dispatch<A>, ...middlewares: Middleware<T, A>[]) => (
76
action: A
87
) => {
9-
const without = (i: number) => {
10-
return middlewares.filter((_, filterIndex) => i !== filterIndex);
8+
const without = (nextMiddlewares: Middleware<T, A>[], i: number) => {
9+
return nextMiddlewares.filter((_, filterIndex) => i !== filterIndex);
1110
};
1211

1312
const next = (nextMiddlewares: Middleware<T, A>[]) => (value: A) => {
1413
dispatch(value);
14+
call(nextMiddlewares, value);
15+
};
16+
17+
const call = (nextMiddlewares: Middleware<T, A>[], value: A) => {
1518
nextMiddlewares.forEach((m, i) => {
16-
m(state, next(without(i)), value);
19+
m(state, next(without(nextMiddlewares, i)), value);
1720
});
18-
};
21+
}
1922

20-
middlewares.forEach((m, i) => m(state, next(without(i)), action));
23+
call(middlewares, action);
2124
};

src/models/middleware.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { State } from "./state";
2+
import { Dispatch } from "react";
3+
4+
export type Middleware<T, A> = (state: State<T>, dispatch: Dispatch<A>, action: A) => void;

0 commit comments

Comments
 (0)