|
1 | 1 | import { Dispatch } from 'react';
|
2 | 2 | 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'; |
5 | 4 |
|
6 | 5 | export const applyMiddlewares = <T, A>(state: State<T>, dispatch: Dispatch<A>, ...middlewares: Middleware<T, A>[]) => (
|
7 | 6 | action: A
|
8 | 7 | ) => {
|
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); |
11 | 10 | };
|
12 | 11 |
|
13 | 12 | const next = (nextMiddlewares: Middleware<T, A>[]) => (value: A) => {
|
14 | 13 | dispatch(value);
|
| 14 | + call(nextMiddlewares, value); |
| 15 | + }; |
| 16 | + |
| 17 | + const call = (nextMiddlewares: Middleware<T, A>[], value: A) => { |
15 | 18 | nextMiddlewares.forEach((m, i) => {
|
16 |
| - m(state, next(without(i)), value); |
| 19 | + m(state, next(without(nextMiddlewares, i)), value); |
17 | 20 | });
|
18 |
| - }; |
| 21 | + } |
19 | 22 |
|
20 |
| - middlewares.forEach((m, i) => m(state, next(without(i)), action)); |
| 23 | + call(middlewares, action); |
21 | 24 | };
|
0 commit comments