Socket
Socket
Sign inDemoInstall

p-event

Package Overview
Dependencies
2
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.0 to 4.1.0

407

index.d.ts
/// <reference lib="esnext"/>
export type AddRemoveListener<Arguments extends unknown[]> = (
event: string | symbol,
listener: (
...args: Arguments,
) => void
) => void;
declare namespace pEvent {
type AddRemoveListener<EventName extends string | symbol, Arguments extends unknown[]> = (
event: EventName,
listener: (...args: Arguments) => void
) => void;
export interface Emitter<EmittedType extends unknown[]> {
on?: AddRemoveListener<EmittedType>;
addListener?: AddRemoveListener<EmittedType>;
addEventListener?: AddRemoveListener<EmittedType>;
off?: AddRemoveListener<EmittedType>;
removeListener?: AddRemoveListener<EmittedType>;
removeEventListener?: AddRemoveListener<EmittedType>;
}
interface Emitter<EventName extends string | symbol, EmittedType extends unknown[]> {
on?: AddRemoveListener<EventName, EmittedType>;
addListener?: AddRemoveListener<EventName, EmittedType>;
addEventListener?: AddRemoveListener<EventName, EmittedType>;
off?: AddRemoveListener<EventName, EmittedType>;
removeListener?: AddRemoveListener<EventName, EmittedType>;
removeEventListener?: AddRemoveListener<EventName, EmittedType>;
}
export type FilterFunction<ElementType extends unknown[]> = (...args: ElementType) => boolean;
type FilterFunction<ElementType extends unknown[]> = (
...args: ElementType
) => boolean;
export interface CancelablePromise<ResolveType> extends Promise<ResolveType> {
cancel(): void;
}
interface CancelablePromise<ResolveType> extends Promise<ResolveType> {
cancel(): void;
}
/**
* Promisify an event by waiting for it to be emitted.
*
* @param emitter - Event emitter object. Should have either a `.on()`/`.addListener()`/`.addEventListener()` and `.off()`/`.removeListener()`/`.removeEventListener()` method, like the [Node.js `EventEmitter`](https://nodejs.org/api/events.html) and [DOM events](https://developer.mozilla.org/en-US/docs/Web/Events).
* @param event - Name of the event or events to listen to. If the same event is defined both here and in `rejectionEvents`, this one takes priority. **Note**: `event` is a string for a single event type, for example, `'data'`. To listen on multiple events, pass an array of strings, such as `['started', 'stopped']`.
* @returns A `Promise` that is fulfilled when emitter emits an event matching `event`, or rejects if emitter emits any of the events defined in the `rejectionEvents` option. The returned promise has a `.cancel()` method, which when called, removes the event listeners and causes the promise to never be settled.
*/
declare function pEvent<EmittedType extends unknown[]>(
emitter: Emitter<EmittedType>,
event: string | symbol | (string | symbol)[],
options: MultiArgumentsOptions<EmittedType>
): CancelablePromise<EmittedType>;
declare function pEvent<EmittedType>(
emitter: Emitter<[EmittedType]>,
event: string | symbol | (string | symbol)[],
filter: FilterFunction<[EmittedType]>
): CancelablePromise<EmittedType>;
declare function pEvent<EmittedType>(
emitter: Emitter<[EmittedType]>,
event: string | symbol | (string | symbol)[],
options?: Options<[EmittedType]>
): CancelablePromise<EmittedType>;
interface Options<EmittedType extends unknown[]> {
/**
Events that will reject the promise.
export default pEvent;
@default ['error']
*/
readonly rejectionEvents?: (string | symbol)[];
/**
* Wait for multiple event emissions. Returns an array.
*/
export function multiple<EmittedType extends unknown[]>(
emitter: Emitter<EmittedType>,
event: string | symbol | (string | symbol)[],
options: MultipleMultiArgumentsOptions<EmittedType>
): CancelablePromise<EmittedType[]>;
export function multiple<EmittedType>(
emitter: Emitter<[EmittedType]>,
event: string | symbol | (string | symbol)[],
options: MultipleOptions<[EmittedType]>
): CancelablePromise<EmittedType[]>;
/**
By default, the promisified function will only return the first argument from the event callback, which works fine for most APIs. This option can be useful for APIs that return multiple arguments in the callback. Turning this on will make it return an array of all arguments from the callback, instead of just the first argument. This also applies to rejections.
/**
* @returns An [async iterator](http://2ality.com/2016/10/asynchronous-iteration.html) that lets you asynchronously iterate over events of `event` emitted from `emitter`. The iterator ends when `emitter` emits an event matching any of the events defined in `resolutionEvents`, or rejects if `emitter` emits any of the events defined in the `rejectionEvents` option.
*/
export function iterator<EmittedType extends unknown[]>(
emitter: Emitter<EmittedType>,
event: string | symbol | (string | symbol)[],
options: IteratorMultiArgumentsOptions<EmittedType>
): AsyncIterableIterator<EmittedType>;
export function iterator<EmittedType>(
emitter: Emitter<[EmittedType]>,
event: string | symbol | (string | symbol)[],
filter: FilterFunction<[EmittedType]>
): AsyncIterableIterator<EmittedType>;
export function iterator<EmittedType>(
emitter: Emitter<[EmittedType]>,
event: string | symbol | (string | symbol)[],
options?: IteratorOptions<[EmittedType]>
): AsyncIterableIterator<EmittedType>;
@default false
export interface Options<EmittedType extends unknown[]> {
/**
* Events that will reject the promise.
*
* @default ['error']
*/
readonly rejectionEvents?: (string | symbol)[];
@example
```
import pEvent = require('p-event');
import emitter from './some-event-emitter';
/**
* By default, the promisified function will only return the first argument from the event callback, which works fine for most APIs. This option can be useful for APIs that return multiple arguments in the callback. Turning this on will make it return an array of all arguments from the callback, instead of just the first argument. This also applies to rejections.
*
* @default false
*
* @example
*
* const pEvent = require('p-event');
* const emitter = require('./some-event-emitter');
*
* (async () => {
* const [foo, bar] = await pEvent(emitter, 'finish', {multiArgs: true});
* })();
*/
readonly multiArgs?: boolean;
(async () => {
const [foo, bar] = await pEvent(emitter, 'finish', {multiArgs: true});
})();
```
*/
readonly multiArgs?: boolean;
/**
* Time in milliseconds before timing out.
*
* @default Infinity
*/
readonly timeout?: number;
/**
Time in milliseconds before timing out.
/**
* Filter function for accepting an event.
*
* @example
*
* const pEvent = require('p-event');
* const emitter = require('./some-event-emitter');
*
* (async () => {
* const result = await pEvent(emitter, 'πŸ¦„', value => value > 3);
* // Do something with first πŸ¦„ event with a value greater than 3
* })();
*/
readonly filter?: FilterFunction<EmittedType>;
}
@default Infinity
*/
readonly timeout?: number;
export interface MultiArgumentsOptions<EmittedType extends unknown[]> extends Options<EmittedType> {
readonly multiArgs: true;
/**
Filter function for accepting an event.
@example
```
import pEvent = require('p-event');
import emitter from './some-event-emitter';
(async () => {
const result = await pEvent(emitter, 'πŸ¦„', value => value > 3);
// Do something with first πŸ¦„ event with a value greater than 3
})();
```
*/
readonly filter?: FilterFunction<EmittedType>;
}
interface MultiArgumentsOptions<EmittedType extends unknown[]>
extends Options<EmittedType> {
readonly multiArgs: true;
}
interface MultipleOptions<EmittedType extends unknown[]>
extends Options<EmittedType> {
/**
The number of times the event needs to be emitted before the promise resolves.
*/
readonly count: number;
/**
Whether to resolve the promise immediately. Emitting one of the `rejectionEvents` won't throw an error.
__Note__: The returned array will be mutated when an event is emitted.
@example
```
import pEvent = require('p-event');
const emitter = new EventEmitter();
const promise = pEvent.multiple(emitter, 'hello', {
resolveImmediately: true,
count: Infinity
});
const result = await promise;
console.log(result);
//=> []
emitter.emit('hello', 'Jack');
console.log(result);
//=> ['Jack']
emitter.emit('hello', 'Mark');
console.log(result);
//=> ['Jack', 'Mark']
// Stops listening
emitter.emit('error', new Error('😿'));
emitter.emit('hello', 'John');
console.log(result);
//=> ['Jack', 'Mark']
```
*/
readonly resolveImmediately?: boolean;
}
interface MultipleMultiArgumentsOptions<EmittedType extends unknown[]>
extends MultipleOptions<EmittedType> {
readonly multiArgs: true;
}
interface IteratorOptions<EmittedType extends unknown[]>
extends Options<EmittedType> {
/**
Maximum number of events for the iterator before it ends. When the limit is reached, the iterator will be marked as `done`. This option is useful to paginate events, for example, fetching 10 events per page.
@default Infinity
*/
limit?: number;
/**
Events that will end the iterator.
@default []
*/
resolutionEvents?: (string | symbol)[];
}
interface IteratorMultiArgumentsOptions<EmittedType extends unknown[]>
extends IteratorOptions<EmittedType> {
multiArgs: true;
}
}
export interface MultipleOptions<EmittedType extends unknown[]> extends Options<EmittedType> {
declare const pEvent: {
/**
* The number of times the event needs to be emitted before the promise resolves.
*/
readonly count: number;
Promisify an event by waiting for it to be emitted.
/**
* Whether to resolve the promise immediately. Emitting one of the `rejectionEvents` won't throw an error.
*
* **Note**: The returned array will be mutated when an event is emitted.
*
* @example
*
* const emitter = new EventEmitter();
*
* const promise = multiple(emitter, 'hello', {
* resolveImmediately: true,
* count: Infinity
* });
*
* const result = await promise;
* console.log(result);
* //=> []
*
* emitter.emit('hello', 'Jack');
* console.log(result);
* //=> ['Jack']
*
* emitter.emit('hello', 'Mark');
* console.log(result);
* //=> ['Jack', 'Mark']
*
* // Stops listening
* emitter.emit('error', new Error('😿'));
*
* emitter.emit('hello', 'John');
* console.log(result);
* //=> ['Jack', 'Mark']
*/
readonly resolveImmediately?: boolean;
}
@param emitter - Event emitter object. Should have either a `.on()`/`.addListener()`/`.addEventListener()` and `.off()`/`.removeListener()`/`.removeEventListener()` method, like the [Node.js `EventEmitter`](https://nodejs.org/api/events.html) and [DOM events](https://developer.mozilla.org/en-US/docs/Web/Events).
@param event - Name of the event or events to listen to. If the same event is defined both here and in `rejectionEvents`, this one takes priority.*Note**: `event` is a string for a single event type, for example, `'data'`. To listen on multiple events, pass an array of strings, such as `['started', 'stopped']`.
@returns Fulfills when emitter emits an event matching `event`, or rejects if emitter emits any of the events defined in the `rejectionEvents` option. The returned promise has a `.cancel()` method, which when called, removes the event listeners and causes the promise to never be settled.
export interface MultipleMultiArgumentsOptions<EmittedType extends unknown[]> extends MultipleOptions<EmittedType> {
readonly multiArgs: true;
}
@example
```
// In Node.js:
import pEvent = require('p-event');
import emitter from './some-event-emitter';
export interface IteratorOptions<EmittedType extends unknown[]> extends Options<EmittedType> {
(async () => {
try {
const result = await pEvent(emitter, 'finish');
// `emitter` emitted a `finish` event
console.log(result);
} catch (error) {
// `emitter` emitted an `error` event
console.error(error);
}
})();
// In the browser:
(async () => {
await pEvent(document, 'DOMContentLoaded');
console.log('😎');
})();
```
*/
<EventName extends string | symbol, EmittedType extends unknown[]>(
emitter: pEvent.Emitter<EventName, EmittedType>,
event: string | symbol | (string | symbol)[],
options: pEvent.MultiArgumentsOptions<EmittedType>
): pEvent.CancelablePromise<EmittedType>;
<EventName extends string | symbol, EmittedType>(
emitter: pEvent.Emitter<EventName, [EmittedType]>,
event: string | symbol | (string | symbol)[],
filter: pEvent.FilterFunction<[EmittedType]>
): pEvent.CancelablePromise<EmittedType>;
<EventName extends string | symbol, EmittedType>(
emitter: pEvent.Emitter<EventName, [EmittedType]>,
event: string | symbol | (string | symbol)[],
options?: pEvent.Options<[EmittedType]>
): pEvent.CancelablePromise<EmittedType>;
/**
* Maximum number of events for the iterator before it ends. When the limit is reached, the iterator will be marked as `done`. This option is useful to paginate events, for example, fetching 10 events per page.
*
* @default Infinity
*/
limit?: number;
Wait for multiple event emissions. Returns an array.
*/
multiple<EventName extends string | symbol, EmittedType extends unknown[]>(
emitter: pEvent.Emitter<EventName, EmittedType>,
event: string | symbol | (string | symbol)[],
options: pEvent.MultipleMultiArgumentsOptions<EmittedType>
): pEvent.CancelablePromise<EmittedType[]>;
multiple<EventName extends string | symbol, EmittedType>(
emitter: pEvent.Emitter<EventName, [EmittedType]>,
event: string | symbol | (string | symbol)[],
options: pEvent.MultipleOptions<[EmittedType]>
): pEvent.CancelablePromise<EmittedType[]>;
/**
* Events that will end the iterator.
*
* @default []
*/
resolutionEvents?: (string | symbol)[];
}
@returns An [async iterator](http://2ality.com/2016/10/asynchronous-iteration.html) that lets you asynchronously iterate over events of `event` emitted from `emitter`. The iterator ends when `emitter` emits an event matching any of the events defined in `resolutionEvents`, or rejects if `emitter` emits any of the events defined in the `rejectionEvents` option.
export interface IteratorMultiArgumentsOptions<EmittedType extends unknown[]>
extends IteratorOptions<EmittedType> {
multiArgs: true;
}
@example
```
import pEvent = require('p-event');
import emitter from './some-event-emitter';
(async () => {
const asyncIterator = pEvent.iterator(emitter, 'data', {
resolutionEvents: ['finish']
});
for await (const event of asyncIterator) {
console.log(event);
}
})();
```
*/
iterator<EventName extends string | symbol, EmittedType extends unknown[]>(
emitter: pEvent.Emitter<EventName, EmittedType>,
event: string | symbol | (string | symbol)[],
options: pEvent.IteratorMultiArgumentsOptions<EmittedType>
): AsyncIterableIterator<EmittedType>;
iterator<EventName extends string | symbol, EmittedType>(
emitter: pEvent.Emitter<EventName, [EmittedType]>,
event: string | symbol | (string | symbol)[],
filter: pEvent.FilterFunction<[EmittedType]>
): AsyncIterableIterator<EmittedType>;
iterator<EventName extends string | symbol, EmittedType>(
emitter: pEvent.Emitter<EventName, [EmittedType]>,
event: string | symbol | (string | symbol)[],
options?: pEvent.IteratorOptions<[EmittedType]>
): AsyncIterableIterator<EmittedType>;
// TODO: Remove this for the next major release
default: typeof pEvent;
};
export = pEvent;

@@ -115,2 +115,3 @@ 'use strict';

module.exports = pEvent;
// TODO: Remove this for the next major release
module.exports.default = pEvent;

@@ -117,0 +118,0 @@

{
"name": "p-event",
"version": "4.0.0",
"version": "4.1.0",
"description": "Promisify an event by waiting for it to be emitted",

@@ -16,3 +16,3 @@ "license": "MIT",

"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},

@@ -50,8 +50,8 @@ "files": [

"devDependencies": {
"@types/node": "^11.9.5",
"ava": "^1.2.1",
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"delay": "^4.1.0",
"tsd-check": "^0.3.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

@@ -178,2 +178,4 @@ # p-event [![Build Status](https://travis-ci.org/sindresorhus/p-event.svg?branch=master)](https://travis-ci.org/sindresorhus/p-event)

```js
const pEvent = require('p-event');
const emitter = new EventEmitter();

@@ -180,0 +182,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc