Skip to content

Commit

Permalink
Upgrade TypeScript (#102)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
bobjflong and sindresorhus authored Mar 29, 2020
1 parent 2af8cf9 commit 0131f7f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@
"@sindresorhus/tsconfig": "^0.6.0",
"@types/benchmark": "^1.0.31",
"@types/node": "^12.12.7",
"@typescript-eslint/eslint-plugin": "^1.11.0",
"@typescript-eslint/parser": "^1.11.0",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"ava": "^2.0.0",
"benchmark": "^2.1.4",
"codecov": "^3.3.0",
"del-cli": "^3.0.0",
"delay": "^4.2.0",
"eslint-config-xo-typescript": "^0.16.0",
"eslint-config-xo-typescript": "^0.27.0",
"in-range": "^2.0.0",
"nyc": "^14.0.0",
"random-int": "^2.0.0",
"time-span": "^3.1.0",
"ts-node": "^8.3.0",
"typescript": "3.7.2",
"typescript": "3.8.3",
"xo": "^0.25.3"
},
"ava": {
Expand Down
11 changes: 6 additions & 5 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Task<TaskResultType> =
| (() => PromiseLike<TaskResultType>)
| (() => TaskResultType);

// eslint-disable-next-line @typescript-eslint/no-empty-function
const empty = (): void => {};

const timeoutError = new TimeoutError();
Expand Down Expand Up @@ -56,7 +57,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
constructor(options?: Options<QueueType, EnqueueOptionsType>) {
super();

// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
options = {
carryoverConcurrencyCount: false,
intervalCap: Infinity,
Expand All @@ -65,15 +66,14 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
autoStart: true,
queueClass: PriorityQueue,
...options
// TODO: Remove this `as`.
} as Options<QueueType, EnqueueOptionsType>;

if (!(typeof options.intervalCap === 'number' && options.intervalCap >= 1)) {
throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${options.intervalCap}\` (${typeof options.intervalCap})`);
throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${options.intervalCap?.toString() ?? ''}\` (${typeof options.intervalCap})`);
}

if (options.interval === undefined || !(Number.isFinite(options.interval) && options.interval >= 0)) {
throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${options.interval}\` (${typeof options.interval})`);
throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${options.interval?.toString() ?? ''}\` (${typeof options.interval})`);
}

this._carryoverConcurrencyCount = options.carryoverConcurrencyCount!;
Expand Down Expand Up @@ -226,6 +226,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
/**
Adds a sync or async task to the queue. Always returns a promise.
*/
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
async add<TaskResultType>(fn: Task<TaskResultType>, options: Partial<EnqueueOptionsType> = {}): Promise<TaskResultType> {
return new Promise<TaskResultType>((resolve, reject) => {
const run = async (): Promise<void> => {
Expand Down Expand Up @@ -349,7 +350,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
For example, this can be used to find the number of items remaining in the queue with a specific priority level.
*/
sizeBy(options: Partial<EnqueueOptionsType>): number {
sizeBy(options: Readonly<Partial<EnqueueOptionsType>>): number {
return this._queue.filter(options).length;
}

Expand Down
13 changes: 9 additions & 4 deletions source/priority-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOp
return;
}

const index = lowerBound(this._queue, element, (a, b) => b.priority! - a.priority!);
const index = lowerBound(
this._queue, element,
(a: Readonly<PriorityQueueOptions>, b: Readonly<PriorityQueueOptions>) => b.priority! - a.priority!
);
this._queue.splice(index, 0, element);
}

dequeue(): RunFunction | undefined {
const item = this._queue.shift();
return item && item.run;
return item?.run;
}

filter(options: Partial<PriorityQueueOptions>): RunFunction[] {
return this._queue.filter(element => element.priority === options.priority).map(element => element.run);
filter(options: Readonly<Partial<PriorityQueueOptions>>): RunFunction[] {
return this._queue.filter(
(element: Readonly<PriorityQueueOptions>) => element.priority === options.priority
).map((element: Readonly<{ run: RunFunction }>) => element.run);
}

get size(): number {
Expand Down

0 comments on commit 0131f7f

Please sign in to comment.