UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

103 lines (102 loc) 2.91 kB
/** * 监听模块 * */ export type WatchEventCallback = (...args: any[]) => void; export type WatchEventItem = { key: string; callback: WatchEventCallback; }; export interface WatchEventSubscription { unsubscribe: () => void; } declare class Watch { private events; private enableListMap; /** * 订阅事件 * @param event 事件名称 * @param callback 回调函数 * @returns 订阅对象,可用于取消订阅 */ subscribe(watchKey: string, event: WatchEventItem): WatchEventSubscription; /** * 启用事件 * @param watchKey 监听的key * @param event 事件名称 */ enableEvent(watchKey: string, eventKey: string): void; /** * 禁用某个事件 * @param watchKey 监听的key * @param eventKey 事件名称 */ disableEvent(watchKey: string, eventKey: string): void; /** * 冻结某个监听下的所有事件 * @param watchKey 监听的key */ disabledAllEventByWatchKey(watchKey: string): void; /** * 启用某个监听下的所有事件 * @param watchKey 监听的key */ enableAllEventByWatchKey(watchKey: string): void; /** * 取消订阅事件 * @param event 事件名称 * @param callback 回调函数 * @returns 是否成功取消订阅 */ unsubscribe(watchKey: string, event: WatchEventItem): void; /** * 发布事件 * @param event 事件名称 * @param args 事件参数 */ publish(watchKey: string, ...args: any[]): void; /** * 根据事件名称发布事件 * @param watchKey 监听的key * @param eventKey 事件名称 * @param args 事件参数 */ publishByEventKey(watchKey: string, eventKey: string, ...args: any[]): void; /** * 获取最后一个启用的事件 * @param watchKey 监听的key * @returns 最后一个启用的事件 */ getLastEnableEventKey(watchKey: string): WatchEventItem['key'] | undefined; /** * 移除某个事件的所有订阅 * @param event 事件名称 * @returns 是否成功移除 */ clearEvent(watchKey: string): void; /** * 移除所有事件订阅 */ clearAllEvents(): void; /** * 获取事件的订阅数量 * @param event 事件名称 * @returns 订阅数量 */ getSubscriberCount(event: string): number; /** * 检查事件是否有订阅者 * @param event 事件名称 * @returns 是否有订阅者 */ hasSubscribers(event: string): boolean; /** * 一次性订阅事件,事件触发后自动取消订阅 * @param event 事件名称 * @param callback 回调函数 * @returns 订阅对象,可用于取消订阅 */ once(watchKey: string, event: WatchEventItem): WatchEventSubscription; } declare const watch: Watch; export default watch;