From c06d3381b827d690073fa6746857e6a8de3aadf6 Mon Sep 17 00:00:00 2001 From: L&H Date: Tue, 19 Dec 2023 16:28:57 +0800 Subject: [PATCH] feat: remove defineStore id param --- apps/react-demo/src/app.tsx | 9 ++++++--- apps/react-demo/src/store/app/submit.store.ts | 2 +- apps/react-demo/src/store/app/ui.store.ts | 2 +- apps/react-demo/src/store/context.ts | 2 +- apps/react-demo/src/store/defineStore.ts | 10 +++++----- apps/react-demo/src/store/global/theme.store.ts | 4 ++-- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/apps/react-demo/src/app.tsx b/apps/react-demo/src/app.tsx index 4c887a1..a376c1c 100644 --- a/apps/react-demo/src/app.tsx +++ b/apps/react-demo/src/app.tsx @@ -8,10 +8,10 @@ import { RootContext } from './store/context'; export function createReactApp(container: HTMLElement, node?: HTMLElement) { const root = createRoot(container); - const rootValue = new Map(); + const rootMap = new Map(); root.render( - +

@@ -19,5 +19,8 @@ export function createReactApp(container: HTMLElement, node?: HTMLElement) { , ); - return () => root.unmount(); + return () => { + rootMap.clear(); + root.unmount(); + }; } diff --git a/apps/react-demo/src/store/app/submit.store.ts b/apps/react-demo/src/store/app/submit.store.ts index 719ca7a..2956ab6 100644 --- a/apps/react-demo/src/store/app/submit.store.ts +++ b/apps/react-demo/src/store/app/submit.store.ts @@ -16,4 +16,4 @@ class SubmitStore { } } -export const useSubmitStore = defineStore('submit', SubmitStore); +export const useSubmitStore = defineStore(SubmitStore); diff --git a/apps/react-demo/src/store/app/ui.store.ts b/apps/react-demo/src/store/app/ui.store.ts index fdbed8d..a30cac0 100644 --- a/apps/react-demo/src/store/app/ui.store.ts +++ b/apps/react-demo/src/store/app/ui.store.ts @@ -10,4 +10,4 @@ class UiStore { } } -export const useUiStore = defineStore('ui', UiStore); +export const useUiStore = defineStore(UiStore); diff --git a/apps/react-demo/src/store/context.ts b/apps/react-demo/src/store/context.ts index 7583b98..205dd92 100644 --- a/apps/react-demo/src/store/context.ts +++ b/apps/react-demo/src/store/context.ts @@ -1,3 +1,3 @@ import { createContext } from 'react'; -export const RootContext = createContext | null>(null); +export const RootContext = createContext | null>(null); diff --git a/apps/react-demo/src/store/defineStore.ts b/apps/react-demo/src/store/defineStore.ts index 0913e2a..b94502c 100644 --- a/apps/react-demo/src/store/defineStore.ts +++ b/apps/react-demo/src/store/defineStore.ts @@ -1,19 +1,19 @@ import { RootContext } from './context'; import { useContext } from 'react'; -export function defineStore InstanceType>(id: string, Ctor: T) { - return (injectedContext?: Map | null): InstanceType => { +export function defineStore InstanceType>(Ctor: T) { + return (injectedContext?: Map | null): InstanceType => { 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; + if (map.has(Ctor)) { + return map.get(Ctor) as InstanceType; } else { const instance = new Ctor(); - map.set(id, instance); + map.set(Ctor, instance); return instance; } }; diff --git a/apps/react-demo/src/store/global/theme.store.ts b/apps/react-demo/src/store/global/theme.store.ts index 10ef49f..3d432af 100644 --- a/apps/react-demo/src/store/global/theme.store.ts +++ b/apps/react-demo/src/store/global/theme.store.ts @@ -19,6 +19,6 @@ class ThemeStore { /** * Shared for Global Store */ -const globalContextValue = new Map(); +const globalContextValue = new Map(); -export const useThemeStore = () => defineStore('theme', ThemeStore)(globalContextValue); +export const useThemeStore = () => defineStore(ThemeStore)(globalContextValue);