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.
feat: add context enhancer and applyMiddleware (#17)
* chore: enhance scaffolding * chore: apply middleware enhancer * chore: test enhanced context * chore: update create context api * chore: docs and test applymiddleware
- Loading branch information
Showing
13 changed files
with
298 additions
and
65 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
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,14 @@ | ||
import * as React from "react"; | ||
|
||
export function useGetter<R>(value: R): () => R { | ||
const ref = React.useRef(value); | ||
React.useEffect(() => { | ||
ref.current = value; | ||
}, [value]); | ||
return React.useCallback( | ||
function getValue() { | ||
return ref.current; | ||
}, | ||
[ref], | ||
); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
export { Provider } from "./components/Provider"; | ||
export { applyMiddleware } from "./utils/applyMiddleware"; | ||
export { createAction } from "./createAction"; | ||
export { createContext } from "./createContext"; | ||
export { createDuck } from "./createDuck"; | ||
export { createReducer } from "./createReducer"; | ||
export { createRootDuck } from "./createRootDuck"; | ||
export { createRootProvider } from "./createRootProvider"; | ||
export { Provider } from "./components/Provider"; |
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 enhancer( | ||
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
Oops, something went wrong.