fix: add env

This commit is contained in:
2024-12-09 12:48:13 +08:00
parent b5dde4e823
commit 78d581c98c
9 changed files with 107 additions and 104 deletions

View File

@@ -2,6 +2,10 @@ import { getPathKey } from '@/utils/path-key.ts';
import { match } from 'path-to-regexp';
import deepEqual from 'fast-deep-equal';
const generateRandom = () => {
// return Math.random().toString(36).substring(8);
return crypto.randomUUID();
};
type PageOptions = {
path?: string;
key?: string;
@@ -19,6 +23,7 @@ type CallbackInfo = {
fn: CallFn;
id: string;
} & PageModule;
let currentUrl = location.href;
export class Page {
pageModule = new Map<string, PageModule>();
// pathname的第一个路径
@@ -40,6 +45,7 @@ export class Page {
}
popstate(event?: PopStateEvent, manualOpts?: { id?: string; type: 'singal' | 'all' }) {
const pathname = window.location.pathname;
console.log('popstate', event);
if (manualOpts) {
if (manualOpts.type === 'singal') {
const item = this.callbacks.find((item) => item.id === manualOpts.id);
@@ -120,7 +126,7 @@ export class Page {
*/
subscribe(key: string, fn?: CallFn, opts?: { pathname?: string; runImmediately?: boolean; id?: string }) {
const runImmediately = opts?.runImmediately ?? true; // 默认立即执行
const id = opts?.id ?? Math.random().toString(36).slice(2);
const id = opts?.id ?? generateRandom();
const path = this.pageModule.get(key)?.path;
if (!path) {
console.error(`PageModule ${key} not found`);

View File

@@ -1,41 +0,0 @@
import { getPathKey } from './utils/path-key.ts';
type GlobalConfig = {
name?: string;
[key: string]: any;
};
export const useConfig = (initConfig?: GlobalConfig) => {
const config: GlobalConfig = (window as any).config;
const _config = config || initConfig;
!config && ((window as any)['config'] = _config);
return _config;
};
export const useConfigKey = <T>(key: string, init?: () => T): T => {
const _config = useConfig({});
if (key && init) {
_config[key] = init();
return _config[key] as any;
}
if (key) {
return _config[key];
}
return _config as any;
};
export const useConfigKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
const _config = useConfig({});
if (key && init) {
_config[key] = await init();
return _config[key] as any;
}
if (key) {
return _config[key];
}
return _config as any;
};
export const usePageConfig = (init?: () => {}) => {
const { id } = getPathKey();
return useConfigKey(id, init);
};

View File

@@ -1,41 +0,0 @@
import { getPathKey } from './utils/path-key.ts';
type GlobalContext = {
name?: string;
[key: string]: any;
};
export const useContext = (initContext?: GlobalContext) => {
const context: GlobalContext = (window as any).context;
const _context = context || initContext;
!context && ((window as any)['context'] = _context);
return _context;
};
export const useContextKey = <T>(key: string, init?: () => T): T => {
const _context = useContext({});
if (key && init) {
_context[key] = init();
return _context[key] as any;
}
if (key) {
return _context[key];
}
return _context as any;
};
export const useContextKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
const _context = useContext({});
if (key && init) {
_context[key] = await init();
return _context[key] as any;
}
if (key) {
return _context[key];
}
return _context as any;
};
export const usePageContext = (init?: () => {}) => {
const { id } = getPathKey();
return useContextKey(id, init);
};

90
src/web-env.ts Normal file
View File

@@ -0,0 +1,90 @@
import { getPathKey } from './utils/path-key.ts';
type GlobalEnv = {
name?: string;
[key: string]: any;
};
export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
const env: GlobalEnv = (window as any)[initKey];
const _env = env || initEnv;
if (!env) {
if (_env) {
(window as any)[initKey] = _env;
} else {
(window as any)[initKey] = {};
}
}
return window[initKey] as GlobalEnv;
};
export const useEnvKey = <T = any>(key: string, init?: () => T | null, initKey = 'config'): T => {
const _env = useEnv({}, initKey);
if (key && _env[key]) {
return _env[key];
}
if (key && init) {
_env[key] = init();
return _env[key];
}
return _env as any;
};
export const useEnvKeySync = async <T = any>(key: string, init?: () => Promise<T> | null, initKey = 'conifg'): Promise<T> => {
const _env = useEnv({}, initKey);
if (key && init) {
_env[key] = await init();
return _env[key];
}
if (key) {
return _env[key];
}
return _env as any;
};
export const usePageEnv = (init?: () => {}, initKey = 'conifg') => {
const { id } = getPathKey();
return useEnvKey(id, init, initKey);
};
type GlobalContext = {
name?: string;
[key: string]: any;
};
export const useContext = (initContext?: GlobalContext) => {
return useEnv(initContext, 'context');
};
export const useContextKey = <T = any>(key: string, init?: () => T): T => {
return useEnvKey(key, init, 'context');
};
export const useContextKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
return useEnvKeySync(key, init, 'context');
};
export const usePageContext = (init?: () => {}) => {
const { id } = getPathKey();
return useContextKey(id, init);
};
type GlobalConfig = {
name?: string;
[key: string]: any;
};
export const useConfig = (initConfig?: GlobalConfig) => {
return useEnv(initConfig, 'config');
};
export const useConfigKey = <T = any>(key: string, init?: () => T): T => {
return useEnvKey(key, init, 'config');
};
export const useConfigKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
return useEnvKeySync(key, init, 'config');
};
export const usePageConfig = (init?: () => {}) => {
const { id } = getPathKey();
return useConfigKey(id, init);
};

View File

@@ -1,6 +1,5 @@
export * from './page.ts';
export * from './web-context.ts';
export * from './web-config.ts';
export * from './web-env.ts';
export * from 'nanoid';
export * from 'path-to-regexp';