Skip to content

Commit

Permalink
chore: better name
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqours committed Dec 20, 2023
1 parent c06d338 commit 0f441ec
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 15 deletions.
3 changes: 2 additions & 1 deletion apps/react-demo/src/store/app/submit.store.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { makeAutoObservable } from 'mobx';
import { defineStore } from '../defineStore';
import { RootContextPointer } from '../context';

/**
* @desc App Store
*/
class SubmitStore {
submitted = false;

constructor() {
constructor(readonly pointer: RootContextPointer) {
makeAutoObservable(this);
}

Expand Down
3 changes: 2 additions & 1 deletion apps/react-demo/src/store/app/ui.store.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
Expand Down
5 changes: 4 additions & 1 deletion apps/react-demo/src/store/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { createContext } from 'react';

export const RootContext = createContext<Map<object, unknown> | null>(null);
export type RootContextPointer = NonNullable<RootContextNullablePointer>;
export type RootContextNullablePointer = Map<unknown, unknown> | null;

export const RootContext = createContext<RootContextNullablePointer>(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,9 +1,9 @@
import { RootContext } from './context';
import { RootContext, RootContextNullablePointer, RootContextPointer } from './context';
import { useContext } from 'react';

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);
export function defineStore<T extends new (pointer: RootContextPointer) => InstanceType<T>>(Ctor: T) {
return (pointer?: RootContextNullablePointer): InstanceType<T> => {
const map = pointer || useContext(RootContext);

if (!map) {
throw new Error('[defineStore] must be used within a Provider');
Expand All @@ -12,7 +12,7 @@ export function defineStore<T extends new (...args: unknown[]) => InstanceType<T
if (map.has(Ctor)) {
return map.get(Ctor) as InstanceType<T>;
} else {
const instance = new Ctor();
const instance = new Ctor(map);
map.set(Ctor, instance);
return instance;
}
Expand Down
7 changes: 4 additions & 3 deletions apps/react-demo/src/store/global/theme.store.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { makeAutoObservable } from 'mobx';
import { defineStore } from '../defineStore';
import { RootContextPointer } from '../context';

/**
* @desc Global Store (Singleton)
*/
class ThemeStore {
theme = 'light';

constructor() {
constructor(readonly pointer: RootContextPointer) {
makeAutoObservable(this);
}

Expand All @@ -19,6 +20,6 @@ class ThemeStore {
/**
* Shared for Global Store
*/
const globalContextValue = new Map<object, unknown>();
const GLOBAL_CONTEXT_POINTER: RootContextPointer = new Map();

export const useThemeStore = () => defineStore(ThemeStore)(globalContextValue);
export const useThemeStore = () => defineStore(ThemeStore)(GLOBAL_CONTEXT_POINTER);
2 changes: 0 additions & 2 deletions apps/vue-demo/src/component/atom/SubmitAtom.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
</template>

<script setup lang="ts">
import { toRefs } from 'vue';
interface IProps {
submitted: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/vue-demo/src/store/global/theme.store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createPinia, defineStore } from 'pinia';

/** 这是全局实例,不需要使用 App.use() 注入 */
const GLOBAL_REF = createPinia();
const GLOBAL_CONTEXT_POINTER = createPinia();

export const useThemeStore = () =>
defineStore('theme', {
Expand All @@ -11,4 +11,4 @@ export const useThemeStore = () =>
this.theme = this.theme === 'light' ? 'dark' : 'light';
},
},
})(GLOBAL_REF);
})(GLOBAL_CONTEXT_POINTER);

0 comments on commit 0f441ec

Please sign in to comment.