UNPKG

mustard-app

Version:

个人前端微应用建设中。。。

62 lines (61 loc) 2.59 kB
import { IAppStatus, IAppStatusCN, MustardName } from '../typings'; export type EventName = MustardName; export type BindMethod = string; export type TCallback = (value: unknown, oldValue: unknown, source: MustardName) => void; export type TLifeCallback = (key: MustardName) => void; export type TDataChangeCallback = (key: MustardName, data: unknown) => void; type EventGlobalLifeKey = `globalLife_${IAppStatusCN}`; type EventGlobalDataChangeKey = 'globalDataChange'; type EventDataKey = `data_${EventName}`; type EventDataChangeKey = `dataChange_${EventName}`; type EventLifeKey = `life_${EventName}_${IAppStatusCN}`; type EventBindKey = `bind_${EventName}_${BindMethod}`; export type EventKey = EventDataKey | EventDataChangeKey | EventLifeKey | EventBindKey | EventGlobalLifeKey | EventGlobalDataChangeKey; export interface EventValue { data: unknown; sourceOfData: MustardName; assignment: boolean; callbacks: Set<TCallback>; repeatSend: WeakSet<TCallback>; } export declare class EventCenter { eventList: Map<EventKey, EventValue>; /** * 初始化 * @param name 事件名 * @param options 事件配置 */ private initEvent; /** * 订阅事件 * @param name 事件名 * @param fn 事件 * @param param2.immediately 是否立即执行(对应消息dispatch过) * @param param2.repeatSend 多次dispatch相同的data,只有第一次生效 */ on(name: EventKey, fn: TCallback, { immediately, repeatSend }?: { immediately?: boolean; repeatSend?: boolean; }): void; /** * 注销订阅事件 * @param name 事件名 * @param fn 事件 不传递全部清空 */ off(name: EventKey, fn?: TCallback): void; /** * 发布消息 * @param name 事件名 * @param source 那个应用发送的消息 * @param data 数据 */ dispatch(name: EventKey, source: MustardName, data?: unknown): void; } export declare function getEventGlobalLifeKeyByValue(value: IAppStatusCN): EventGlobalLifeKey; export declare function getEventGlobalDataChangeKey(): EventGlobalDataChangeKey; export declare function getEventDataKey(name: EventName): EventDataKey; export declare function getEventDataChangeKey(name: EventName): EventDataChangeKey; export declare function getEventLifeKeyByKey(name: EventName, key: IAppStatusCN): EventLifeKey; export declare function getEventLifeKeyByValue(name: EventName, value: IAppStatus): EventLifeKey; export declare function getEventBindKey(name: EventName, method: BindMethod): EventBindKey; export {};