Skip to content

Commit 90d9331

Browse files
committed
wip
1 parent ed9f6f5 commit 90d9331

30 files changed

+396
-34
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: node_js
2+
node_js:
3+
- '4.0'
4+
- 'stable'
5+
6+
sudo: false
7+
8+
before_install:
9+
- npm install -g npm@latest
10+
- npm install -g grunt-cli
11+
12+
install:
13+
- npm install

lib/functions/delay.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function delay(time: number, value?: any): Promise<any>;

lib/functions/delay.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
function delay(time, value) {
3+
return new Promise(function (resolve) {
4+
setTimeout(resolve.bind(null, value), time);
5+
});
6+
}
7+
exports.delay = delay;

lib/functions/links.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Tasks } from "../types";
2+
export declare function all(tasks: Tasks): Promise<any>;
3+
export declare function race(tasks: Tasks): Promise<any>;

lib/functions/links.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
function all(tasks) {
3+
return Promise.all(tasks);
4+
}
5+
exports.all = all;
6+
function race(tasks) {
7+
return Promise.race(tasks);
8+
}
9+
exports.race = race;

lib/functions/parallel.d.ts

Whitespace-only changes.

lib/functions/parallel.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*import {
2+
Tasks
3+
} from "../types";
4+
5+
export function parallel(tasks: Tasks, maxThreads: number = tasks.length): Promise<any> {
6+
if (tasks.length === 0) {
7+
return Promise.resolve();
8+
}
9+
10+
return new Promise(function (resolve, reject) {
11+
12+
});
13+
}*/

lib/functions/retry.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { ITask } from "../types";
2+
export declare function retry(task: ITask, times: number): Promise<any>;

lib/functions/retry.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
function process(task, times, reasons, resolve, reject) {
3+
task().then(function (result) {
4+
resolve(result);
5+
}).catch(function (reason) {
6+
reasons.push(reason);
7+
if (reasons.length >= times) {
8+
reject(reasons);
9+
}
10+
else {
11+
process(task, times, reasons, resolve, reject);
12+
}
13+
});
14+
}
15+
function retry(task, times) {
16+
return new Promise(function (resolve, reject) {
17+
var reasons = [];
18+
process(task, times, reasons, resolve, reject);
19+
});
20+
}
21+
exports.retry = retry;

lib/functions/timeout.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { ITask } from "../types";
2+
export declare function timeout(time: number, task: ITask): Promise<any>;

lib/functions/timeout.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
var types_1 = require("../types");
3+
function timeout(time, task) {
4+
return new Promise(function (resolve, reject) {
5+
var timeout = setTimeout(reject.bind(null, new types_1.TimeoutError()), time);
6+
task().then(function (result) {
7+
resolve(result);
8+
clearTimeout(timeout);
9+
}, function (reason) {
10+
reject(reason);
11+
clearTimeout(timeout);
12+
});
13+
});
14+
}
15+
exports.timeout = timeout;

lib/functions/times.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { ITask } from "../types";
2+
export declare function times(task: ITask, times: number, stopOnFirstReject?: boolean): Promise<any>;

lib/functions/times.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
function stopCheck(times, results, resolve) {
3+
if (results.length >= times) {
4+
resolve(results);
5+
return true;
6+
}
7+
}
8+
function process(task, times, results, resolve, reject, stopOnFirstReject) {
9+
task().then(function (result) {
10+
results.push(result);
11+
if (!stopCheck(times, results, resolve)) {
12+
process(task, times, results, resolve, reject, stopOnFirstReject);
13+
}
14+
}).catch(function (reason) {
15+
results.push(reason);
16+
if (stopOnFirstReject) {
17+
reject(results);
18+
}
19+
else if (!stopCheck(times, results, resolve)) {
20+
process(task, times, results, resolve, reject, stopOnFirstReject);
21+
}
22+
});
23+
}
24+
function times(task, times, stopOnFirstReject) {
25+
if (times <= 0) {
26+
return Promise.resolve([]);
27+
}
28+
return new Promise(function (resolve, reject) {
29+
var results = [];
30+
process(task, times, results, resolve, reject, stopOnFirstReject);
31+
});
32+
}
33+
exports.times = times;

