UNPKG

ebus-light

Version:
1 lines 6.18 kB
{"version":3,"file":"ebus.mjs","sources":["../src/mitt.ts","../src/ebus.ts"],"sourcesContent":["export type EventType = string | symbol;\n\n// An event handler can take an optional event argument\n// and should not return a value\nexport type Handler<T = unknown> = (event: T) => void;\nexport type WildcardHandler<T = Record<string, unknown>> = (\n type: keyof T,\n event: T[keyof T]\n) => void;\n\n// An array of all currently registered event handlers for a type\nexport type EventHandlerList<T = unknown> = Array<Handler<T>>;\nexport type WildCardEventHandlerList<T = Record<string, unknown>> = Array<\nWildcardHandler<T>\n>;\n\n// A map of event types and their corresponding event handlers.\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\nkeyof Events | '*',\nEventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>\n>;\n\nexport interface Emitter<Events extends Record<EventType, unknown>> {\n all: EventHandlerMap<Events>;\n\n on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;\n on(type: '*', handler: WildcardHandler<Events>): void;\n\n off<Key extends keyof Events>(\n type: Key,\n handler?: Handler<Events[Key]>\n ): void;\n off(type: '*', handler: WildcardHandler<Events>): void;\n\n emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;\n emit<Key extends keyof Events>(\n type: undefined extends Events[Key] ? Key : never\n ): void;\n}\n\n/**\n * Mitt: Tiny (~200b) functional event emitter / pubsub.\n * @name mitt\n * @returns {Mitt}\n */\nexport default function mitt<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events> {\n type GenericEventHandler =\n | Handler<Events[keyof Events]>\n | WildcardHandler<Events>;\n all = all || new Map();\n\n return {\n /**\n * A Map of event names to registered handler functions.\n */\n all,\n\n /**\n * Register an event handler for the given type.\n * @param {string|symbol} type Type of event to listen for, or `'*'` for all events\n * @param {Function} handler Function to call in response to given event\n * @memberOf mitt\n */\n on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {\n const handlers: Array<GenericEventHandler> | undefined = all!.get(type);\n if (handlers) {\n handlers.push(handler);\n } else {\n all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);\n }\n },\n\n /**\n * Remove an event handler for the given type.\n * If `handler` is omitted, all handlers of the given type are removed.\n * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)\n * @param {Function} [handler] Handler function to remove\n * @memberOf mitt\n */\n off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {\n const handlers: Array<GenericEventHandler> | undefined = all!.get(type);\n if (handlers) {\n if (handler) {\n handlers.splice(handlers.indexOf(handler) >>> 0, 1);\n } else {\n all!.set(type, []);\n }\n }\n },\n\n /**\n * Invoke all handlers for the given type.\n * If present, `'*'` handlers are invoked after type-matched handlers.\n *\n * Note: Manually firing '*' handlers is not supported.\n *\n * @param {string|symbol} type The event type to invoke\n * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler\n * @memberOf mitt\n */\n emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {\n let handlers = all!.get(type);\n if (handlers) {\n (handlers as EventHandlerList<Events[keyof Events]>)\n .slice()\n .map((handler) => {\n handler(evt!);\n });\n }\n\n handlers = all!.get('*');\n if (handlers) {\n (handlers as WildCardEventHandlerList<Events>)\n .slice()\n .map((handler) => {\n handler(type, evt!);\n });\n }\n },\n };\n}\n","import mitt from './mitt';\n\ntype App = any;\nconst innerEventBus = mitt();\nexport interface Options {\n // 是否挂载在全局\n global?: boolean;\n // 是否 provide\n inject?: boolean;\n // 实例上挂载的名称\n globalPropertyName?: string;\n // 通过 inject 引入的名称\n injectName?: string;\n}\n\n\nconst eventBus: ReturnType<typeof mitt> & {\n install: (app: App, options?: Options) => void;\n} = {\n ...innerEventBus,\n install,\n};\n\n\nconst DEFAULT_OPTIONS = {\n global: false,\n inject: false,\n globalPropertyName: '$eventBus',\n injectName: '$eventBus',\n};\n\n\neventBus.install = install;\n\nexport default eventBus;\nexport const bus = eventBus;\n\n\nfunction install(app: App, options?: Options) {\n const opt = {\n ...DEFAULT_OPTIONS,\n ...(options || {}),\n };\n\n if (opt.global && opt.globalPropertyName) {\n app.config.globalProperties[opt.globalPropertyName] = eventBus;\n }\n\n if (opt.inject && opt.injectName) {\n app.provide(opt.injectName, eventBus);\n }\n\n return eventBus;\n}\n"],"names":["mitt","all","Map","on","type","handler","handlers","get","push","set","off","splice","indexOf","emit","evt","slice","map","eventBus","_extends","install","DEFAULT_OPTIONS","global","inject","globalPropertyName","injectName","bus","app","options","opt","config","globalProperties","provide"],"mappings":"SA6CwBA,EAAgDC,GAMvE,MAAO,CAILA,IANFA,EAAMA,GAAO,IAAIC,IAcfC,YAA6BC,EAAWC,GACtC,IAAMC,EAAmDL,EAAKM,IAAIH,GAC9DE,EACFA,EAASE,KAAKH,GAEdJ,EAAKQ,IAAIL,EAAM,CAACC,KAWpBK,aAA8BN,EAAWC,GACvC,IAAMC,EAAmDL,EAAKM,IAAIH,GAC9DE,IACED,EACFC,EAASK,OAAOL,EAASM,QAAQP,KAAa,EAAG,GAEjDJ,EAAKQ,IAAIL,EAAM,MAerBS,cAA+BT,EAAWU,GACxC,IAAIR,EAAWL,EAAKM,IAAIH,GACpBE,GACDA,EACES,QACAC,IAAI,SAACX,GACJA,EAAQS,MAIdR,EAAWL,EAAKM,IAAI,OAEjBD,EACES,QACAC,IAAI,SAACX,GACJA,EAAQD,EAAMU,8NChHzB,IAaMG,EAAQC,KAbQlB,KAiBpBmB,QAAAA,IAIIC,EAAkB,CACtBC,QAAQ,EACRC,QAAQ,EACRC,mBAAoB,YACpBC,WAAY,aAIdP,EAASE,QAAUA,EAEnB,IACaM,EAAMR,EAGnB,SAASE,EAAQO,EAAUC,GACzB,IAAMC,EAAGV,KACJE,EACCO,GAAW,IAWjB,OARIC,EAAIP,QAAUO,EAAIL,qBACpBG,EAAIG,OAAOC,iBAAiBF,EAAIL,oBAAsBN,GAGpDW,EAAIN,QAAUM,EAAIJ,YACpBE,EAAIK,QAAQH,EAAIJ,WAAYP,GAGvBA"}