Skip to content

Commit

Permalink
chore: update create context api
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Sep 12, 2020
1 parent db9e70c commit 2e9e260
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ As a proof of concept converted the sandbox app from the react-redux basic tutor
## Next

- Implement slice selectors and `useSelector` hook, [reference][react-redux-useselector]
- Implement asynchronous middleware context support, [reference][redux-applymiddleware]
- Implement observable pattern for context value, [reference][proposal-observable]

## Suggestions
Expand Down
4 changes: 2 additions & 2 deletions src/components/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export function Provider<S, T extends string, P>({
);

const enhanced = React.useMemo<ContextValue<S, T, P>>(() => {
const { enhance, ...value } = root;
const { enhancer, ...value } = root;
Object.assign(value, { dispatch, getState });
return enhance?.(value) ?? value;
return enhancer?.(value) ?? value;
}, [dispatch, getState, root]);

React.useEffect(
Expand Down
4 changes: 2 additions & 2 deletions src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ function createUnimplemented(objectName?: string): (m: string) => () => never {
export function createContext<S, T extends string, P>(
rootReducer: Reducer<S, T, P>,
preloadedState: S,
enhancer?: ContextEnhance<S, T, P>,
displayName?: string,
enhance?: ContextEnhance<S, T, P>,
): Context<S, T, P> {
const unimplemented = createUnimplemented(`Context(${displayName ?? ""})`);
const Context = React.createContext<ContextValue<S, T, P>>({
dispatch: (a) => a,
enhance,
enhancer,
getState: () => preloadedState,
reducer: rootReducer,
state: preloadedState,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/applyMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { compose } from "./compose";
export function applyMiddleware<S, T extends string, P>(
...middlewares: Middleware<S, T, P>[]
): ContextEnhance<S, T, P> {
return function enhance(
return function enhancer(
context: ContextValue<S, T, P>,
): ContextValue<S, T, P> {
const dispatchStub: ContextDispatch<T, P> = () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/createContext.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`createContext has unimplemented observable symbol 1`] = `
Object {
"dispatch": [Function],
"enhance": undefined,
"enhancer": undefined,
"getState": [Function],
"reducer": [Function],
"state": Object {},
Expand Down
2 changes: 1 addition & 1 deletion tests/createContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("createContext", () => {
});

it("creates context with displayname", () => {
const Context = createContext((s) => s, {}, "TextContext");
const Context = createContext((s) => s, {}, undefined, "TextContext");
expect(Context.displayName).toEqual("TextContext");
});

Expand Down
2 changes: 1 addition & 1 deletion tests/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export function createMocks(): {
const EnhancedContext = createContext(
rootDuck.reducer,
rootDuck.initialState,
"EnhancedContext",
applyMiddleware(logger),
"EnhancedContext",
);

return {
Expand Down
2 changes: 1 addition & 1 deletion typings/context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type ContextEnhance<S = unknown, T extends string = string, P = unknown> = (

// this is similar to a redux store
type ContextValue<S = unknown, T extends string = string, P = unknown> = {
enhance?: ContextEnhance<S, T, P>;
enhancer?: ContextEnhance<S, T, P>;
reducer: Reducer<S, T, P>;
state: S;
} & MiddlewareAPI<S, T, P>;
Expand Down

0 comments on commit 2e9e260

Please sign in to comment.