Skip to content

Commit

Permalink
feat: remove defineStore id param
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqours committed Dec 19, 2023
1 parent d076ca1 commit c06d338
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
9 changes: 6 additions & 3 deletions apps/react-demo/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import { RootContext } from './store/context';

export function createReactApp(container: HTMLElement, node?: HTMLElement) {
const root = createRoot(container);
const rootValue = new Map<string, unknown>();
const rootMap = new Map<object, unknown>();

root.render(
<RootContext.Provider value={rootValue}>
<RootContext.Provider value={rootMap}>
<Section />
<Section />
<p style={{ width: 128 }} dangerouslySetInnerHTML={{ __html: JetbrainsLogo }} />
<MobxThemeSwitch />
</RootContext.Provider>,
);

return () => root.unmount();
return () => {
rootMap.clear();
root.unmount();
};
}
2 changes: 1 addition & 1 deletion apps/react-demo/src/store/app/submit.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class SubmitStore {
}
}

export const useSubmitStore = defineStore('submit', SubmitStore);
export const useSubmitStore = defineStore(SubmitStore);
2 changes: 1 addition & 1 deletion apps/react-demo/src/store/app/ui.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ class UiStore {
}
}

export const useUiStore = defineStore('ui', UiStore);
export const useUiStore = defineStore(UiStore);
2 changes: 1 addition & 1 deletion apps/react-demo/src/store/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createContext } from 'react';

export const RootContext = createContext<Map<string, unknown> | null>(null);
export const RootContext = createContext<Map<object, unknown> | null>(null);
10 changes: 5 additions & 5 deletions apps/react-demo/src/store/defineStore.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { RootContext } from './context';
import { useContext } from 'react';

export function defineStore<T extends new (...args: unknown[]) => InstanceType<T>>(id: string, Ctor: T) {
return (injectedContext?: Map<string, unknown> | null): InstanceType<T> => {
export function defineStore<T extends new (...args: unknown[]) => InstanceType<T>>(Ctor: T) {
return (injectedContext?: Map<object, unknown> | null): InstanceType<T> => {
const map = injectedContext || useContext(RootContext);

if (!map) {
throw new Error('[defineStore] must be used within a Provider');
}

if (map.has(id)) {
return map.get(id) as InstanceType<T>;
if (map.has(Ctor)) {
return map.get(Ctor) as InstanceType<T>;
} else {
const instance = new Ctor();
map.set(id, instance);
map.set(Ctor, instance);
return instance;
}
};
Expand Down
4 changes: 2 additions & 2 deletions apps/react-demo/src/store/global/theme.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class ThemeStore {
/**
* Shared for Global Store
*/
const globalContextValue = new Map<string, unknown>();
const globalContextValue = new Map<object, unknown>();

export const useThemeStore = () => defineStore('theme', ThemeStore)(globalContextValue);
export const useThemeStore = () => defineStore(ThemeStore)(globalContextValue);

0 comments on commit c06d338

Please sign in to comment.