lib/functions/waterfall.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { Tasks } from "../types";
2+
export declare function waterfall(tasks: Tasks): Promise<any>;

lib/functions/waterfall.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
function waterfall(tasks) {
3+
if (tasks.length === 0) {
4+
return Promise.resolve();
5+
}
6+
var promise = tasks[0]();
7+
if (tasks.length > 1) {
8+
for (var _i = 0, tasks_1 = tasks; _i < tasks_1.length; _i++) {
9+
var task = tasks_1[_i];
10+
promise = promise.then(task);
11+
}
12+
}
13+
return promise;
14+
}
15+
exports.waterfall = waterfall;

lib/prow.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { ITask, Tasks, TimeoutError } from "./types";
2+
export { delay } from "./functions/delay";
3+
export { timeout } from "./functions/timeout";
4+
export { waterfall } from "./functions/waterfall";
5+
export { retry } from "./functions/retry";
6+
export { times } from "./functions/times";

lib/prow.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var types_1 = require("./types");
3+
exports.TimeoutError = types_1.TimeoutError;
4+
var delay_1 = require("./functions/delay");
5+
exports.delay = delay_1.delay;
6+
var timeout_1 = require("./functions/timeout");
7+
exports.timeout = timeout_1.timeout;
8+
var waterfall_1 = require("./functions/waterfall");
9+
exports.waterfall = waterfall_1.waterfall;
10+
var retry_1 = require("./functions/retry");
11+
exports.retry = retry_1.retry;
12+
var times_1 = require("./functions/times");
13+
exports.times = times_1.times;

lib/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface ITask {
2+
(...args: any[]): Promise<any>;
3+
}
4+
export declare type Tasks = ITask[];
5+
export declare class TimeoutError extends Error {
6+
}

