fix: update

This commit is contained in:
xion 2024-12-01 23:58:08 +08:00
parent 4148d162e4
commit a4f03c76eb
6 changed files with 85 additions and 14 deletions

View File

@ -60,9 +60,14 @@
"./page": { "./page": {
"import": "./dist/web-page.js", "import": "./dist/web-page.js",
"require": "./dist/web-page.js" "require": "./dist/web-page.js"
},
"./web": {
"import": "./dist/web.js",
"require": "./dist/web.js"
} }
}, },
"dependencies": { "dependencies": {
"eventemitter3": "^5.0.1",
"path-to-regexp": "^8.2.0" "path-to-regexp": "^8.2.0"
} }
} }

View File

@ -16,7 +16,7 @@ export default [
format: 'es', // 输出格式设置为 ES 模块 format: 'es', // 输出格式设置为 ES 模块
}, },
plugins: [ plugins: [
resolve(), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块 resolve({ browser: true }), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块
commonjs(), // 使用 @rollup/plugin-commonjs 解析 CommonJS 模块 commonjs(), // 使用 @rollup/plugin-commonjs 解析 CommonJS 模块
typescript(), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件 typescript(), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
], ],
@ -33,9 +33,9 @@ export default [
input: 'src/web-config.ts', input: 'src/web-config.ts',
output: { output: {
file: 'dist/web-config.js', file: 'dist/web-config.js',
format: 'cjs', format: 'es',
}, },
plugins: [resolve(), commonjs(), typescript()], plugins: [resolve({ browser: true }), commonjs(), typescript()],
}, },
{ {
input: 'src/web-config.ts', input: 'src/web-config.ts',
@ -51,7 +51,7 @@ export default [
file: 'dist/web-context.js', file: 'dist/web-context.js',
format: 'es', format: 'es',
}, },
plugins: [resolve(), commonjs(), typescript()], plugins: [resolve({ browser: true }), commonjs(), typescript()],
}, },
{ {
input: 'src/web-context.ts', input: 'src/web-context.ts',
@ -67,7 +67,7 @@ export default [
file: 'dist/web-page.js', file: 'dist/web-page.js',
format: 'es', format: 'es',
}, },
plugins: [resolve(), commonjs(), typescript()], plugins: [resolve({ browser: true }), commonjs(), typescript()],
}, },
{ {
input: 'src/page.ts', input: 'src/page.ts',
@ -77,4 +77,20 @@ export default [
}, },
plugins: [dts()], plugins: [dts()],
}, },
{
input: 'src/web.ts',
output: {
file: 'dist/web.js',
format: 'es',
},
plugins: [resolve({ browser: true }), commonjs(), typescript()],
},
{
input: 'src/web.ts',
output: {
file: 'dist/web.d.ts',
format: 'es',
},
plugins: [dts()],
},
]; ];

View File

@ -3,7 +3,11 @@ import { shallow } from 'zustand/shallow';
import { get } from 'lodash-es'; import { get } from 'lodash-es';
import deepEqual from 'fast-deep-equal'; import deepEqual from 'fast-deep-equal';
export const create = createZutandStore;
export type { StateCreator, StoreApi };
type Listener = (state: any, prevState: any) => void; type Listener = (state: any, prevState: any) => void;
export class StoreManager { export class StoreManager {
stores: Record<string, any>; stores: Record<string, any>;
constructor() { constructor() {
@ -14,7 +18,7 @@ export class StoreManager {
return this.stores[key]; return this.stores[key];
} }
this.stores[key] = createZutandStore(initialStore); 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) { create<T = any, U extends T = any>(initialStore: StateCreator<T, [], [], U>, key: string) {
return this.createStore(initialStore, key); return this.createStore(initialStore, key);
@ -31,8 +35,14 @@ export class StoreManager {
shallow(objA: any, objB: any) { shallow(objA: any, objB: any) {
return shallow(objA, objB); 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) { if (!_store) {
console.error('no store', key); console.error('no store', key);
return; 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 }; export { shallow, deepEqual };

View File

@ -11,7 +11,7 @@ export const useConfig = (initConfig?: GlobalConfig) => {
return _config; return _config;
}; };
export const useConfigKey = <T>(key: string, init: () => T): T => { export const useConfigKey = <T>(key: string, init?: () => T): T => {
const _config = useConfig({}); const _config = useConfig({});
if (key && init) { if (key && init) {
_config[key] = init(); _config[key] = init();
@ -23,7 +23,7 @@ export const useConfigKey = <T>(key: string, init: () => T): T => {
return _config as any; 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({}); const _config = useConfig({});
if (key && init) { if (key && init) {
_config[key] = await init(); _config[key] = await init();

View File

@ -11,7 +11,7 @@ export const useContext = (initContext?: GlobalContext) => {
return _context; return _context;
}; };
export const useContextKey = <T>(key: string, init: () => T): T => { export const useContextKey = <T>(key: string, init?: () => T): T => {
const _context = useContext({}); const _context = useContext({});
if (key && init) { if (key && init) {
_context[key] = init(); _context[key] = init();
@ -23,7 +23,7 @@ export const useContextKey = <T>(key: string, init: () => T): T => {
return _context as any; 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({}); const _context = useContext({});
if (key && init) { if (key && init) {
_context[key] = await init(); _context[key] = await init();

8
src/web.ts Normal file
View 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';