fix: update
This commit is contained in:
52
src/store.ts
52
src/store.ts
@@ -3,7 +3,11 @@ import { shallow } from 'zustand/shallow';
|
||||
import { get } from 'lodash-es';
|
||||
import deepEqual from 'fast-deep-equal';
|
||||
|
||||
export const create = createZutandStore;
|
||||
export type { StateCreator, StoreApi };
|
||||
|
||||
type Listener = (state: any, prevState: any) => void;
|
||||
|
||||
export class StoreManager {
|
||||
stores: Record<string, any>;
|
||||
constructor() {
|
||||
@@ -14,7 +18,7 @@ export class StoreManager {
|
||||
return this.stores[key];
|
||||
}
|
||||
this.stores[key] = createZutandStore(initialStore);
|
||||
return this.stores[key];
|
||||
return this.stores[key] as StoreApi<T>;
|
||||
}
|
||||
create<T = any, U extends T = any>(initialStore: StateCreator<T, [], [], U>, key: string) {
|
||||
return this.createStore(initialStore, key);
|
||||
@@ -31,8 +35,14 @@ export class StoreManager {
|
||||
shallow(objA: any, objB: any) {
|
||||
return shallow(objA, objB);
|
||||
}
|
||||
subscribe(fn: Listener, { key, path, deep }: { key: string; path: string; deep?: boolean }) {
|
||||
const _store = this.stores[key] as StoreApi<any>;
|
||||
/**
|
||||
* path 为可以是 '.a.b.c'的形式
|
||||
* @param fn
|
||||
* @param param1
|
||||
* @returns
|
||||
*/
|
||||
subscribe(fn: Listener, { key, path, deep, store }: { key: string; path: string; deep?: boolean; store?: StoreApi<any> }) {
|
||||
const _store = store || (this.stores[key] as StoreApi<any>);
|
||||
if (!_store) {
|
||||
console.error('no store', key);
|
||||
return;
|
||||
@@ -59,8 +69,40 @@ export class StoreManager {
|
||||
});
|
||||
}
|
||||
}
|
||||
// export const store = new StoreManager();
|
||||
|
||||
export const store = new StoreManager();
|
||||
|
||||
type FnListener<T = any> = (state: T, prevState: T) => void;
|
||||
type SubOptions = {
|
||||
path?: string;
|
||||
deep?: boolean;
|
||||
store?: StoreApi<any>;
|
||||
};
|
||||
export const sub = <T = any>(fn: FnListener<T>, { path, deep, store }: SubOptions) => {
|
||||
if (!store) {
|
||||
console.error('no store');
|
||||
return;
|
||||
}
|
||||
return store.subscribe((newState: T, oldState: T) => {
|
||||
try {
|
||||
const newPath = get(newState, path);
|
||||
const oldPath = get(oldState, path);
|
||||
if (!newPath && !oldPath) {
|
||||
// 都不存在
|
||||
return;
|
||||
}
|
||||
if (deep) {
|
||||
if (!deepEqual(newPath, oldPath)) {
|
||||
fn?.(newState, oldState);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!shallow(newPath, oldPath)) {
|
||||
fn?.(newState, oldState);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('subscribe error', e);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export { shallow, deepEqual };
|
||||
|
||||
@@ -11,7 +11,7 @@ export const useConfig = (initConfig?: GlobalConfig) => {
|
||||
return _config;
|
||||
};
|
||||
|
||||
export const useConfigKey = <T>(key: string, init: () => T): T => {
|
||||
export const useConfigKey = <T>(key: string, init?: () => T): T => {
|
||||
const _config = useConfig({});
|
||||
if (key && init) {
|
||||
_config[key] = init();
|
||||
@@ -23,7 +23,7 @@ export const useConfigKey = <T>(key: string, init: () => T): T => {
|
||||
return _config as any;
|
||||
};
|
||||
|
||||
export const useConfigKeySync = async <T = any>(key: string, init: () => Promise<T>): Promise<T> => {
|
||||
export const useConfigKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
|
||||
const _config = useConfig({});
|
||||
if (key && init) {
|
||||
_config[key] = await init();
|
||||
|
||||
@@ -11,7 +11,7 @@ export const useContext = (initContext?: GlobalContext) => {
|
||||
return _context;
|
||||
};
|
||||
|
||||
export const useContextKey = <T>(key: string, init: () => T): T => {
|
||||
export const useContextKey = <T>(key: string, init?: () => T): T => {
|
||||
const _context = useContext({});
|
||||
if (key && init) {
|
||||
_context[key] = init();
|
||||
@@ -23,7 +23,7 @@ export const useContextKey = <T>(key: string, init: () => T): T => {
|
||||
return _context as any;
|
||||
};
|
||||
|
||||
export const useContextKeySync = async <T = any>(key: string, init: () => Promise<T>): Promise<T> => {
|
||||
export const useContextKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
|
||||
const _context = useContext({});
|
||||
if (key && init) {
|
||||
_context[key] = await init();
|
||||
|
||||
8
src/web.ts
Normal file
8
src/web.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export * from './page.ts';
|
||||
export * from './web-context.ts';
|
||||
export * from './web-config.ts';
|
||||
|
||||
export * from 'nanoid';
|
||||
export * from 'path-to-regexp';
|
||||
|
||||
export * from 'eventemitter3';
|
||||
Reference in New Issue
Block a user