Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions packages/react-snapshot-context/src/createSnapshotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ export type SnapshotContext<T> = Omit<ReturnType<typeof createContext<ContextVal
Provider: FC<Props<T>>;
};

/**
* The arguments that can be passed to the `createSnapshotContext` function.
*
* Both the default value and options are optional. However, if you want to suppress
* errors and allow the snapshot hooks to be used outside the of the context, you must
* provide a default value.
*/
type Args<T> =
| []
| [defaultValue: T]
| [defaultValue?: T, options?: { suppressErrors?: false }]
| [defaultValue: T, options: { suppressErrors?: true }];

/**
* Creates a Snapshot context. This pattern brings several benefits:
* 1. It provides a way to share state across multiple components without
Expand Down Expand Up @@ -72,16 +85,26 @@ export type SnapshotContext<T> = Omit<ReturnType<typeof createContext<ContextVal
* <MyComponent />
* </NumberContext.SnapshotProvider>
*/
export const createSnapshotContext = <T,>(): SnapshotContext<T> => {
export const createSnapshotContext = <T,>(...args: Args<T>): SnapshotContext<T> => {
const [defaultValue, options] = args;

const BaseContext = createContext<ContextValue<T>>({
getSnapshot() {
throw new Error('Cannot be accessed outside of snapshot provider');
if (!options?.suppressErrors) {
throw new Error('Cannot be accessed outside of snapshot provider');
}
return defaultValue as T;
},
setSnapshot() {
throw new Error('Cannot be accessed outside of snapshot provider');
if (!options?.suppressErrors) {
throw new Error('Cannot be accessed outside of snapshot provider');
}
},
subscribe() {
throw new Error('Cannot be accessed outside of snapshot provider');
if (!options?.suppressErrors) {
throw new Error('Cannot be accessed outside of snapshot provider');
}
return () => void 0;
}
});

Expand Down
Loading