add definitions for promise-pool

This commit is contained in:
VILIC VANE
2014-08-10 00:55:22 +08:00
parent f981db1a42
commit f402bb57bd
3 changed files with 172 additions and 0 deletions
+1
View File
@@ -289,6 +289,7 @@ All definitions files include a header with the author and editors, so at some p
* [PouchDB](http://pouchdb.com) (by [Bill Sears](https://github.com/MrBigDog2U/))
* [PreloadJS](http://www.createjs.com/#!/PreloadJS) (by [Pedro Ferreira](https://bitbucket.org/drk4))
* [ProgressJs](http://usablica.github.io/progress.js/) (by [Shunsuke Ohtani](https://github.com/zaneli))
* [promise-pool](https://github.com/vilic/promise-pool) (by [VILIC VANE](https://github.com/vilic))
* [Q](https://github.com/kriskowal/q) (by Barrie Nemetchek, Andrew Gaspar)
* [Q-io](https://github.com/kriskowal/q-io) (by [Bart van der Schoor](https://github.com/Bartvds))
* [q-retry](https://github.com/vilic/q-retry) (by [VILIC VANE](https://github.com/vilic))
+47
View File
@@ -0,0 +1,47 @@
import Q = require('q');
import promisePool = require('promise-pool');
var pool = new promisePool.Pool<number>((taskDataId, index) => {
return Q.delay(Math.floor(Math.random() * 5000)).then(function () {
taskDataId == 0;
index == 0;
});
}, 20);
pool
.pause()
.delay(5000)
.then(function () {
pool.resume();
});
pool.retries == 0;
pool.retryInterval == 0;
pool.maxRetryInterval == 0;
pool.retryIntervalMultiplier == 0;
pool.add(0);
pool
.start(onProgress)
.then(result => {
result.total == 0;
return pool.reset();
})
.then(() => {
return pool.start(onProgress);
})
.then(result => {
result.total == 0;
return pool.reset();
})
.then(() => {
pool.endless == true;
});
function onProgress(progress: promisePool.IProgress) {
progress.success == true;
progress.fulfilled == 0;
progress.total == 0;
progress.index == 0;
}
+124
View File
@@ -0,0 +1,124 @@
// Type definitions for promise-pool
// Project: https://github.com/vilic/promise-pool
// Definitions by: VILIC VANE <https://github.com/vilic>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../q-retry/q-retry.d.ts" />
declare module "promise-pool" {
/**
* interface for the final result.
*/
export interface IResult {
fulfilled: number;
rejected: number;
total: number;
}
/**
* interface for progress data.
*/
export interface IProgress {
index: number;
success: boolean;
error: any;
retries: number;
fulfilled: number;
rejected: number;
pending: number;
total: number;
}
/**
* tasks pool that manages concurrency.
*/
export class Pool<T> {
/**
* (get/set) the max concurrency of this task pool.
*/
public concurrency: number;
private _tasksData;
/**
* (get/set) the processor function that handles tasks data.
*/
public processor: (data: T, index: number) => Q.Promise<void>;
private _deferred;
private _pauseDeferred;
/**
* (get) the number of successful tasks.
*/
public fulfilled: number;
/**
* (get) the number of failed tasks.
*/
public rejected: number;
/**
* (get) the number of pending tasks.
*/
public pending: number;
/**
* (get) the number of completed tasks and pending tasks in total.
*/
public total: number;
/**
* (get/set) indicates whether this task pool is endless, if so, tasks can still be added even after all previous tasks have been fulfilled.
*/
public endless: boolean;
/**
* (get/set) defaults to 0, the number or retries that this task pool will take for every single task, could be Infinity.
*/
public retries: number;
/**
* (get/set) defaults to 0, interval (milliseconds) between each retries.
*/
public retryInterval: number;
/**
* (get/set) defaults to Infinity, max retry interval when retry interval multiplier applied.
*/
public maxRetryInterval: number;
/**
* (get/set) defaults to 1, the multiplier applies to interval after every retry.
*/
public retryIntervalMultiplier: number;
private _index;
private _currentConcurrency;
public onProgress: (progress: IProgress) => void;
/**
* initialize a task pool.
* @param processor a function takes the data and index as parameters and returns a promise.
* @param concurrency the concurrency of this task pool.
* @param endless defaults to false. indicates whether this task pool is endless, if so, tasks can still be added even after all previous tasks have been fulfilled.
* @param tasksData an initializing array of task data.
*/
constructor(processor: (data: T, index: number) => Q.Promise<void>, concurrency: number, endless?: boolean, tasksData?: T[]);
/**
* add a data item.
* @param taskData task data to add.
*/
public add(taskData: T): void;
/**
* add data items.
* @param tasskData tasks data to add.
*/
public add(tasksData: T[]): void;
/**
* start tasks, return a promise that will be fulfilled after all tasks accomplish if endless is false.
* @param onProgress a callback that will be triggered every time when a single task is fulfilled.
*/
public start(onProgress?: (progress: IProgress) => void): Q.Promise<IResult>;
private _start();
private _process(data, index);
private _notifyProgress(index, success, err, retries);
private _next();
/**
* pause tasks and return a promise that will be fulfilled after the running tasks accomplish. this will wait for running tasks to complete instead of aborting them.
*/
public pause(): Q.Promise<void>;
/**
* resume tasks.
*/
public resume(): void;
/**
* pause tasks, then clear pending tasks data and reset counters. return a promise that will be fulfilled after resetting accomplish.
*/
public reset(): Q.Promise<void>;
}
}