From 0f441ecae52757e893b483eabf1002af0a57b3fb Mon Sep 17 00:00:00 2001 From: L&H Date: Wed, 20 Dec 2023 12:34:30 +0800 Subject: [PATCH] chore: better name --- apps/react-demo/src/store/app/submit.store.ts | 3 ++- apps/react-demo/src/store/app/ui.store.ts | 3 ++- apps/react-demo/src/store/context.ts | 5 ++++- apps/react-demo/src/store/defineStore.ts | 10 +++++----- apps/react-demo/src/store/global/theme.store.ts | 7 ++++--- apps/vue-demo/src/component/atom/SubmitAtom.vue | 2 -- apps/vue-demo/src/store/global/theme.store.ts | 4 ++-- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/apps/react-demo/src/store/app/submit.store.ts b/apps/react-demo/src/store/app/submit.store.ts index 2956ab6..9c90fea 100644 --- a/apps/react-demo/src/store/app/submit.store.ts +++ b/apps/react-demo/src/store/app/submit.store.ts @@ -1,5 +1,6 @@ import { makeAutoObservable } from 'mobx'; import { defineStore } from '../defineStore'; +import { RootContextPointer } from '../context'; /** * @desc App Store @@ -7,7 +8,7 @@ import { defineStore } from '../defineStore'; class SubmitStore { submitted = false; - constructor() { + constructor(readonly pointer: RootContextPointer) { makeAutoObservable(this); } diff --git a/apps/react-demo/src/store/app/ui.store.ts b/apps/react-demo/src/store/app/ui.store.ts index a30cac0..dda6de7 100644 --- a/apps/react-demo/src/store/app/ui.store.ts +++ b/apps/react-demo/src/store/app/ui.store.ts @@ -1,11 +1,12 @@ import { makeAutoObservable } from 'mobx'; import { defineStore } from '../defineStore'; +import { RootContextPointer } from '../context'; /** * @desc App Store */ class UiStore { - constructor() { + constructor(readonly pointer: RootContextPointer) { makeAutoObservable(this); } } diff --git a/apps/react-demo/src/store/context.ts b/apps/react-demo/src/store/context.ts index 205dd92..b0dffc4 100644 --- a/apps/react-demo/src/store/context.ts +++ b/apps/react-demo/src/store/context.ts @@ -1,3 +1,6 @@ import { createContext } from 'react'; -export const RootContext = createContext | null>(null); +export type RootContextPointer = NonNullable; +export type RootContextNullablePointer = Map | null; + +export const RootContext = createContext(null); diff --git a/apps/react-demo/src/store/defineStore.ts b/apps/react-demo/src/store/defineStore.ts index b94502c..4a6f4e7 100644 --- a/apps/react-demo/src/store/defineStore.ts +++ b/apps/react-demo/src/store/defineStore.ts @@ -1,9 +1,9 @@ -import { RootContext } from './context'; +import { RootContext, RootContextNullablePointer, RootContextPointer } from './context'; import { useContext } from 'react'; -export function defineStore InstanceType>(Ctor: T) { - return (injectedContext?: Map | null): InstanceType => { - const map = injectedContext || useContext(RootContext); +export function defineStore InstanceType>(Ctor: T) { + return (pointer?: RootContextNullablePointer): InstanceType => { + const map = pointer || useContext(RootContext); if (!map) { throw new Error('[defineStore] must be used within a Provider'); @@ -12,7 +12,7 @@ export function defineStore InstanceType; } else { - const instance = new Ctor(); + const instance = new Ctor(map); 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 3d432af..1d3ebe8 100644 --- a/apps/react-demo/src/store/global/theme.store.ts +++ b/apps/react-demo/src/store/global/theme.store.ts @@ -1,5 +1,6 @@ import { makeAutoObservable } from 'mobx'; import { defineStore } from '../defineStore'; +import { RootContextPointer } from '../context'; /** * @desc Global Store (Singleton) @@ -7,7 +8,7 @@ import { defineStore } from '../defineStore'; class ThemeStore { theme = 'light'; - constructor() { + constructor(readonly pointer: RootContextPointer) { makeAutoObservable(this); } @@ -19,6 +20,6 @@ class ThemeStore { /** * Shared for Global Store */ -const globalContextValue = new Map(); +const GLOBAL_CONTEXT_POINTER: RootContextPointer = new Map(); -export const useThemeStore = () => defineStore(ThemeStore)(globalContextValue); +export const useThemeStore = () => defineStore(ThemeStore)(GLOBAL_CONTEXT_POINTER); diff --git a/apps/vue-demo/src/component/atom/SubmitAtom.vue b/apps/vue-demo/src/component/atom/SubmitAtom.vue index b7877f2..fb0e73d 100644 --- a/apps/vue-demo/src/component/atom/SubmitAtom.vue +++ b/apps/vue-demo/src/component/atom/SubmitAtom.vue @@ -5,8 +5,6 @@