Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
Closes #132
Closes #127
  • Loading branch information
sindresorhus committed Mar 28, 2021
1 parent 7036650 commit 8c7325a
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 98 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
6 changes: 2 additions & 4 deletions bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Benchmark = require('benchmark');
import {Deferred, Event} from 'benchmark';
import PQueue from './source';
import Benchmark, {Deferred, Event} from 'benchmark';
import PQueue from './source/index.js';

const suite = new Benchmark.Suite();

Expand Down Expand Up @@ -54,7 +53,6 @@ suite
}

await queue.onEmpty();
// @ts-expect-error benchmark typings incorrect
deferred.resolve();
}
})
Expand Down
40 changes: 20 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"license": "MIT",
"repository": "sindresorhus/p-queue",
"funding": "https://github.com/sponsors/sindresorhus",
"main": "dist/index.js",
"type": "module",
"exports": "./dist/index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"build": "del dist && tsc",
"test": "xo && npm run build && nyc ava",
"bench": "ts-node bench.ts",
"bench": "node --loader=ts-node/esm bench.ts",
"prepublishOnly": "npm run build"
},
"files": [
Expand Down Expand Up @@ -46,41 +47,40 @@
"p-timeout": "^4.1.0"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^0.7.0",
"@sindresorhus/tsconfig": "^1.0.0",
"@types/benchmark": "^2.1.0",
"@types/node": "^14.14.19",
"ava": "^2.4.0",
"@types/node": "^14.14.37",
"ava": "^3.15.0",
"benchmark": "^2.1.4",
"codecov": "^3.8.1",
"del-cli": "^3.0.1",
"delay": "^4.4.0",
"in-range": "^2.0.0",
"delay": "^5.0.0",
"in-range": "^3.0.0",
"nyc": "^15.1.0",
"random-int": "^2.0.1",
"time-span": "^4.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.3",
"xo": "^0.37.1"
"typescript": "^4.2.3",
"xo": "^0.38.2"
},
"ava": {
"babel": false,
"compileEnhancements": false,
"extensions": [
"ts"
],
"require": [
"ts-node/register"
],
"files": [
"test/**"
],
"extensions": {
"ts": "module"
},
"nonSemVerExperiments": {
"configurableModuleFormat": true
},
"nodeArguments": [
"--loader=ts-node/esm"
]
},
"xo": {
"rules": {
"@typescript-eslint/member-ordering": "off",
"node/no-unsupported-features/es-syntax": "off",
"@typescript-eslint/no-floating-promises": "off",
"import/no-named-default": "off",
"@typescript-eslint/no-invalid-void-type": "off"
}
},
Expand Down
30 changes: 17 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ $ npm install p-queue
Here we run only one promise at the time. For example, set `concurrency` to 4 to run four promises at the same time.

```js
const {default: PQueue} = require('p-queue');
const got = require('got');
import PQueue from 'p-queue';
import got from 'got';

const queue = new PQueue({concurrency: 1});

Expand Down Expand Up @@ -170,6 +170,8 @@ Size of the queue, filtered by the given options.
For example, this can be used to find the number of items remaining in the queue with a specific priority level.

```js
import PQueue from 'p-queue';

const queue = new PQueue();

queue.add(async () => '🦄', {priority: 1});
Expand Down Expand Up @@ -202,8 +204,8 @@ Whether the queue is currently paused.
Emitted as each item is processed in the queue for the purpose of tracking progress.

```js
const delay = require('delay');
const {default: PQueue} = require('p-queue');
import delay from 'delay';
import PQueue from 'p-queue';

const queue = new PQueue({concurrency: 2});

Expand All @@ -218,13 +220,14 @@ queue.add(() => Promise.resolve());
queue.add(() => Promise.resolve());
queue.add(() => delay(500));
```

#### idle

Emitted every time the queue becomes empty and all promises have completed; `queue.size === 0 && queue.pending === 0`.

