@rg-dev/tzevaadom-api 1.0.0 → 1.0.2

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.
Files changed (2) hide show
  1. package/dist/index.d.cts +612 -0
  2. package/package.json +6 -6
package/dist/index.d.cts CHANGED
@@ -1,5 +1,617 @@
1
+ /**
2
+ Emittery accepts strings, symbols, and numbers as event names.
3
+
4
+ Symbol event names are preferred given that they can be used to avoid name collisions when your classes are extended, especially for internal events.
5
+ */
6
+ type EventName = PropertyKey;
7
+
8
+ // Helper type for turning the passed `EventData` type map into a list of string keys that don't require data alongside the event name when emitting. Uses the same trick that `Omit` does internally to filter keys by building a map of keys to keys we want to keep, and then accessing all the keys to return just the list of keys we want to keep.
9
+ type DatalessEventNames<EventData> = {
10
+ [Key in keyof EventData]: EventData[Key] extends undefined ? Key : never;
11
+ }[keyof EventData];
12
+
13
+ declare const listenerAdded: unique symbol;
14
+ declare const listenerRemoved: unique symbol;
15
+ type OmnipresentEventData = {[listenerAdded]: ListenerChangedData; [listenerRemoved]: ListenerChangedData};
16
+
17
+ /**
18
+ Emittery can collect and log debug information.
19
+
20
+ To enable this feature set the `DEBUG` environment variable to `emittery` or `*`. Additionally, you can set the static `isDebugEnabled` variable to true on the Emittery class, or `myEmitter.debug.enabled` on an instance of it for debugging a single instance.
21
+
22
+ See API for more information on how debugging works.
23
+ */
24
+ type DebugLogger<EventData, Name extends keyof EventData> = (type: string, debugName: string, eventName?: Name, eventData?: EventData[Name]) => void;
25
+
26
+ /**
27
+ Configure debug options of an instance.
28
+ */
29
+ type DebugOptions<EventData> = {
30
+ /**
31
+ Define a name for the instance of Emittery to use when outputting debug data.
32
+
33
+ @default undefined
34
+
35
+ @example
36
+ ```
37
+ import Emittery from 'emittery';
38
+
39
+ Emittery.isDebugEnabled = true;
40
+
41
+ const emitter = new Emittery({debug: {name: 'myEmitter'}});
42
+
43
+ emitter.on('test', data => {
44
+ // …
45
+ });
46
+
47
+ emitter.emit('test');
48
+ //=> [16:43:20.417][emittery:subscribe][myEmitter] Event Name: test
49
+ // data: undefined
50
+ ```
51
+ */
52
+ readonly name: string;
53
+
54
+ /**
55
+ Toggle debug logging just for this instance.
56
+
57
+ @default false
58
+
59
+ @example
60
+ ```
61
+ import Emittery from 'emittery';
62
+
63
+ const emitter1 = new Emittery({debug: {name: 'emitter1', enabled: true}});
64
+ const emitter2 = new Emittery({debug: {name: 'emitter2'}});
65
+
66
+ emitter1.on('test', data => {
67
+ // …
68
+ });
69
+
70
+ emitter2.on('test', data => {
71
+ // …
72
+ });
73
+
74
+ emitter1.emit('test');
75
+ //=> [16:43:20.417][emittery:subscribe][emitter1] Event Name: test
76
+ // data: undefined
77
+
78
+ emitter2.emit('test');
79
+ ```
80
+ */
81
+ readonly enabled?: boolean;
82
+
83
+ /**
84
+ Function that handles debug data.
85
+
86
+ @default
87
+ ```
88
+ (type, debugName, eventName, eventData) => {
89
+ eventData = JSON.stringify(eventData);
90
+
91
+ if (typeof eventName === 'symbol' || typeof eventName === 'number') {
92
+ eventName = eventName.toString();
93
+ }
94
+
95
+ const currentTime = new Date();
96
+ const logTime = `${currentTime.getHours()}:${currentTime.getMinutes()}:${currentTime.getSeconds()}.${currentTime.getMilliseconds()}`;
97
+ console.log(`[${logTime}][emittery:${type}][${debugName}] Event Name: ${eventName}\n\tdata: ${eventData}`);
98
+ }
99
+ ```
100
+
101
+ @example
102
+ ```
103
+ import Emittery from 'emittery';
104
+
105
+ const myLogger = (type, debugName, eventName, eventData) => {
106
+ console.log(`[${type}]: ${eventName}`);
107
+ };
108
+
109
+ const emitter = new Emittery({
110
+ debug: {
111
+ name: 'myEmitter',
112
+ enabled: true,
113
+ logger: myLogger
114
+ }
115
+ });
116
+
117
+ emitter.on('test', data => {
118
+ // …
119
+ });
120
+
121
+ emitter.emit('test');
122
+ //=> [subscribe]: test
123
+ ```
124
+ */
125
+ readonly logger?: DebugLogger<EventData, keyof EventData>;
126
+ };
127
+
128
+ /**
129
+ Configuration options for Emittery.
130
+ */
131
+ type Options<EventData> = {
132
+ readonly debug?: DebugOptions<EventData>;
133
+ };
134
+
135
+ /**
136
+ A promise returned from `emittery.once` with an extra `off` method to cancel your subscription.
137
+ */
138
+ type EmitteryOncePromise<T> = {
139
+ off(): void;
140
+ } & Promise<T>;
141
+
142
+ /**
143
+ Removes an event subscription.
144
+ */
145
+ type UnsubscribeFunction = () => void;
146
+
147
+ /**
148
+ The data provided as `eventData` when listening for `Emittery.listenerAdded` or `Emittery.listenerRemoved`.
149
+ */
150
+ type ListenerChangedData = {
151
+ /**
152
+ The listener that was added or removed.
153
+ */
154
+ listener: (eventData?: unknown) => (void | Promise<void>);
155
+
156
+ /**
157
+ The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
158
+ */
159
+ eventName?: EventName;
160
+ };
161
+
162
+ /**
163
+ Emittery is a strictly typed, fully async EventEmitter implementation. Event listeners can be registered with `on` or `once`, and events can be emitted with `emit`.
164
+
165
+ `Emittery` has a generic `EventData` type that can be provided by users to strongly type the list of events and the data passed to the listeners for those events. Pass an interface of {[eventName]: undefined | <eventArg>}, with all the event names as the keys and the values as the type of the argument passed to listeners if there is one, or `undefined` if there isn't.
166
+
167
+ @example
168
+ ```
1
169
  import Emittery from 'emittery';
2
170
 
171
+ const emitter = new Emittery<
172
+ // Pass `{[eventName: <string | symbol | number>]: undefined | <eventArg>}` as the first type argument for events that pass data to their listeners.
173
+ // A value of `undefined` in this map means the event listeners should expect no data, and a type other than `undefined` means the listeners will receive one argument of that type.
174
+ {
175
+ open: string,
176
+ close: undefined
177
+ }
178
+ >();
179
+
180
+ // Typechecks just fine because the data type for the `open` event is `string`.
181
+ emitter.emit('open', 'foo\n');
182
+
183
+ // Typechecks just fine because `close` is present but points to undefined in the event data type map.
184
+ emitter.emit('close');
185
+
186
+ // TS compilation error because `1` isn't assignable to `string`.
187
+ emitter.emit('open', 1);
188
+
189
+ // TS compilation error because `other` isn't defined in the event data type map.
190
+ emitter.emit('other');
191
+ ```
192
+ */
193
+ declare class Emittery<
194
+ EventData = Record<EventName, any>, // TODO: Use `unknown` instead of `any`.
195
+ AllEventData = EventData & OmnipresentEventData,
196
+ DatalessEvents = DatalessEventNames<EventData>,
197
+ > {
198
+ /**
199
+ Toggle debug mode for all instances.
200
+
201
+ Default: `true` if the `DEBUG` environment variable is set to `emittery` or `*`, otherwise `false`.
202
+
203
+ @example
204
+ ```
205
+ import Emittery from 'emittery';
206
+
207
+ Emittery.isDebugEnabled = true;
208
+
209
+ const emitter1 = new Emittery({debug: {name: 'myEmitter1'}});
210
+ const emitter2 = new Emittery({debug: {name: 'myEmitter2'}});
211
+
212
+ emitter1.on('test', data => {
213
+ // …
214
+ });
215
+
216
+ emitter2.on('otherTest', data => {
217
+ // …
218
+ });
219
+
220
+ emitter1.emit('test');
221
+ //=> [16:43:20.417][emittery:subscribe][myEmitter1] Event Name: test
222
+ // data: undefined
223
+
224
+ emitter2.emit('otherTest');
225
+ //=> [16:43:20.417][emittery:subscribe][myEmitter2] Event Name: otherTest
226
+ // data: undefined
227
+ ```
228
+ */
229
+ static isDebugEnabled: boolean;
230
+
231
+ /**
232
+ Fires when an event listener was added.
233
+
234
+ An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
235
+
236
+ @example
237
+ ```
238
+ import Emittery from 'emittery';
239
+
240
+ const emitter = new Emittery();
241
+
242
+ emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
243
+ console.log(listener);
244
+ //=> data => {}
245
+
246
+ console.log(eventName);
247
+ //=> '🦄'
248
+ });
249
+
250
+ emitter.on('🦄', data => {
251
+ // Handle data
252
+ });
253
+ ```
254
+ */
255
+ static readonly listenerAdded: typeof listenerAdded;
256
+
257
+ /**
258
+ Fires when an event listener was removed.
259
+
260
+ An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
261
+
262
+ @example
263
+ ```
264
+ import Emittery from 'emittery';
265
+
266
+ const emitter = new Emittery();
267
+
268
+ const off = emitter.on('🦄', data => {
269
+ // Handle data
270
+ });
271
+
272
+ emitter.on(Emittery.listenerRemoved, ({listener, eventName}) => {
273
+ console.log(listener);
274
+ //=> data => {}
275
+
276
+ console.log(eventName);
277
+ //=> '🦄'
278
+ });
279
+
280
+ off();
281
+ ```
282
+ */
283
+ static readonly listenerRemoved: typeof listenerRemoved;
284
+
285
+ /**
286
+ In TypeScript, it returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
287
+
288
+ @example
289
+ ```
290
+ import Emittery from 'emittery';
291
+
292
+ @Emittery.mixin('emittery')
293
+ class MyClass {}
294
+
295
+ const instance = new MyClass();
296
+
297
+ instance.emit('event');
298
+ ```
299
+ */
300
+ static mixin(
301
+ emitteryPropertyName: string | symbol,
302
+ methodNames?: readonly string[]
303
+ ): <T extends {new (...arguments_: readonly any[]): any}>(klass: T) => T; // eslint-disable-line @typescript-eslint/prefer-function-type
304
+
305
+ /**
306
+ Debugging options for the current instance.
307
+ */
308
+ debug: DebugOptions<EventData>;
309
+
310
+ /**
311
+ Create a new Emittery instance with the specified options.
312
+
313
+ @returns An instance of Emittery that you can use to listen for and emit events.
314
+ */
315
+ constructor(options?: Options<EventData>);
316
+
317
+ /**
318
+ Subscribe to one or more events.
319
+
320
+ Using the same listener multiple times for the same event will result in only one method call per emitted event.
321
+
322
+ @returns An unsubscribe method.
323
+
324
+ @example
325
+ ```
326
+ import Emittery from 'emittery';
327
+
328
+ const emitter = new Emittery();
329
+
330
+ emitter.on('🦄', data => {
331
+ console.log(data);
332
+ });
333
+
334
+ emitter.on(['🦄', '🐶'], data => {
335
+ console.log(data);
336
+ });
337
+
338
+ emitter.emit('🦄', '🌈'); // log => '🌈' x2
339
+ emitter.emit('🐶', '🍖'); // log => '🍖'
340
+ ```
341
+ */
342
+ on<Name extends keyof AllEventData>(
343
+ eventName: Name | readonly Name[],
344
+ listener: (eventData: AllEventData[Name]) => void | Promise<void>,
345
+ options?: {signal?: AbortSignal}
346
+ ): UnsubscribeFunction;
347
+
348
+ /**
349
+ Get an async iterator which buffers data each time an event is emitted.
350
+
351
+ Call `return()` on the iterator to remove the subscription.
352
+
353
+ @example
354
+ ```
355
+ import Emittery from 'emittery';
356
+
357
+ const emitter = new Emittery();
358
+ const iterator = emitter.events('🦄');
359
+
360
+ emitter.emit('🦄', '🌈1'); // Buffered
361
+ emitter.emit('🦄', '🌈2'); // Buffered
362
+
363
+ iterator
364
+ .next()
365
+ .then(({value, done}) => {
366
+ // done === false
367
+ // value === '🌈1'
368
+ return iterator.next();
369
+ })
370
+ .then(({value, done}) => {
371
+ // done === false
372
+ // value === '🌈2'
373
+ // Revoke subscription
374
+ return iterator.return();
375
+ })
376
+ .then(({done}) => {
377
+ // done === true
378
+ });
379
+ ```
380
+
381
+ In practice you would usually consume the events using the [for await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) statement. In that case, to revoke the subscription simply break the loop.
382
+
383
+ @example
384
+ ```
385
+ import Emittery from 'emittery';
386
+
387
+ const emitter = new Emittery();
388
+ const iterator = emitter.events('🦄');
389
+
390
+ emitter.emit('🦄', '🌈1'); // Buffered
391
+ emitter.emit('🦄', '🌈2'); // Buffered
392
+
393
+ // In an async context.
394
+ for await (const data of iterator) {
395
+ if (data === '🌈2') {
396
+ break; // Revoke the subscription when we see the value `🌈2`.
397
+ }
398
+ }
399
+ ```
400
+
401
+ It accepts multiple event names.
402
+
403
+ @example
404
+ ```
405
+ import Emittery from 'emittery';
406
+
407
+ const emitter = new Emittery();
408
+ const iterator = emitter.events(['🦄', '🦊']);
409
+
410
+ emitter.emit('🦄', '🌈1'); // Buffered
411
+ emitter.emit('🦊', '🌈2'); // Buffered
412
+
413
+ iterator
414
+ .next()
415
+ .then(({value, done}) => {
416
+ // done === false
417
+ // value === '🌈1'
418
+ return iterator.next();
419
+ })
420
+ .then(({value, done}) => {
421
+ // done === false
422
+ // value === '🌈2'
423
+ // Revoke subscription
424
+ return iterator.return();
425
+ })
426
+ .then(({done}) => {
427
+ // done === true
428
+ });
429
+ ```
430
+ */
431
+ events<Name extends keyof EventData>(
432
+ eventName: Name | readonly Name[]
433
+ ): AsyncIterableIterator<EventData[Name]>;
434
+
435
+ /**
436
+ Remove one or more event subscriptions.
437
+
438
+ @example
439
+ ```
440
+ import Emittery from 'emittery';
441
+
442
+ const emitter = new Emittery();
443
+
444
+ const listener = data => {
445
+ console.log(data);
446
+ };
447
+
448
+ emitter.on(['🦄', '🐶', '🦊'], listener);
449
+ await emitter.emit('🦄', 'a');
450
+ await emitter.emit('🐶', 'b');
451
+ await emitter.emit('🦊', 'c');
452
+ emitter.off('🦄', listener);
453
+ emitter.off(['🐶', '🦊'], listener);
454
+ await emitter.emit('🦄', 'a'); // nothing happens
455
+ await emitter.emit('🐶', 'b'); // nothing happens
456
+ await emitter.emit('🦊', 'c'); // nothing happens
457
+ ```
458
+ */
459
+ off<Name extends keyof AllEventData>(
460
+ eventName: Name | readonly Name[],
461
+ listener: (eventData: AllEventData[Name]) => void | Promise<void>
462
+ ): void;
463
+
464
+ /**
465
+ Subscribe to one or more events only once. It will be unsubscribed after the first event that matches the predicate (if provided).
466
+
467
+ @param eventName - The event name(s) to subscribe to.
468
+ @param predicate - Optional predicate function to filter event data. The event will only be emitted if the predicate returns true.
469
+
470
+ @returns The promise of event data when `eventName` is emitted and predicate matches (if provided). This promise is extended with an `off` method.
471
+
472
+ @example
473
+ ```
474
+ import Emittery from 'emittery';
475
+
476
+ const emitter = new Emittery();
477
+
478
+ emitter.once('🦄').then(data => {
479
+ console.log(data);
480
+ //=> '🌈'
481
+ });
482
+
483
+ emitter.once(['🦄', '🐶']).then(data => {
484
+ console.log(data);
485
+ });
486
+
487
+ // With predicate
488
+ emitter.once('data', data => data.ok === true).then(data => {
489
+ console.log(data);
490
+ //=> {ok: true, value: 42}
491
+ });
492
+
493
+ emitter.emit('🦄', '🌈'); // Logs `🌈` twice
494
+ emitter.emit('🐶', '🍖'); // Nothing happens
495
+ emitter.emit('data', {ok: false}); // Nothing happens
496
+ emitter.emit('data', {ok: true, value: 42}); // Logs {ok: true, value: 42}
497
+ ```
498
+ */
499
+ once<Name extends keyof AllEventData>(eventName: Name | readonly Name[], predicate?: (eventData: AllEventData[Name]) => boolean): EmitteryOncePromise<AllEventData[Name]>;
500
+
501
+ /**
502
+ Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
503
+
504
+ @returns A promise that resolves when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
505
+ */
506
+ emit<Name extends DatalessEvents>(eventName: Name): Promise<void>;
507
+ emit<Name extends keyof EventData>(
508
+ eventName: Name,
509
+ eventData: EventData[Name]
510
+ ): Promise<void>;
511
+
512
+ /**
513
+ Same as `emit()`, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.
514
+
515
+ If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will *not* be called.
516
+
517
+ @returns A promise that resolves when all the event listeners are done.
518
+ */
519
+ emitSerial<Name extends DatalessEvents>(eventName: Name): Promise<void>;
520
+ emitSerial<Name extends keyof EventData>(
521
+ eventName: Name,
522
+ eventData: EventData[Name]
523
+ ): Promise<void>;
524
+
525
+ /**
526
+ Subscribe to be notified about any event.
527
+
528
+ @returns A method to unsubscribe.
529
+ */
530
+ onAny(
531
+ listener: (
532
+ eventName: keyof EventData,
533
+ eventData: EventData[keyof EventData]
534
+ ) => void | Promise<void>,
535
+ options?: {signal?: AbortSignal}
536
+ ): UnsubscribeFunction;
537
+
538
+ /**
539
+ Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
540
+
541
+ Call `return()` on the iterator to remove the subscription.
542
+
543
+ In the same way as for `events`, you can subscribe by using the `for await` statement.
544
+
545
+ @example
546
+ ```
547
+ import Emittery from 'emittery';
548
+
549
+ const emitter = new Emittery();
550
+ const iterator = emitter.anyEvent();
551
+
552
+ emitter.emit('🦄', '🌈1'); // Buffered
553
+ emitter.emit('🌟', '🌈2'); // Buffered
554
+
555
+ iterator.next()
556
+ .then(({value, done}) => {
557
+ // done is false
558
+ // value is ['🦄', '🌈1']
559
+ return iterator.next();
560
+ })
561
+ .then(({value, done}) => {
562
+ // done is false
563
+ // value is ['🌟', '🌈2']
564
+ // revoke subscription
565
+ return iterator.return();
566
+ })
567
+ .then(({done}) => {
568
+ // done is true
569
+ });
570
+ ```
571
+ */
572
+ anyEvent(): AsyncIterableIterator<
573
+ [keyof EventData, EventData[keyof EventData]]
574
+ >;
575
+
576
+ /**
577
+ Remove an `onAny` subscription.
578
+ */
579
+ offAny(
580
+ listener: (
581
+ eventName: keyof EventData,
582
+ eventData: EventData[keyof EventData]
583
+ ) => void | Promise<void>
584
+ ): void;
585
+
586
+ /**
587
+ Clear all event listeners on the instance.
588
+
589
+ If `eventName` is given, only the listeners for that event are cleared.
590
+ */
591
+ clearListeners<Name extends keyof EventData>(eventName?: Name | readonly Name[]): void;
592
+
593
+ /**
594
+ The number of listeners for the `eventName` or all events if not specified.
595
+ */
596
+ listenerCount<Name extends keyof EventData>(eventName?: Name | readonly Name[]): number;
597
+
598
+ /**
599
+ Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
600
+
601
+ @example
602
+ ```
603
+ import Emittery from 'emittery';
604
+
605
+ const object = {};
606
+
607
+ new Emittery().bindMethods(object);
608
+
609
+ object.emit('event');
610
+ ```
611
+ */
612
+ bindMethods(target: Record<string, unknown>, methodNames?: readonly string[]): void;
613
+ }
614
+
3
615
  interface AlertData {
4
616
  notificationId: string;
5
617
  time: number;
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
1
  {
2
2
  "name": "@rg-dev/tzevaadom-api",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "scripts": {
6
- "test": "tsup && node dist/index.cjs",
6
+ "test": "tsup && node dist/index.cts",
7
7
  "build": "tsup"
8
8
  },
9
9
  "author": "",
10
10
  "license": "ISC",
11
- "main": "./dist/index.cjs",
11
+ "main": "./dist/index.cts",
12
12
  "files": [
13
13
  "dist"
14
14
  ],
15
15
  "module": "./dist/index.js",
16
- "types": "./dist/index.d.ts",
16
+ "types": "./dist/index.d.cts",
17
17
  "type": "module",
18
18
  "exports": {
19
19
  ".": {
20
- "import": "./dist/index.js",
21
- "require": "./dist/index.cjs"
20
+ "import": "./dist/index.cts",
21
+ "require": "./dist/index.cts"
22
22
  }
23
23
  },
24
24
  "devDependencies": {