Comparing version 5.0.1 to 6.0.0
@@ -6,3 +6,3 @@ export type AddRemoveListener<EventName extends string | symbol, Arguments extends unknown[]> = ( | ||
export interface Emitter<EventName extends string | symbol, EmittedType extends unknown[]> { | ||
export type Emitter<EventName extends string | symbol, EmittedType extends unknown[]> = { | ||
on?: AddRemoveListener<EventName, EmittedType>; | ||
@@ -14,3 +14,3 @@ addListener?: AddRemoveListener<EventName, EmittedType>; | ||
removeEventListener?: AddRemoveListener<EventName, EmittedType>; | ||
} | ||
}; | ||
@@ -21,7 +21,7 @@ export type FilterFunction<ElementType extends unknown | unknown[]> = ( | ||
export interface CancelablePromise<ResolveType> extends Promise<ResolveType> { | ||
export type CancelablePromise<ResolveType> = { | ||
cancel(): void; | ||
} | ||
} & Promise<ResolveType>; | ||
export interface Options<EmittedType extends unknown | unknown[]> { | ||
export type Options<EmittedType extends unknown | unknown[]> = { | ||
/** | ||
@@ -69,11 +69,14 @@ Events that will reject the promise. | ||
readonly filter?: FilterFunction<EmittedType>; | ||
} | ||
export interface MultiArgumentsOptions<EmittedType extends unknown[]> | ||
extends Options<EmittedType> { | ||
/** | ||
An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to abort waiting for the event. | ||
*/ | ||
readonly signal?: AbortSignal; | ||
}; | ||
export type MultiArgumentsOptions<EmittedType extends unknown[]> = { | ||
readonly multiArgs: true; | ||
} | ||
} & Options<EmittedType>; | ||
export interface MultipleOptions<EmittedType extends unknown | unknown[]> | ||
extends Options<EmittedType> { | ||
export type MultipleOptions<EmittedType extends unknown | unknown[]> = { | ||
/** | ||
@@ -121,11 +124,9 @@ The number of times the event needs to be emitted before the promise resolves. | ||
readonly resolveImmediately?: boolean; | ||
} | ||
} & Options<EmittedType>; | ||
export interface MultipleMultiArgumentsOptions<EmittedType extends unknown[]> | ||
extends MultipleOptions<EmittedType> { | ||
export type MultipleMultiArgumentsOptions<EmittedType extends unknown[]> = { | ||
readonly multiArgs: true; | ||
} | ||
} & MultipleOptions<EmittedType>; | ||
export interface IteratorOptions<EmittedType extends unknown | unknown[]> | ||
extends Options<EmittedType> { | ||
export type IteratorOptions<EmittedType extends unknown | unknown[]> = { | ||
/** | ||
@@ -144,8 +145,7 @@ The 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. | ||
readonly resolutionEvents?: ReadonlyArray<string | symbol>; | ||
} | ||
} & Options<EmittedType>; | ||
export interface IteratorMultiArgumentsOptions<EmittedType extends unknown[]> | ||
extends IteratorOptions<EmittedType> { | ||
export type IteratorMultiArgumentsOptions<EmittedType extends unknown[]> = { | ||
multiArgs: true; | ||
} | ||
} & IteratorOptions<EmittedType>; | ||
@@ -152,0 +152,0 @@ /** |
21
index.js
@@ -31,2 +31,4 @@ import pTimeout from 'p-timeout'; | ||
options.signal?.throwIfAborted(); | ||
// Allow multiple events | ||
@@ -77,2 +79,8 @@ const events = [event].flat(); | ||
if (options.signal) { | ||
options.signal.addEventListener('abort', () => { | ||
rejectHandler(options.signal.reason); | ||
}, {once: true}); | ||
} | ||
if (options.resolveImmediately) { | ||
@@ -86,3 +94,3 @@ resolve(items); | ||
if (typeof options.timeout === 'number') { | ||
const timeout = pTimeout(returnValue, options.timeout); | ||
const timeout = pTimeout(returnValue, {milliseconds: options.timeout}); | ||
timeout.cancel = cancel; | ||
@@ -107,3 +115,3 @@ return timeout; | ||
const arrayPromise = pEventMultiple(emitter, event, options); | ||
const promise = arrayPromise.then(array => array[0]); // eslint-disable-line promise/prefer-await-to-then | ||
const promise = arrayPromise.then(array => array[0]); | ||
promise.cancel = arrayPromise.cancel; | ||
@@ -136,2 +144,4 @@ | ||
options.signal?.throwIfAborted(); | ||
if (limit === 0) { | ||
@@ -226,2 +236,3 @@ // Return an empty async iterator to avoid any further cost | ||
if (options.filter && !options.filter(value)) { | ||
cancel(); | ||
return; | ||
@@ -252,2 +263,8 @@ } | ||
if (options.signal) { | ||
options.signal.addEventListener('abort', () => { | ||
rejectHandler(options.signal.reason); | ||
}, {once: true}); | ||
} | ||
return { | ||
@@ -254,0 +271,0 @@ [Symbol.asyncIterator]() { |
{ | ||
"name": "p-event", | ||
"version": "5.0.1", | ||
"version": "6.0.0", | ||
"description": "Promisify an event by waiting for it to be emitted", | ||
@@ -14,5 +14,8 @@ "license": "MIT", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
"node": ">=16.17" | ||
}, | ||
@@ -50,11 +53,16 @@ "scripts": { | ||
"dependencies": { | ||
"p-timeout": "^5.0.2" | ||
"p-timeout": "^6.1.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^16.11.6", | ||
"ava": "^3.15.0", | ||
"delay": "^5.0.0", | ||
"tsd": "^0.18.0", | ||
"xo": "^0.45.0" | ||
"@types/node": "^20.3.1", | ||
"ava": "^5.3.0", | ||
"delay": "^6.0.0", | ||
"tsd": "^0.28.1", | ||
"xo": "^0.54.2" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"@typescript-eslint/no-redundant-type-constituents": "off" | ||
} | ||
} | ||
} |
@@ -136,2 +136,8 @@ # p-event | ||
##### signal | ||
Type: `AbortSignal` | ||
An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to abort waiting for the event. | ||
### pEventMultiple(emitter, event, options) | ||
@@ -138,0 +144,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24639
435
320
+ Addedp-timeout@6.1.3(transitive)
- Removedp-timeout@5.1.0(transitive)
Updatedp-timeout@^6.1.2