Skip to content

Commit

Permalink
Require Node.js 16
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 14, 2023
1 parent 021f863 commit 918fe05
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 18
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
37 changes: 16 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ export type AddRemoveListener<EventName extends string | symbol, Arguments exten
listener: (...arguments: Arguments) => void
) => void;

export interface Emitter<EventName extends string | symbol, EmittedType extends unknown[]> {
export type 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 | unknown[]> = (
value: ElementType
) => boolean;

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[]> = {
/**
Events that will reject the promise.
Expand Down Expand Up @@ -68,15 +68,13 @@ export interface Options<EmittedType extends unknown | unknown[]> {
An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to abort waiting for the event.
*/
readonly signal?: AbortSignal;
}
};

export interface MultiArgumentsOptions<EmittedType extends unknown[]>
extends Options<EmittedType> {
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[]> = {
/**
The number of times the event needs to be emitted before the promise resolves.
*/
Expand Down Expand Up @@ -119,15 +117,13 @@ export interface MultipleOptions<EmittedType extends unknown | unknown[]>
```
*/
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[]> = {
/**
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.
Expand All @@ -141,12 +137,11 @@ export interface IteratorOptions<EmittedType extends unknown | unknown[]>
@default []
*/
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>;

/**
Promisify an event by waiting for it to be emitted.
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export function pEventMultiple(emitter, event, options) {
}

if (options.signal) {
options.signal.addEventListener('abort', () => rejectHandler(options.signal.reason), {once: true});
options.signal.addEventListener('abort', () => {
rejectHandler(options.signal.reason);
}, {once: true});
}

if (options.resolveImmediately) {
Expand All @@ -87,7 +89,7 @@ export function pEventMultiple(emitter, event, options) {
returnValue.cancel = cancel;

if (typeof options.timeout === 'number') {
const timeout = pTimeout(returnValue, options.timeout);
const timeout = pTimeout(returnValue, {milliseconds: options.timeout});
timeout.cancel = cancel;
return timeout;
}
Expand All @@ -107,7 +109,7 @@ export function pEvent(emitter, event, options) {
};

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;

return promise;
Expand Down Expand Up @@ -253,7 +255,9 @@ export function pEventIterator(emitter, event, options) {
}

if (options.signal) {
options.signal.addEventListener('abort', () => rejectHandler(options.signal.reason), {once: true});
options.signal.addEventListener('abort', () => {
rejectHandler(options.signal.reason);
}, {once: true});
}

return {
Expand Down
6 changes: 3 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import process from 'node:process';
import {EventEmitter} from 'node:events';
import fs from 'node:fs';
import {expectType} from 'tsd';
import {pEvent, pEventMultiple, pEventIterator, CancelablePromise} from './index.js';
import {pEvent, pEventMultiple, pEventIterator, type CancelablePromise} from './index.js';

class NodeEmitter extends EventEmitter {
on(_event: 'finish', _listener: (number: number, string: string) => void) {
Expand Down Expand Up @@ -129,9 +129,9 @@ void pEventIterator(new NodeEmitter(), 'finish', {
});

async function getOpenReadStream(file: string): Promise<NodeJS.ReadableStream> {
const stream = fs.createReadStream(file); // eslint-disable-line @typescript-eslint/no-unsafe-assignment
const stream = fs.createReadStream(file) as NodeJS.ReadableStream;
await pEvent(stream, 'open');
return stream; // eslint-disable-line @typescript-eslint/no-unsafe-return
return stream;
}

const stream = await getOpenReadStream('unicorn.txt');
Expand Down
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
"url": "https://sindresorhus.com"
},
"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"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -46,13 +49,18 @@
"bluebird"
],
"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"
}
}
}

0 comments on commit 918fe05

Please sign in to comment.