Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Core Functionality #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
489 changes: 330 additions & 159 deletions README.md

Large diffs are not rendered by default.

42 changes: 0 additions & 42 deletions src/HydrationBoundary.tsx

This file was deleted.

27 changes: 0 additions & 27 deletions src/RenderingBoundary.tsx

This file was deleted.

10 changes: 0 additions & 10 deletions src/SuspenseBoundary.tsx

This file was deleted.

24 changes: 24 additions & 0 deletions src/hydrate-atoms/HydrationBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client';

import type { PropsWithChildren } from 'react';
import type {
AnyWritableAtom,
HydrateAtomOptions,
InferAtomTuples,
} from './types.js';
import { useHydrateAtoms } from './use-hydrate-atoms.js';

export function HydrationBoundary<
T extends (readonly [AnyWritableAtom, ...unknown[]])[],
>({
children,
hydrateAtoms,
options,
}: PropsWithChildren<{
hydrateAtoms: InferAtomTuples<T>;
options?: HydrateAtomOptions | undefined;
}>) {
useHydrateAtoms(hydrateAtoms, options);

return <>{children}</>;
}
2 changes: 2 additions & 0 deletions src/hydrate-atoms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './use-hydrate-atoms.js';
export * from './HydrationBoundary.js';
17 changes: 17 additions & 0 deletions src/hydrate-atoms/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { WritableAtom } from 'jotai';
import type { Store } from '../shared.js';

export type AnyWritableAtom = WritableAtom<unknown, never[], unknown>;

export type InferAtomTuples<T> = {
[K in keyof T]: T[K] extends readonly [infer A, ...unknown[]]
? A extends WritableAtom<unknown, infer Args, unknown>
? readonly [A, ...Args]
: T[K]
: never;
};

export type HydrateAtomOptions = {
store?: Store | undefined;
enableReHydrate?: boolean | undefined;
};
44 changes: 44 additions & 0 deletions src/hydrate-atoms/use-hydrate-atoms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { atom, useStore } from 'jotai';
import type {
AnyWritableAtom,
HydrateAtomOptions,
InferAtomTuples,
} from './types.js';
import { useCallback, useEffect, useRef } from 'react';

export function useHydrateAtoms<
T extends (readonly [AnyWritableAtom, ...unknown[]])[],
>(hydrateAtoms: InferAtomTuples<T>, options?: HydrateAtomOptions) {
const isHydratedRef = useRef(false);
const lastHydrateAtoms = useRef(hydrateAtoms);
const store = useStore(
options?.store != null ? { store: options?.store } : undefined,
);

const hydrate = useCallback(() => {
store.set(isHydratingPrimitiveAtom, true);
for (const [atom, ...args] of hydrateAtoms) {
store.set(atom, ...args);
}
store.set(isHydratingPrimitiveAtom, false);
isHydratedRef.current = true;
}, [store, hydrateAtoms]);

if (!isHydratedRef.current) {
hydrate();
}

useEffect(() => {
if (!options?.enableReHydrate) {
return;
}
if (hydrateAtoms !== lastHydrateAtoms.current) {
lastHydrateAtoms.current = hydrateAtoms;
hydrate();
}
}, [hydrate, options?.enableReHydrate, hydrateAtoms]);
}

const isHydratingPrimitiveAtom = atom(false);

export const isHydratingAtom = atom((get) => get(isHydratingPrimitiveAtom));
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './HydrationBoundary.js';
export * from './RenderingBoundary.js';
export * from './SuspenseBoundary.js';
export * from './hydrate-atoms/index.js';
export * from './ssr-provider/index.js';
3 changes: 3 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { createStore } from 'jotai';

export type Store = ReturnType<typeof createStore>;
33 changes: 33 additions & 0 deletions src/ssr-provider/SSRProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use client';

import { Provider } from 'jotai';
import type { PropsWithChildren } from 'react';
import { useCreateStore } from './use-create-store.js';
import type { Store } from '../shared.js';

function SSRProviderWithGivenStore({
store,
children,
}: PropsWithChildren<{ store: Store }>) {
return <Provider store={store}>{children}</Provider>;
}

function SSRProviderWithNewStore({ children }: PropsWithChildren) {
const store = useCreateStore();
return <Provider store={store}>{children}</Provider>;
}

export function SSRProvider({
store,
children,
}: PropsWithChildren<{ store?: Store }>) {
if (store) {
return (
<SSRProviderWithGivenStore store={store}>
{children}
</SSRProviderWithGivenStore>
);
} else {
return <SSRProviderWithNewStore>{children}</SSRProviderWithNewStore>;
}
}
2 changes: 2 additions & 0 deletions src/ssr-provider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './use-create-store.js';
export * from './SSRProvider.js';
7 changes: 7 additions & 0 deletions src/ssr-provider/use-create-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createStore } from 'jotai';
import { useState } from 'react';

export const useCreateStore = () => {
const [store] = useState(() => createStore());
return store;
};
Loading