generated from iamogbz/node-js-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// github.com/reduxjs/redux/blob/8551ba8/src/applyMiddleware.ts | ||
import { compose } from "./compose"; | ||
|
||
export function applyMiddleware<S, T extends string, P>( | ||
...middlewares: Middleware<S, T, P>[] | ||
): ContextEnhance<S, T, P> { | ||
return function enhance( | ||
context: ContextValue<S, T, P>, | ||
): ContextValue<S, T, P> { | ||
const dispatchStub: ContextDispatch<T, P> = () => { | ||
throw new Error( | ||
"Dispatching while constructing your middleware is not allowed. " + | ||
"Other middleware would not be applied to this dispatch.", | ||
); | ||
}; | ||
|
||
const middlewareAPI: MiddlewareAPI<S, T, P> = { | ||
getState: context.getState, | ||
dispatch: (action, ...args) => dispatchStub(action, ...args), | ||
}; | ||
const chain = middlewares.map((middleware) => | ||
middleware(middlewareAPI), | ||
); | ||
const dispatch = compose<typeof context.dispatch>(...chain)( | ||
context.dispatch, | ||
); | ||
return { | ||
...context, | ||
dispatch, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// github.com/reduxjs/redux/blob/f1fc7ce/src/compose.ts | ||
type Func<T extends unknown[], R> = (...a: T) => R; | ||
|
||
/** | ||
* Composes single-argument functions from right to left. The rightmost | ||
* function can take multiple arguments as it provides the signature for the | ||
* resulting composite function. | ||
* | ||
* @param funcs The functions to compose. | ||
* @returns A function obtained by composing the argument functions from right | ||
* to left. For example, `compose(f, g, h)` is identical to doing | ||
* `(...args) => f(g(h(...args)))`. | ||
*/ | ||
export function compose(): <R>(a: R) => R; | ||
|
||
export function compose<F extends Function>(f: F): F; | ||
|
||
/* two functions */ | ||
export function compose<A, T extends unknown[], R>( | ||
f1: (a: A) => R, | ||
f2: Func<T, A>, | ||
): Func<T, R>; | ||
|
||
/* three functions */ | ||
export function compose<A, B, T extends unknown[], R>( | ||
f1: (b: B) => R, | ||
f2: (a: A) => B, | ||
f3: Func<T, A>, | ||
): Func<T, R>; | ||
|
||
/* four functions */ | ||
export function compose<A, B, C, T extends unknown[], R>( | ||
f1: (c: C) => R, | ||
f2: (b: B) => C, | ||
f3: (a: A) => B, | ||
f4: Func<T, A>, | ||
): Func<T, R>; | ||
|
||
/* rest */ | ||
export function compose<R>( | ||
f1: (a: unknown) => R, | ||
...funcs: Function[] | ||
): (...args: unknown[]) => R; | ||
|
||
export function compose<R>(...funcs: Function[]): (...args: unknown[]) => R; | ||
|
||
export function compose(...funcs: Function[]): Function { | ||
if (funcs.length === 0) { | ||
// infer the argument type so it is usable in inference down the line | ||
return <T>(arg: T): T => arg; | ||
} | ||
|
||
if (funcs.length === 1) { | ||
return funcs[0]; | ||
} | ||
|
||
return funcs.reduce( | ||
(a, b) => ((...args: unknown[]) => a(b(...args))) as typeof a, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type MiddlewareAPI<S = unknown, T extends string = string, P = unknown> = { | ||
dispatch: ContextDispatch<T, P>; | ||
getState: () => S; | ||
}; | ||
|
||
type Middleware<S = unknown, T extends string = string, P = unknown> = ( | ||
api: MiddlewareAPI<S, T, P>, | ||
) => (next: ContextDispatch<T, P>) => ContextDispatch<T, P>; |