Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 27, 2021
1 parent 76b81cd commit 35e2d38
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 65 deletions.
12 changes: 6 additions & 6 deletions bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ suite

await queue.onEmpty();
deferred.resolve();
}
},
})
.add('operation with random priority', {
defer: true,
Expand All @@ -31,13 +31,13 @@ suite
for (let i = 0; i < 100; i++) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
queue.add(async () => {}, {
priority: Math.trunc(Math.random() * 100)
priority: Math.trunc(Math.random() * 100),
});
}

await queue.onEmpty();
deferred.resolve();
}
},
})
.add('operation with increasing priority', {
defer: true,
Expand All @@ -48,13 +48,13 @@ suite
for (let i = 0; i < 100; i++) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
queue.add(async () => {}, {
priority: i
priority: i,
});
}

await queue.onEmpty();
deferred.resolve();
}
},
})
.on('cycle', (event: Event) => {
console.log(String(event.target));
Expand All @@ -64,5 +64,5 @@ suite
console.log(`Fastest is ${(this as Benchmark.Suite).filter('fastest').map('name') as string}`);
})
.run({
async: true
async: true,
});
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@
"p-timeout": "^5.0.0"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^1.0.1",
"@types/benchmark": "^2.1.0",
"@types/node": "^14.14.37",
"@sindresorhus/tsconfig": "^2.0.0",
"@types/benchmark": "^2.1.1",
"@types/node": "^16.7.2",
"ava": "^3.15.0",
"benchmark": "^2.1.4",
"codecov": "^3.8.1",
"del-cli": "^3.0.1",
"codecov": "^3.8.3",
"del-cli": "^4.0.1",
"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.2.3",
"xo": "^0.38.2"
"random-int": "^3.0.0",
"time-span": "^5.0.0",
"ts-node": "^10.2.1",
"typescript": "^4.4.2",
"xo": "^0.44.0"
},
"ava": {
"files": [
Expand Down
10 changes: 5 additions & 5 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
concurrency: Number.POSITIVE_INFINITY,
autoStart: true,
queueClass: PriorityQueue,
...options
...options,
} as Options<QueueType, EnqueueOptionsType>;

if (!(typeof options.intervalCap === 'number' && options.intervalCap >= 1)) {
Expand Down Expand Up @@ -135,7 +135,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
() => {
this._onResumeInterval();
},
delay
delay,
);
}

Expand Down Expand Up @@ -192,7 +192,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
() => {
this._onInterval();
},
this._interval
this._interval,
);

this._intervalEnd = Date.now() + this._interval;
Expand Down Expand Up @@ -249,7 +249,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
}

return undefined;
}
},
);

const result = await operation;
Expand All @@ -276,7 +276,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
*/
async addAll<TaskResultsType>(
functions: ReadonlyArray<Task<TaskResultsType>>,
options?: EnqueueOptionsType
options?: EnqueueOptionsType,
): Promise<TaskResultsType[]> {
return Promise.all(functions.map(async function_ => this.add(function_, options)));
}
Expand Down
10 changes: 5 additions & 5 deletions source/priority-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOp
enqueue(run: RunFunction, options?: Partial<PriorityQueueOptions>): void {
options = {
priority: 0,
...options
...options,
};

const element = {
priority: options.priority,
run
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;
}

const index = lowerBound(
this._queue, element,
(a: Readonly<PriorityQueueOptions>, b: Readonly<PriorityQueueOptions>) => b.priority! - a.priority!
(a: Readonly<PriorityQueueOptions>, b: Readonly<PriorityQueueOptions>) => b.priority! - a.priority!,
);
this._queue.splice(index, 0, element);
}
Expand All @@ -39,7 +39,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
(element: Readonly<PriorityQueueOptions>) => element.priority === options.priority,
).map((element: Readonly<{run: RunFunction}>) => element.run);
}

Expand Down
Loading

0 comments on commit 35e2d38

Please sign in to comment.