lib/types.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || function (d, b) {
3+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4+
function __() { this.constructor = d; }
5+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6+
};
7+
var TimeoutError = (function (_super) {
8+
__extends(TimeoutError, _super);
9+
function TimeoutError() {
10+
_super.apply(this, arguments);
11+
}
12+
return TimeoutError;
13+
}(Error));
14+
exports.TimeoutError = TimeoutError;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
],
2121
"scripts": {
2222
"build": "tsc",
23+
"pretest": "npm run lint",
2324
"test": "mocha",
24-
"lint": "tslint --project ./"
25+
"lint": "tslint --project ./",
26+
"prepublish": "npm run lint && npm run build && mocha"
2527
},
2628
"devDependencies": {
2729
"chai": "^3.5.0",

src/functions/parallel.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import {
1+
/*import {
22
Tasks
33
} from "../types";
44
5-
6-
export function parallel(tasks: Tasks, maxThreads?: number): Promise<any> {
7-
if (!maxThreads) {
8-
maxThreads = tasks.length;
5+
export function parallel(tasks: Tasks, maxThreads: number = tasks.length): Promise<any> {
6+
if (tasks.length === 0) {
7+
return Promise.resolve();
98
}
109
1110
return new Promise(function (resolve, reject) {
1211
13-
1412
});
15-
}
13+
}*/

src/functions/retry.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import {
2-
Task
2+
ITask
33
} from "../types";
44

5-
function process(task: Task, times: number, reasons: any[], resolve, reject) {
5+
function process(task: ITask, times: number, reasons: any[], resolve, reject) {
66
task().then((result) => {
77
resolve(result);
88
}).catch((reason) => {
99
reasons.push(reason);
1010
if (reasons.length >= times) {
1111
reject(reasons);
1212
} else {
13-
process(task, times, resolve, reject, reasons);
13+
process(task, times, reasons, resolve, reject);
1414
}
1515
});
1616
}
1717

18-
export function retry(task: Task, times: number): Promise<any> {
18+
export function retry(task: ITask, times: number): Promise<any> {
1919
return new Promise(function (resolve, reject) {
2020
const reasons = [];
2121
process(task, times, reasons, resolve, reject);

src/functions/timeout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2-
Task, TimeoutError
2+
ITask, TimeoutError
33
} from "../types";
44

5-
export function timeout(time: number, task: Task): Promise<any> {
5+
export function timeout(time: number, task: ITask): Promise<any> {
66
return new Promise(function (resolve, reject) {
77
const timeout = setTimeout(reject.bind(null, new TimeoutError()), time);
88
task().then((result) => {

src/functions/times.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
import {
2-
Task
2+
ITask
33
} from "../types";
44

5+
function stopCheck(times: number, results: any[], resolve): boolean {
6+
if (results.length >= times) {
7+
resolve(results);
8+
return true;
9+
}
10+
}
11+
12+
function process(task: ITask, times: number, results: any[], resolve, reject, stopOnFirstReject: boolean) {
13+
task().then((result) => {
14+
results.push(result);
15+
if (!stopCheck(times, results, resolve)) {
16+
process(task, times, results, resolve, reject, stopOnFirstReject);
17+
}
18+
}).catch((reason) => {
19+
results.push(reason);
20+
if (stopOnFirstReject) {
21+
reject(results);
22+
} else if (!stopCheck(times, results, resolve)) {
23+
process(task, times, results, resolve, reject, stopOnFirstReject);
24+
}
25+
});
26+
}
27+
28+
export function times(task: ITask, times: number, stopOnFirstReject?: boolean): Promise<any> {
29+
if (times <= 0) {
30+
return Promise.resolve([]);
31+
}
532

6-
export function times(task: Task, times: number, stopOnFirstReject: boolean): Promise<any> {
733
return new Promise(function (resolve, reject) {
834
const results = [];
9-
35+
process(task, times, results, resolve, reject, stopOnFirstReject);
1036
});
11-
}
37+
}

src/prow.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
export {
2-
Task, Tasks, TimeoutError
2+
ITask, Tasks, TimeoutError
33
} from "./types";
44

55
export {delay} from "./functions/delay";
66
export {timeout} from "./functions/timeout";
77
export {waterfall} from "./functions/waterfall";
88
export {retry} from "./functions/retry";
9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
9+
export {times} from "./functions/times";

src/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
export interface Task {
2-
(...args: any[]): Promise<any>
1+
export interface ITask {
2+
(...args: any[]): Promise<any>;
33
}
4-
export type Tasks = Task[];
54

6-
export class TimeoutError extends Error{
5+
export type Tasks = ITask[];
6+
7+
export class TimeoutError extends Error {
78
}

test/delay.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const _ = require("lodash");
2+
const chai = require("chai");
3+
const chaiAsPromised = require("chai-as-promised");
4+
chai.use(chaiAsPromised);
5+
const {assert, expect} = chai;
6+
const prow = require("../lib/prow");
7+
8+
describe("Delay", function () {
9+
it("return value", function () {
10+
return assert.becomes(prow.delay(10, 300), 300);
11+
});
12+
13+
it("delay 160ms", function () {
14+
const start = process.hrtime();
15+
return assert.becomes(prow.delay(160, 300), 300).then(() => {
16+
const time = process.hrtime(start);
17+
assert.approximately(time[0] * 100000000 + time[1], 160000000, 5000000)
18+
});
19+
});
20+
21+
it("delay 1050ms", function () {
22+
const start = process.hrtime();
23+
return assert.becomes(prow.delay(1050, 300), 300).then(() => {
24+
const time = process.hrtime(start);
25+
assert.approximately(time[0] * 1000000000 + time[1], 1050000000, 5000000)
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)