@stacksjs/events 0.61.24 → 0.63.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -61,3 +61,6 @@ export {
61
61
  mitt as default,
62
62
  all
63
63
  };
64
+
65
+ //# debugId=D06D3393B578A3BB64756E2164756E21
66
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "sourcesContent": [
5
+ "// thanks to mitt for the base of this wonderful functional event emitter\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>> = (type: keyof T, event: T[keyof T]) => 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 keyof Events | '*',\n EventHandlerList<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 ((type: '*', handler: WildcardHandler<Events>) => void)\n\n off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) &\n ((type: '*', handler?: WildcardHandler<Events>) => void)\n\n emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) &\n (<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never) => void)\n}\n\n/**\n * Tiny (~200b) functional event emitter / pubsub.\n */\nexport default function mitt<Events extends Record<EventType, unknown>>(\n all?: EventHandlerMap<Events>,\n): Emitter<Events> {\n if (!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: Handler<Events[Key]> | WildcardHandler<Events>) {\n const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (\n all as EventHandlerMap<Events>\n ).get(type)\n if (handlers) handlers.push(handler as Handler<Events[Key]> | WildcardHandler<Events>)\n // Explicitly assert the type of the handler array being set in the map. Unsure if there is a better way to do this\n else\n (all as EventHandlerMap<Events>).set(type, [handler] as\n | EventHandlerList<Events[keyof Events]>\n | WildCardEventHandlerList<Events>)\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?: Handler<Events[Key]> | WildcardHandler<Events>) {\n const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (\n all as EventHandlerMap<Events>\n ).get(type)\n if (handlers) {\n if (handler) {\n const index = handlers.indexOf(handler as Handler<Events[Key]> | WildcardHandler<Events>)\n if (index > -1) handlers.splice(index, 1)\n } else {\n ;(all as EventHandlerMap<Events>).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 as EventHandlerMap<Events>).get(type)\n if (handlers) {\n ;(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => {\n if (evt !== undefined) handler(evt)\n })\n }\n handlers = (all as EventHandlerMap<Events>).get('*')\n if (handlers) {\n ;(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {\n if (evt !== undefined) handler(type, evt)\n })\n }\n },\n }\n}\n\n/**\n * This module provides a simple, yet powerful, event bus for the application.\n *\n * TODO: https://vitejs.dev/guide/api-plugin.html#typescript-for-custom-events\n *\n * @example To fire an event, you may use any of the following approaches:\n * ```ts\n * useEvent('user:registered', { name: 'Chris'})\n * dispatch('user:registered', { name: 'Chris'})\n *\n * // alternatively, you may use the following:\n * events.emit('user:registered', { name: 'Chris'})\n * useEvents.emit('user:registered', { name: 'Chris'})\n * ````\n *\n * @example To capture an event, you may use any of the following approaches:\n * ```ts\n * useListen('user:registered', (user) => console.log(user))\n * listen('user:registered', (user) => console.log(user))\n * ```\n */\n\n// TODO: need to create an action that auto generates this Events type from the ./app/Events\ntype StacksEvents = {\n 'user:registered': { name: string }\n 'user:logged-in': { name: string }\n 'user:logged-out': { name: string }\n 'user:updated': { name: string }\n 'user:deleted': { name: string }\n 'user:password-reset': { name: string }\n 'user:password-changed': { name: string }\n}\n\nconst events = mitt<StacksEvents>\nconst emitter = events()\nconst useEvent: typeof emitter.emit = emitter.emit.bind(emitter)\nconst useEvents = events()\nconst dispatch: typeof emitter.emit = emitter.emit.bind(emitter)\nconst listen: typeof emitter.on = emitter.on.bind(emitter)\nconst off: typeof emitter.off = emitter.off.bind(emitter)\nconst all: typeof emitter.all = emitter.all\n\nexport { all, dispatch, events, listen, mitt, off, useEvent, useEvents }\n"
6
+ ],
7
+ "mappings": ";;AAmCA,SAAwB,IAA+C,CACrE,KACiB;AACjB,OAAK;AAAK,UAAM,IAAI;AAEpB,SAAO;AAAA,IAIL;AAAA,IAQA,EAA4B,CAAC,MAAiB,SAAyD;AACrG,YAAM,WACJ,IACA,IAAI,IAAI;AACV,UAAI;AAAU,iBAAS,KAAK,OAAyD;AAAA;AAGnF,QAAC,IAAgC,IAAI,MAAM,CAAC,OAAO,CAEf;AAAA;AAAA,IAUxC,GAA6B,CAAC,MAAiB,SAA0D;AACvG,YAAM,WACJ,IACA,IAAI,IAAI;AACV,UAAI,UAAU;AACZ,YAAI,SAAS;AACX,gBAAM,QAAQ,SAAS,QAAQ,OAAyD;AACxF,cAAI,QAAQ;AAAI,qBAAS,OAAO,OAAO,CAAC;AAAA,QAC1C,OAAO;AACJ,UAAC,IAAgC,IAAI,MAAM,CAAC,CAAC;AAAA;AAAA,MAElD;AAAA;AAAA,IAaF,IAA8B,CAAC,MAAW,KAAmB;AAC3D,UAAI,WAAY,IAAgC,IAAI,IAAI;AACxD,UAAI,UAAU;AACX,QAAC,SAAoD,MAAM,EAAE,QAAQ,CAAC,YAAY;AACjF,cAAI,QAAQ;AAAW,oBAAQ,GAAG;AAAA,SACnC;AAAA,MACH;AACA,iBAAY,IAAgC,IAAI,GAAG;AACnD,UAAI,UAAU;AACX,QAAC,SAA8C,MAAM,EAAE,QAAQ,CAAC,YAAY;AAC3E,cAAI,QAAQ;AAAW,oBAAQ,MAAM,GAAG;AAAA,SACzC;AAAA,MACH;AAAA;AAAA,EAEJ;AAAA;AAoCF,IAAM,SAAS;AACf,IAAM,UAAU,OAAO;AACvB,IAAM,WAAgC,QAAQ,KAAK,KAAK,OAAO;AAC/D,IAAM,YAAY,OAAO;AACzB,IAAM,WAAgC,QAAQ,KAAK,KAAK,OAAO;AAC/D,IAAM,SAA4B,QAAQ,GAAG,KAAK,OAAO;AACzD,IAAM,MAA0B,QAAQ,IAAI,KAAK,OAAO;AACxD,IAAM,MAA0B,QAAQ;",
8
+ "debugId": "D06D3393B578A3BB64756E2164756E21",
9
+ "names": []
10
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/events",
3
3
  "type": "module",
4
- "version": "0.61.24",
4
+ "version": "0.63.0",
5
5
  "description": "Functional event emitting.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",