emittery 1.1.0 → 1.2.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/index.d.ts CHANGED
@@ -462,10 +462,12 @@ export default class Emittery<
462
462
  ): void;
463
463
 
464
464
  /**
465
- Subscribe to one or more events only once. It will be unsubscribed after the first
466
- event.
465
+ Subscribe to one or more events only once. It will be unsubscribed after the first event that matches the predicate (if provided).
467
466
 
468
- @returns The promise of event data when `eventName` is emitted. This promise is extended with an `off` method.
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.
469
471
 
470
472
  @example
471
473
  ```
@@ -482,11 +484,19 @@ export default class Emittery<
482
484
  console.log(data);
483
485
  });
484
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
+
485
493
  emitter.emit('🦄', '🌈'); // Logs `🌈` twice
486
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}
487
497
  ```
488
498
  */
489
- once<Name extends keyof AllEventData>(eventName: Name | readonly Name[]): EmitteryOncePromise<AllEventData[Name]>;
499
+ once<Name extends keyof AllEventData>(eventName: Name | readonly Name[], predicate?: (eventData: AllEventData[Name]) => boolean): EmitteryOncePromise<AllEventData[Name]>;
490
500
 
491
501
  /**
492
502
  Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
package/index.js CHANGED
@@ -334,11 +334,19 @@ export default class Emittery {
334
334
  }
335
335
  }
336
336
 
337
- once(eventNames) {
337
+ once(eventNames, predicate) {
338
+ if (predicate !== undefined && typeof predicate !== 'function') {
339
+ throw new TypeError('predicate must be a function');
340
+ }
341
+
338
342
  let off_;
339
343
 
340
344
  const promise = new Promise(resolve => {
341
345
  off_ = this.on(eventNames, data => {
346
+ if (predicate && !predicate(data)) {
347
+ return;
348
+ }
349
+
342
350
  off_();
343
351
  resolve(data);
344
352
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emittery",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Simple and modern async event emitter",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/emittery",
package/readme.md CHANGED
@@ -297,11 +297,11 @@ await emitter.emit('🦊', 'c'); // Nothing happens
297
297
 
298
298
  ##### listener(data)
299
299
 
300
- #### once(eventName | eventName[])
300
+ #### once(eventName | eventName[], predicate?)
301
301
 
302
- Subscribe to one or more events only once. It will be unsubscribed after the first event.
302
+ Subscribe to one or more events only once. It will be unsubscribed after the first event that matches the predicate (if provided).
303
303
 
304
- Returns a promise for the event data when `eventName` is emitted. This promise is extended with an `off` method.
304
+ Returns a promise for the event data when `eventName` is emitted and predicate matches (if provided). This promise is extended with an `off` method.
305
305
 
306
306
  ```js
307
307
  import Emittery from 'emittery';
@@ -317,8 +317,16 @@ emitter.once(['🦄', '🐶']).then(data => {
317
317
  console.log(data);
318
318
  });
319
319
 
320
+ // With predicate
321
+ emitter.once('data', data => data.ok === true).then(data => {
322
+ console.log(data);
323
+ //=> {ok: true, value: 42}
324
+ });
325
+
320
326
  emitter.emit('🦄', '🌈'); // Log => '🌈' x2
321
327
  emitter.emit('🐶', '🍖'); // Nothing happens
328
+ emitter.emit('data', {ok: false}); // Nothing happens
329
+ emitter.emit('data', {ok: true, value: 42}); // Log => {ok: true, value: 42}
322
330
  ```
323
331
 
324
332
  #### events(eventName)