Skip to content

Compose createDevStore with previous createStore #199

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

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Atom, WritableAtom, createStore } from 'jotai/vanilla';
import type {
INTERNAL_AtomState,
INTERNAL_Store,
INTERNAL_getBuildingBlocksRev1,
} from 'jotai/vanilla/internals';

export type DevStore = {
Expand All @@ -17,6 +18,10 @@ export type StoreWithoutDevMethods = ReturnType<typeof createStore>;
export type StoreWithDevMethods = INTERNAL_Store & DevStore;

export type Store = StoreWithoutDevMethods | StoreWithDevMethods;
type Mutable<T> = { -readonly [P in keyof T]: T[P] };
export type BuildingBlocks = Mutable<
ReturnType<typeof INTERNAL_getBuildingBlocksRev1>
>;

export type Options = Parameters<typeof useStore>[0];

Expand Down
50 changes: 24 additions & 26 deletions src/utils/internals/compose-with-devtools.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Atom, WritableAtom } from 'jotai';
import { INTERNAL_overrideCreateStore } from 'jotai/vanilla';
import { INTERNAL_overrideCreateStore, createStore } from 'jotai/vanilla';
import {
INTERNAL_buildStoreRev1 as INTERNAL_buildStore,
INTERNAL_getBuildingBlocksRev1,
INTERNAL_initializeStoreHooks,
} from 'jotai/vanilla/internals';
import {
AnyAtom,
AnyAtomError,
AnyAtomValue,
BuildingBlocks,
DevStore,
Store,
StoreWithDevMethods,
Expand Down Expand Up @@ -156,27 +158,26 @@ const __composeWithDevTools = (
return store as typeof store & DevToolsStoreMethods;
};

const createDevStore = (): StoreWithDevMethods => {
const createDevStore = (
prevCreateStore: (() => Store) | undefined,
): StoreWithDevMethods => {
let inRestoreAtom = 0;
const storeHooks = INTERNAL_initializeStoreHooks({});
const atomStateMap = new WeakMap();
const mountedAtoms = new WeakMap();
const store = INTERNAL_buildStore(
atomStateMap,
mountedAtoms,
undefined,
undefined,
undefined,
undefined,
storeHooks,
undefined,
(atom, get, set, ...args) => {
if (inRestoreAtom) {
return set(atom, ...(args as any));
}
return atom.write(get, set, ...(args as any));
},
);
const prevStore = prevCreateStore?.() ?? INTERNAL_buildStore();
const buildingBlocks = [
...INTERNAL_getBuildingBlocksRev1(prevStore),
] as BuildingBlocks;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it already mutable without the type annotation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think buildingBlocks should be frozen.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be frozen. I'm only talking about the typing. Doesn't this work?

  const buildingBlocks = [
    ...INTERNAL_getBuildingBlocksRev1(prevStore),
  ];

const storeHooks = INTERNAL_initializeStoreHooks(buildingBlocks[6]);
const atomWrite = buildingBlocks[8];
buildingBlocks[8] = (atom, get, set, ...args) => {
if (inRestoreAtom) {
return set(atom, ...args);
}
return atomWrite
? atomWrite(atom, get, set, ...args)
: atom.write(get, set, ...args);
};
const store = INTERNAL_buildStore(...buildingBlocks);
const [atomStateMap, mountedAtoms] = buildingBlocks;
const debugMountedAtoms = new Set<Atom<unknown>>();
storeHooks.m.add(undefined, (atom) => {
debugMountedAtoms.add(atom);
Expand Down Expand Up @@ -214,18 +215,15 @@ const createDevStore = (): StoreWithDevMethods => {
},
};

return {
...store,
...devStore,
};
return Object.assign(store, devStore);
};

const isDevStore = (store: Store): store is StoreWithDevMethods => {
return 'get_internal_weak_map' in store;
};

INTERNAL_overrideCreateStore((prev) => {
return createDevStore;
return () => createDevStore(prev);
});

export const composeWithDevTools = (
Expand Down
Loading