{"version":3,"file":"mitt.mjs","names":[],"sources":["../src/mitt.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Mitt - Tiny (~200b) functional event emitter / pubsub.\n *\n * Vendored from: https://github.com/developit/mitt\n * License: MIT\n * Copyright (c) 2021 Jason Miller\n */\n\nexport 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<WildcardHandler<T>>;\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>(type: Key, handler?: Handler<Events[Key]>): 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>(type: undefined extends Events[Key] ? Key : never): 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 = Handler<Events[keyof Events]> | 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 type Type of event to listen for, or `'*'` for all events\n\t\t * @param handler Function to call in response to given event\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 type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)\n\t\t * @param handler Handler function to remove\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 type The event type to invoke\n\t\t * @param evt Any value (object is recommended and powerful), passed to each handler\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]>).slice().map((handler) => {\n\t\t\t\t\thandler(evt!);\n\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>).slice().map((handler) => {\n\t\t\t\t\thandler(type, evt!);\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;AAiDA,SAAwB,KACvB,KACkB;AAElB,OAAM,uBAAO,IAAI,KAAK;AAEtB,QAAO;EAIN;EAOA,GAA6B,MAAW,SAA8B;GACrE,MAAM,WAAmD,IAAK,IAAI,KAAK;AACvE,OAAI,SACH,UAAS,KAAK,QAAQ;OAEtB,KAAK,IAAI,MAAM,CAAC,QAAQ,CAA2C;;EAUrE,IAA8B,MAAW,SAA+B;GACvE,MAAM,WAAmD,IAAK,IAAI,KAAK;AACvE,OAAI,SACH,KAAI,QACH,UAAS,OAAO,SAAS,QAAQ,QAAQ,KAAK,GAAG,EAAE;OAEnD,KAAK,IAAI,MAAM,EAAE,CAAC;;EAcrB,KAA+B,MAAW,KAAmB;GAC5D,IAAI,WAAW,IAAK,IAAI,KAAK;AAC7B,OAAI,SACH,CAAC,SAAoD,OAAO,CAAC,KAAK,YAAY;AAC7E,YAAQ,IAAK;KACZ;AAGH,cAAW,IAAK,IAAI,IAAI;AACxB,OAAI,SACH,CAAC,SAA8C,OAAO,CAAC,KAAK,YAAY;AACvE,YAAQ,MAAM,IAAK;KAClB;;EAGJ"}