Skip to content

Commit

Permalink
chore: enhance scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Sep 12, 2020
1 parent 43b3ace commit fae845a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function Provider<S, T extends string, P>({
);

const enhanced = React.useMemo<ContextValue<S, T, P>>(
() => ({ ...root, dispatch }),
() => root.enhance({ ...root, dispatch }),
[root, dispatch],
);

Expand Down
5 changes: 4 additions & 1 deletion src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ function createUnimplemented(objectName?: string): (m: string) => () => never {
};
}

const idFn = <A>(a: A): A => a;

export function createContext<S, T extends string, P>(
rootReducer: Reducer<S, T, P>,
preloadedState: S,
displayName?: string,
): Context<S, T, P> {
const unimplemented = createUnimplemented(`Context(${displayName ?? ""})`);
const Context = React.createContext<ContextValue<S, T, P>>({
dispatch: (a) => a,
dispatch: idFn,
enhance: idFn,
reducer: rootReducer,
state: preloadedState,
[SymbolObservable]: unimplemented(SymbolObservable.toString()),
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/createContext.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`createContext has unimplemented observable symbol 1`] = `
Object {
"dispatch": [Function],
"enhance": [Function],
"reducer": [Function],
"state": Object {},
Symbol(@@observable): [Function],
Expand Down
2 changes: 1 addition & 1 deletion tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("e2e", (): void => {
const Provider = createRootProvider(Context);

function Example(): React.ReactElement {
const { state, dispatch } = React.useContext(Context);
const { dispatch, state } = React.useContext(Context);
const increment = React.useCallback(() => {
dispatch(counterDuck.actions.increment());
}, [dispatch]);
Expand Down
13 changes: 9 additions & 4 deletions typings/context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ type ContextDispatch<T extends string = string, P = unknown> = (
action: Action<T, P>,
) => Action<T, P>;

type ContextEnhance<V extends ContextValue> = (value: V) => V;

// this is similar to a redux store
type ContextValue<S, T extends string = string, P = unknown> = {
type ContextValue<S = unknown, T extends string = string, P = unknown> = {
dispatch: ContextDispatch<T, P>;
enhance: ContextEnhance<ContextValue<S, T, P>>;
reducer: Reducer<S, T, P>;
state: S;
};

type Context<S, T extends string = string, P = unknown> = React.Context<
ContextValue<S, T, P>
>;
type Context<
S = unknown,
T extends string = string,
P = unknown
> = React.Context<ContextValue<S, T, P>>;

0 comments on commit fae845a

Please sign in to comment.