```js
const delay = require('delay');
const {default: PQueue} = require('p-queue');
import delay from 'delay';
import PQueue from 'p-queue';

const queue = new PQueue();

Expand Down Expand Up @@ -254,14 +257,15 @@ Emitted every time the add method is called and the number of pending or queued
Emitted every time a task is completed and the number of pending or queued tasks is decreased.

```js
const delay = require('delay');
const {default: PQueue} = require('p-queue');
import delay from 'delay';
import PQueue from 'p-queue';

const queue = new PQueue();

queue.on('add', () => {
console.log(`Task is added. Size: ${queue.size} Pending: ${queue.pending}`);
});

queue.on('next', () => {
console.log(`Task is completed. Size: ${queue.size} Pending: ${queue.pending}`);
});
Expand All @@ -284,8 +288,8 @@ await queue.add(() => delay(600));
A more advanced example to help you understand the flow.

```js
const delay = require('delay');
const {default: PQueue} = require('p-queue');
import delay from 'delay';
import PQueue from 'p-queue';

const queue = new PQueue({concurrency: 1});

Expand Down Expand Up @@ -354,6 +358,8 @@ $ node example.js
For implementing more complex scheduling policies, you can provide a QueueClass in the options:

```js
import PQueue from 'p-queue';

class QueueClass {
constructor() {
this._queue = [];
Expand All @@ -376,9 +382,7 @@ class QueueClass {
}
}

const queue = new PQueue({
queueClass: QueueClass
});
const queue = new PQueue({queueClass: QueueClass});
```

`p-queue` will call corresponding methods to put and get operations from this queue.
Expand Down
10 changes: 5 additions & 5 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import EventEmitter = require('eventemitter3');
import {default as pTimeout, TimeoutError} from 'p-timeout';
import {Queue, RunFunction} from './queue';
import PriorityQueue from './priority-queue';
import {QueueAddOptions, DefaultAddOptions, Options} from './options';
import EventEmitter from 'eventemitter3';
import pTimeout, {TimeoutError} from 'p-timeout';
import {Queue, RunFunction} from './queue.js';
import PriorityQueue from './priority-queue.js';
import {QueueAddOptions, DefaultAddOptions, Options} from './options.js';

type ResolveFunction<T = void> = (value?: T | PromiseLike<T>) => void;

Expand Down
2 changes: 1 addition & 1 deletion source/lower-bound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function lowerBound<T>(array: readonly T[], value: T, comparator:
const step = Math.trunc(count / 2);
let it = first + step;

if (comparator(array[it], value) <= 0) {
if (comparator(array[it]!, value) <= 0) {
first = ++it;
count -= step + 1;
} else {
Expand Down
2 changes: 1 addition & 1 deletion source/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Queue, RunFunction} from './queue';
import {Queue, RunFunction} from './queue.js';

export type QueueAddOptions = Readonly<Record<string, unknown>>;

Expand Down
10 changes: 5 additions & 5 deletions source/priority-queue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Queue, RunFunction} from './queue';
import lowerBound from './lower-bound';
import {QueueAddOptions} from './options';
import {Queue, RunFunction} from './queue.js';
import lowerBound from './lower-bound.js';
import {QueueAddOptions} from './options.js';

export interface PriorityQueueOptions extends QueueAddOptions {
priority?: number;
Expand All @@ -20,7 +20,7 @@ export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOp
run
};

if (this.size && this._queue[this.size - 1].priority! >= options.priority!) {
if (this.size && this._queue[this.size - 1]?.priority! >= options.priority!) {
this._queue.push(element);
return;
}
Expand All @@ -40,7 +40,7 @@ export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOp
filter(options: Readonly<Partial<PriorityQueueOptions>>): RunFunction[] {
return this._queue.filter(
(element: Readonly<PriorityQueueOptions>) => element.priority === options.priority
).map((element: Readonly<{ run: RunFunction }>) => element.run);
).map((element: Readonly<{run: RunFunction}>) => element.run);
}

get size(): number {
Expand Down
Loading

0 comments on commit 8c7325a

Please sign in to comment.