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

Conversation

dmaskasky
Copy link
Member

Summary

If a previous createStore has already overridden createStore, we should try our best to compose with it.

Copy link

codesandbox-ci bot commented Jul 22, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Comment on lines 165 to 180
const prevStore = prevCreateStore?.();
const buildingBlocks = (
prevStore ? INTERNAL_getBuildingBlocksRev1(prevStore) : []
) as BuildingBlocks;
buildingBlocks[0] ??= new WeakMap();
buildingBlocks[1] ??= new WeakMap();
buildingBlocks[6] ??= {};
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;
Copy link
Member Author

Choose a reason for hiding this comment

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

@dai-shi
I think we need more thought around how to derive a store from a previous store. Currently, overriding certain building blocks won't work as expected.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

Again, thanks for working on this.

@dmaskasky dmaskasky force-pushed the dmaskasky-compose-with-prevstore branch 2 times, most recently from 26b1d10 to 5c27901 Compare July 23, 2025 08:23
@dmaskasky dmaskasky force-pushed the dmaskasky-compose-with-prevstore branch from 5c27901 to 2c586d0 Compare July 23, 2025 09:07
@dmaskasky dmaskasky requested a review from dai-shi July 23, 2025 09:10
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),
  ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants