Skip to content

Commit

Permalink
upgrade packages (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaziuk authored Oct 19, 2018
1 parent 10b955d commit d6492ea
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 66 deletions.
15 changes: 4 additions & 11 deletions delay.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import {
expect,
} from "chai";

import {
spy,
} from "sinon";

import { expect } from "chai";
import delay from "./delay";

describe(delay.name || "delay", () => {
it("should return a 'Promise'", () => {
it(`should return a '${Promise.name}'`, () => {
expect(delay(0)).to.be.instanceOf(Promise);
});
it("should be working with negative timeout", async () => {
Expand All @@ -18,10 +11,10 @@ describe(delay.name || "delay", () => {
it("should resolve value", async () => {
expect(await delay(0, "abc")).to.be.equal("abc");
});
it("should resolve value from 'Promise'", async () => {
it(`should resolve value from '${Promise.name}'`, async () => {
expect(await delay(0, Promise.resolve("abc"))).to.be.equal("abc");
});
it("should reject value from 'Promise'", (done) => {
it(`should reject value from '${Promise.name}'`, (done) => {
const reject = Promise.reject(new Error());
reject.catch(() => void 0);
delay(0, reject).catch(() => { done(); });
Expand Down
15 changes: 3 additions & 12 deletions index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import {
expect,
} from "chai";

import {
SinonSpy,
spy,
stub,
} from "sinon";

import ASAP, { task } from "./index";

import { expect } from "chai";
import { spy } from "sinon";
import delay from "./delay";
import ASAP from "./index";

describe(ASAP.name, () => {
it("should be a class", () => {
Expand Down
31 changes: 14 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* () => import("fs").then((fs) => fs.readFileSync("/path/to/a/file"));
* ```
*/
export type task<T = any> = () => T | PromiseLike<T>;
export type Task<T = any> = () => T | PromiseLike<T>;

export interface IASAP {
/**
Expand All @@ -29,7 +29,7 @@ export interface IASAP {
* }
* ```
*/
q<T>(fn: task<T> | PromiseLike<task<T>>, priority?: number): Promise<T>;
q<T>(fn: Task<T> | PromiseLike<Task<T>>, priority?: number): Promise<T>;
}

function ASAP(this: any, c: boolean | number = 1): any {
Expand All @@ -46,14 +46,9 @@ function ASAP(this: any, c: boolean | number = 1): any {
let concurrency: number;

/**
* array of functions which returns promises
* Set of functions which returns promises with priority
*/
const heap = [] as Array<[() => Promise<any>, number]>;

/**
* Set of resolved or rejected promise methods
*/
const complete = new Set<() => Promise<any>>();
const heap = new Set<[() => Promise<any>, number]>();

/**
* Set of pending/running promise methods
Expand All @@ -66,16 +61,18 @@ function ASAP(this: any, c: boolean | number = 1): any {
const process = (): void => {
const { size } = pending;
if (size < concurrency) {
heap.filter(
// filter the heap to get only not completed nor pending (running) tasks
([v]) => !complete.has(v) && !pending.has(v),
).sort(
Array.from(heap).sort(
// sort the heap from highest to lowest priority
([, a], [, b]) => a - b,
).slice(
0,
concurrency - size, // slice the array to the size of left concurrency value
).forEach(([v]) => {
).forEach((heapItem) => {
const [v] = heapItem;

// delete
heap.delete(heapItem);

// mark the promise function as pending
pending.add(v);

Expand Down Expand Up @@ -104,17 +101,17 @@ function ASAP(this: any, c: boolean | number = 1): any {
},
},
q: {
value: <T>(fn: task<T> | PromiseLike<task<T>>, priority?: number) => new Promise<T>((resolve, reject) => {
value: <T>(fn: Task<T> | PromiseLike<Task<T>>, priority?: number) => new Promise<T>((resolve, reject) => {
const promFn = () => {
// create a new promise in case when the `fn` throws anything
const prom = Promise.resolve(fn).then((v) => v());

// react on `fn` resolution and set the promise as completed
return prom.then(resolve, reject).then(() => { complete.add(promFn); });
return prom.then(resolve, reject);
};

// push the promise function and priority to the task list
heap.push([promFn, priority || 0]);
heap.add([promFn, priority || 0]);

// process the task list
process();
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"access": "public",
"name": "asap-es",
"version": "1.3.1",
"version": "1.3.2",
"description": "a queue runner with priorities, concurrency and promises",
"main": "index",
"private": false,
Expand Down Expand Up @@ -48,15 +48,15 @@
"homepage": "https://github.com/tlaziuk/asap-es",
"devDependencies": {
"@types/chai": "^4.0.0",
"@types/mocha": "~2.2.0",
"@types/sinon": "^4.0.0",
"@types/mocha": "^5.2.0",
"@types/sinon": "^5.0.0",
"chai": "^4.0.0",
"coveralls": "^3.0.0",
"mocha": "~2.2.0",
"nyc": "^11.0.0",
"sinon": "^4.0.0",
"ts-node": "^4.0.0",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"sinon": "^5.0.0",
"ts-node": "^7.0.0",
"tslint": "^5.8.0",
"typescript": "~2.7.0"
"typescript": "^3.0.0"
}
}
20 changes: 12 additions & 8 deletions timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
expect,
} from "chai";

import {
spy,
} from "sinon";

import { expect } from "chai";
import { spy } from "sinon";
import ASAP from ".";
import delay from "./delay";
import timeout from "./timeout";
Expand All @@ -24,6 +18,16 @@ describe(timeout.name || "timeout", () => {
}
expect(taskSpy.callCount).to.be.equal(1);
});
it("should the task function be called when params are promises", async () => {
const taskSpy = spy();
const taskNew = timeout(Promise.resolve(0), Promise.resolve(taskSpy));
try {
await taskNew();
} catch {
// pass
}
expect(taskSpy.callCount).to.be.equal(1);
});
it("should the new task resolve to value of original task", async () => {
const task = () => "abc";
const taskNew = timeout(0, task);
Expand Down
21 changes: 11 additions & 10 deletions timeout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { task } from ".";
import { Task } from ".";

/**
* reject when the execution exceeds given time
Expand All @@ -9,23 +9,24 @@ import { task } from ".";
*/
export default <T = any>(
timeout: number | PromiseLike<number>,
fn: task<T> | PromiseLike<task<T>>,
fn: Task<T> | PromiseLike<Task<T>>,
reason?: string | PromiseLike<string>,
): (() => Promise<T>) => () => Promise.all([timeout, fn, reason as string | undefined]).then(
): (() => Promise<T>) => () => Promise.all([timeout, fn, reason as any]).then(
// run the logic when task will be ready
([timeoutResolved, taskResolved, reasonResolved]) => new Promise<T>(
(resolve, reject) => {
Promise.resolve(
// task is ready, yet it may return a promise
taskResolved(),
).then(
resolve,
reject,
).then(resolve, reject);

setTimeout(
() => {
// reject when the timeout is reached
reject(new Error(reasonResolved));
},
timeoutResolved,
);
setTimeout(() => {
// reject when the timeout is reached
reject(new Error(reasonResolved));
}, timeout);
},
),
);

0 comments on commit d6492ea

Please sign in to comment.