1 |
- {"ast":null,"code":"import \"core-js/modules/es.array.push.js\";\nexport default function (n) {\n return {\n all: n = n || new Map(),\n on: function (t, e) {\n var i = n.get(t);\n i ? i.push(e) : n.set(t, [e]);\n },\n off: function (t, e) {\n var i = n.get(t);\n i && (e ? i.splice(i.indexOf(e) >>> 0, 1) : n.set(t, []));\n },\n emit: function (t, e) {\n var i = n.get(t);\n i && i.slice().map(function (n) {\n n(e);\n }), (i = n.get(\"*\")) && i.slice().map(function (n) {\n n(t, e);\n });\n }\n };\n}","map":{"version":3,"names":["n","all","Map","on","t","e","i","get","push","set","off","splice","indexOf","emit","slice","map"],"sources":["../src/index.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\ttype: keyof T,\n\tevent: 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<\n\tWildcardHandler<T>\n>;\n\n// A map of event types and their corresponding event handlers.\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n\tkeyof Events | '*',\n\tEventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>\n>;\n\nexport interface Emitter<Events extends Record<EventType, unknown>> {\n\tall: EventHandlerMap<Events>;\n\n\ton<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;\n\ton(type: '*', handler: WildcardHandler<Events>): void;\n\n\toff<Key extends keyof Events>(\n\t\ttype: Key,\n\t\thandler?: Handler<Events[Key]>\n\t): void;\n\toff(type: '*', handler: WildcardHandler<Events>): void;\n\n\temit<Key extends keyof Events>(type: Key, event: Events[Key]): void;\n\temit<Key extends keyof Events>(\n\t\ttype: undefined extends Events[Key] ? Key : never\n\t): 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>>(\n\tall?: EventHandlerMap<Events>\n): Emitter<Events> {\n\ttype GenericEventHandler =\n\t\t| Handler<Events[keyof Events]>\n\t\t| WildcardHandler<Events>;\n\tall = all || new Map();\n\n\treturn {\n\t\t/**\n\t\t * A Map of event names to registered handler functions.\n\t\t */\n\t\tall,\n\n\t\t/**\n\t\t * Register an event handler for the given type.\n\t\t * @param {string|symbol} type Type of event to listen for, or `'*'` for all events\n\t\t * @param {Function} handler Function to call in response to given event\n\t\t * @memberOf mitt\n\t\t */\n\t\ton<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {\n\t\t\tconst handlers: Array<GenericEventHandler> | undefined = all!.get(type);\n\t\t\tif (handlers) {\n\t\t\t\thandlers.push(handler);\n\t\t\t} else {\n\t\t\t\tall!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);\n\t\t\t}\n\t\t},\n\n\t\t/**\n\t\t * Remove an event handler for the given type.\n\t\t * If `handler` is omitted, all handlers of the given type are removed.\n\t\t * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)\n\t\t * @param {Function} [handler] Handler function to remove\n\t\t * @memberOf mitt\n\t\t */\n\t\toff<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {\n\t\t\tconst handlers: Array<GenericEventHandler> | undefined = all!.get(type);\n\t\t\tif (handlers) {\n\t\t\t\tif (handler) {\n\t\t\t\t\thandlers.splice(handlers.indexOf(handler) >>> 0, 1);\n\t\t\t\t} else {\n\t\t\t\t\tall!.set(type, []);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\t/**\n\t\t * Invoke all handlers for the given type.\n\t\t * If present, `'*'` handlers are invoked after type-matched handlers.\n\t\t *\n\t\t * Note: Manually firing '*' handlers is not supported.\n\t\t *\n\t\t * @param {string|symbol} type The event type to invoke\n\t\t * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler\n\t\t * @memberOf mitt\n\t\t */\n\t\temit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {\n\t\t\tlet handlers = all!.get(type);\n\t\t\tif (handlers) {\n\t\t\t\t(handlers as EventHandlerList<Events[keyof Events]>)\n\t\t\t\t\t.slice()\n\t\t\t\t\t.map((handler) => {\n\t\t\t\t\t\thandler(evt!);\n\t\t\t\t\t});\n\t\t\t}\n\n\t\t\thandlers = all!.get('*');\n\t\t\tif (handlers) {\n\t\t\t\t(handlers as WildCardEventHandlerList<Events>)\n\t\t\t\t\t.slice()\n\t\t\t\t\t.map((handler) => {\n\t\t\t\t\t\thandler(type, evt!);\n\t\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n}\n"],"mappings":";yBA8CCA,CAAA;EAOA,OAAO;IAINC,GAAA,EANDD,CAAA,GAAMA,CAAA,IAAO,IAAIE,GAAA;IAchBC,EAAA,WAAAA,CAA6BC,CAAA,EAAWC,CAAA;MACvC,IAAMC,CAAA,GAAmDN,CAAA,CAAKO,GAAA,CAAIH,CAAA;MAC9DE,CAAA,GACHA,CAAA,CAASE,IAAA,CAAKH,CAAA,IAEdL,CAAA,CAAKS,GAAA,CAAIL,CAAA,EAAM,CAACC,CAAA;IAAA;IAWlBK,GAAA,WAAAA,CAA8BN,CAAA,EAAWC,CAAA;MACxC,IAAMC,CAAA,GAAmDN,CAAA,CAAKO,GAAA,CAAIH,CAAA;MAC9DE,CAAA,KACCD,CAAA,GACHC,CAAA,CAASK,MAAA,CAAOL,CAAA,CAASM,OAAA,CAAQP,CAAA,MAAa,GAAG,KAEjDL,CAAA,CAAKS,GAAA,CAAIL,CAAA,EAAM;IAAA;IAelBS,IAAA,WAAAA,CAA+BT,CAAA,EAAWC,CAAA;MACzC,IAAIC,CAAA,GAAWN,CAAA,CAAKO,GAAA,CAAIH,CAAA;MACpBE,CAAA,IACFA,CAAA,CACCQ,KAAA,GACAC,GAAA,CAAI,UAACf,CAAA;QACLA,CAAA,CAAQK,CAAA;MAAA,KAIXC,CAAA,GAAWN,CAAA,CAAKO,GAAA,CAAI,SAElBD,CAAA,CACCQ,KAAA,GACAC,GAAA,CAAI,UAACf,CAAA;QACLA,CAAA,CAAQI,CAAA,EAAMC,CAAA;MAAA;IAAA;EAAA;AAAA"},"metadata":{},"sourceType":"module","externalDependencies":[]}
|