UNPKG

@stevenleep/storage

Version:

enhanced local storage

80 lines (75 loc) 3.09 kB
declare enum StorageType { LocalStorage = "localStorage", SessionStorage = "sessionStorage", MemoryStorage = "memoryStorage", Cookie = "cookie" } declare const StorageMap: Map<string, BaseStorageInterface>; /** * 使用TS自己的Storage在Omit/Pick等处理后,会使得类型丢失 * * 故,Copy出Storage的类型定义 * */ interface StorageDefine { readonly length?: number; clear(): void; getItem(key: string): string | null | Promise<string | null>; key(index: number): string | null | Promise<string | null>; removeItem(key: string): void; setItem(key: string, value: string): void; [key: string]: any; } /** * 实现了BaseStorage就可用于扩展数据 */ interface BaseStorageInterface extends StorageDefine { } interface BaseStorageOptions { storage?: BaseStorageInterface | StorageType; namespace?: string; context?: Record<string | number | symbol, any>; } declare class BaseStorage implements BaseStorageInterface { private readonly baseStorageOptions; protected storage: BaseStorageInterface; namespace: string; constructor(baseStorageOptions?: BaseStorageOptions); setNamespace(namespace: string): void; setStorage(storeTypeOrCustomStorage: StorageType | BaseStorageInterface): void; getNamespace(): string; getStorage(): BaseStorageInterface; isMounted(): boolean; private mappingNamespace; clear(): void; getItem(key: string): string | Promise<string | null> | null; key(index: number): string | Promise<string | null> | null; removeItem(key: string): void; setItem(key: string, value: string): void; } type Subscriber<T> = (newValue: T, oldValue: T, key?: string) => void; declare class BaseReactive<T = any> { subscribers: Map<string, Subscriber<T>[]>; stateChangedSubscribers: Set<Subscriber<T>>; subscribe(key: string, callback: Subscriber<T>): () => void; unsubscribe(key: string, callback: Subscriber<T>): void; broadcast(newValue: T, oldValue: T): void; clear(key?: string): void; notify(key: string, newValue: T, oldValue: T): void; subscribeStateChange(callback: Subscriber<T>): () => void; notifyStateChange(newValue: T, oldValue: T, key?: string): void; unsubscribeStateChange(callback: Subscriber<T>): void; clearStateChange(): void; subscribeWithScope(namespace: string, key: string, callback: Subscriber<T>): (() => void) | void; } declare class DoActionStorage extends BaseStorage { private readonly options; readonly observer: BaseReactive; constructor(options?: BaseStorageOptions); setItem(key: string, value: any): void; removeItem(key: string): void; getItem(key: string): string | Promise<string | null> | null; clear(): void; stateChange(key: string, newValue: string | null | Promise<string | null>, oldValue: string | Promise<string | null> | null): void; } export { BaseReactive, BaseStorage, BaseStorageInterface, BaseStorageOptions, DoActionStorage, StorageType, StorageMap as Storages, Subscriber, DoActionStorage as default };