store init

This commit is contained in:
2025-03-26 11:52:56 +08:00
parent b5dde4e823
commit 152fda350e
8 changed files with 266 additions and 12 deletions

View File

@@ -20,7 +20,22 @@ export class StoreManager {
this.stores[key] = createZutandStore(initialStore);
return this.stores[key] as StoreApi<T>;
}
create<T = any, U extends T = any>(initialStore: StateCreator<T, [], [], U>, key: string) {
create<T extends Record<string, any>, U extends T = T>(initialStore: StateCreator<T, [], [], U>, key: string) {
return this.createStore(initialStore, key) as StoreApi<T>;
}
createIfNotExists<T extends Record<string, any>, U extends T = T>(
initialStore: StateCreator<T, [], [], U>,
key: string,
opts?: {
force?: boolean;
},
): StoreApi<T> {
if (this.stores[key] && !opts?.force) {
return this.stores[key];
}
if (this.stores[key] && opts?.force) {
this.removeStore(key);
}
return this.createStore(initialStore, key);
}
getStore(key: string) {
@@ -84,8 +99,8 @@ export const sub = <T = any>(fn: FnListener<T>, { path, deep, store }: SubOption
}
return store.subscribe((newState: T, oldState: T) => {
try {
const newPath = get(newState, path);
const oldPath = get(oldState, path);
const newPath = get(newState, path as string);
const oldPath = get(oldState, path as string);
if (!newPath && !oldPath) {
// 都不存在
return;