utilful 1.2.0 → 1.2.1

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/README.md CHANGED
@@ -137,7 +137,7 @@ emitter.on('*', (type, e) => console.log(type, e))
137
137
  emitter.emit('foo', { a: 'b' })
138
138
 
139
139
  // Clearing all events
140
- emitter.all.clear()
140
+ emitter.events.clear()
141
141
 
142
142
  // Working with handler references:
143
143
  function onFoo() {}
@@ -5,7 +5,7 @@ type EventHandlerList<T = unknown> = Handler<T>[];
5
5
  type WildCardEventHandlerList<T = Record<string, unknown>> = WildcardHandler<T>[];
6
6
  type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | '*', EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
7
7
  interface Emitter<Events extends Record<EventType, unknown>> {
8
- all: EventHandlerMap<Events>;
8
+ events: EventHandlerMap<Events>;
9
9
  on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
10
10
  on(type: '*', handler: WildcardHandler<Events>): void;
11
11
  off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
@@ -19,6 +19,6 @@ interface Emitter<Events extends Record<EventType, unknown>> {
19
19
  * @remarks Ported from `mitt`.
20
20
  * @see https://github.com/developit/mitt
21
21
  */
22
- declare function createEmitter<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events>;
22
+ declare function createEmitter<Events extends Record<EventType, unknown>>(events?: EventHandlerMap<Events>): Emitter<Events>;
23
23
 
24
24
  export { type Emitter, type EventHandlerList, type EventHandlerMap, type EventType, type Handler, type WildCardEventHandlerList, type WildcardHandler, createEmitter };
package/dist/emitter.d.ts CHANGED
@@ -5,7 +5,7 @@ type EventHandlerList<T = unknown> = Handler<T>[];
5
5
  type WildCardEventHandlerList<T = Record<string, unknown>> = WildcardHandler<T>[];
6
6
  type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | '*', EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
7
7
  interface Emitter<Events extends Record<EventType, unknown>> {
8
- all: EventHandlerMap<Events>;
8
+ events: EventHandlerMap<Events>;
9
9
  on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
10
10
  on(type: '*', handler: WildcardHandler<Events>): void;
11
11
  off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
@@ -19,6 +19,6 @@ interface Emitter<Events extends Record<EventType, unknown>> {
19
19
  * @remarks Ported from `mitt`.
20
20
  * @see https://github.com/developit/mitt
21
21
  */
22
- declare function createEmitter<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events>;
22
+ declare function createEmitter<Events extends Record<EventType, unknown>>(events?: EventHandlerMap<Events>): Emitter<Events>;
23
23
 
24
24
  export { type Emitter, type EventHandlerList, type EventHandlerMap, type EventType, type Handler, type WildCardEventHandlerList, type WildcardHandler, createEmitter };
package/dist/emitter.mjs CHANGED
@@ -1,62 +1,61 @@
1
- function createEmitter(all) {
2
- all ||= /* @__PURE__ */ new Map();
1
+ function createEmitter(events) {
2
+ events ||= /* @__PURE__ */ new Map();
3
3
  return {
4
4
  /**
5
5
  * A Map of event names to registered handler functions.
6
6
  */
7
- all,
7
+ events,
8
8
  /**
9
9
  * Register an event handler for the given type.
10
- * @param {string|symbol} type Type of event to listen for, or `'*'` for all events
11
- * @param {Function} handler Function to call in response to given event
10
+ *
12
11
  * @memberOf createEmitter
13
12
  */
14
13
  on(type, handler) {
15
- const handlers = all.get(type);
14
+ const handlers = events.get(type);
16
15
  if (handlers) {
17
16
  handlers.push(handler);
18
17
  } else {
19
- all.set(type, [handler]);
18
+ events.set(type, [handler]);
20
19
  }
21
20
  },
22
21
  /**
23
22
  * Remove an event handler for the given type.
23
+ *
24
+ * @remarks
24
25
  * If `handler` is omitted, all handlers of the given type are removed.
25
26
  *
26
- * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
27
- * @param {Function} [handler] Handler function to remove
28
27
  * @memberOf createEmitter
29
28
  */
30
29
  off(type, handler) {
31
- const handlers = all.get(type);
30
+ const handlers = events.get(type);
32
31
  if (handlers) {
33
32
  if (handler) {
34
33
  handlers.splice(handlers.indexOf(handler) >>> 0, 1);
35
34
  } else {
36
- all.set(type, []);
35
+ events.set(type, []);
37
36
  }
38
37
  }
39
38
  },
40
39
  /**
41
40
  * Invoke all handlers for the given type.
42
- * If present, `'*'` handlers are invoked after type-matched handlers.
43
41
  *
44
42
  * @remarks
43
+ * If present, `'*'` handlers are invoked after type-matched handlers.
45
44
  * Manually firing '*' handlers is not supported.
46
45
  *
47
- * @param {string|symbol} type The event type to invoke
48
- * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
49
46
  * @memberOf createEmitter
50
47
  */
51
48
  emit(type, evt) {
52
- let handlers = all.get(type);
53
- handlers?.slice().forEach((handler) => {
54
- handler(evt);
55
- });
56
- handlers = all.get("*");
57
- handlers?.slice().forEach((handler) => {
58
- handler(type, evt);
59
- });
49
+ let handlers = events.get(type);
50
+ if (handlers) {
51
+ for (const handler of [...handlers])
52
+ handler(evt);
53
+ }
54
+ handlers = events.get("*");
55
+ if (handlers) {
56
+ for (const handler of [...handlers])
57
+ handler(type, evt);
58
+ }
60
59
  }
61
60
  };
62
61
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilful",
3
3
  "type": "module",
4
- "version": "1.2.0",
4
+ "version": "1.2.1",
5
5
  "packageManager": "pnpm@10.5.2",
6
6
  "description": "A collection of TypeScript utilities",
7
7
  "author": "Johann Schopplich <hello@johannschopplich.com>",