Skip to content

Commit

Permalink
Support plain Node.js promises, clean up and bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Apr 2, 2016
1 parent 05d6515 commit 99c8608
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ API
> Methods:
> > **new( )** <sup>&rArr; <code>[Task](#api-Task)</code></sup>
> > &emsp;&#x25aa; func <sup><code>() => PromiseType</code></sup>
> > **.start( )** <sup>&rArr; <code>PromiseType</code></sup>
> > &emsp;<em>Start the task immediately and call onFinish callback when done.</em>
> > &emsp;&#x25aa; onFinish <sup><code>() => void</code></sup>
> > **.delay( )** <sup>&rArr; <code>PromiseType</code></sup>
> > &emsp;<em>Wrap task result in a new promise so it can be resolved later.</em>
> > &emsp;&#x25aa; Promise <sup><code>[PromisyClass](#api-PromisyClass)</code></sup>
> > **.resume( )** <sup>&rArr; <code>PromiseType</code></sup>
> > &emsp;<em>Resolve the result of a delayed task and call onFinish when done.</em>
> > &emsp;<em>Start the task and call onFinish when done.</em>
> > &emsp;&#x25aa; onFinish <sup><code>() => void</code></sup>
>
> <a name="api-TaskQueue"></a>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cwait",
"version": "0.0.1",
"version": "0.0.2",
"description": "Limit number of promises running in parallel",
"main": "dist/cwait.js",
"typings": "dist/cwait.d.ts",
Expand Down
36 changes: 19 additions & 17 deletions src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
/** Basic functionality we need promises to implement. @ignore internal use. */

export interface Promisy<PromiseType> {
then(handler: any): PromiseType;
catch(handler: any): PromiseType;
finally(handler: any): PromiseType;
then(resolved: (result?: any) => any, rejected?: (err?: any) => any): PromiseType;
}

/** Promise class shim with basic functionality. @ignore internal use. */
Expand All @@ -22,29 +20,33 @@ export class Task<PromiseType extends Promisy<PromiseType>> {
this.func = func;
}

/** Start the task immediately and call onFinish callback when done. */

start(onFinish: () => void) {
return(this.func().finally(onFinish));
}

/** Wrap task result in a new promise so it can be resolved later. */

delay(Promise: PromisyClass<PromiseType>) {
return(new Promise((resolve: any, reject: any) => {
this.resolve = resolve;
this.reject = reject;
}));
if(!this.promise) {
this.promise = new Promise((resolve: any, reject: any) => {
this.resolve = resolve;
this.reject = reject;
});
}

return(this.promise);
}

/** Resolve the result of a delayed task and call onFinish when done. */
/** Start the task and call onFinish when done. */

resume(onFinish: () => void) {
return(this.start(onFinish).then(this.resolve).catch(this.reject));
var result = this.func();

result.then(onFinish, onFinish);
if(this.resolve) result.then(this.resolve, this.reject);

return(result);
}

private func: () => PromiseType;

private resolve: any;
private reject: any;
private promise: PromiseType;
private resolve: (result: any) => void;
private reject: (err: any) => void;
}
13 changes: 10 additions & 3 deletions src/TaskQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class TaskQueue<PromiseType extends Promisy<PromiseType>> {
constructor(Promise: PromisyClass<PromiseType>, concurrency: number) {
this.Promise = Promise;
this.concurrency = concurrency;
this.nextBound = () => this.next();
}

/** Add a new task to the queue.
Expand All @@ -18,7 +19,11 @@ export class TaskQueue<PromiseType extends Promisy<PromiseType>> {

++this.busyCount;

return(func().finally(() => this.next()));
var result = func();

result.then(this.nextBound, this.nextBound);

return(result);
} else {
// Schedule the task and return a promise resolving
// to the result of task.start().
Expand All @@ -43,14 +48,16 @@ export class TaskQueue<PromiseType extends Promisy<PromiseType>> {
private next() {
var task = this.backlog.shift();

if(task) task.resume(() => this.next());
if(task) task.resume(this.nextBound);
else --this.busyCount;
}

private nextBound: () => void;

private Promise: PromisyClass<PromiseType>;

/** Number of promises allowed to resolve concurrently. */
concurrency = 2;
concurrency: number;

private backlog: Task<PromiseType>[] = [];
private busyCount = 0;
Expand Down

0 comments on commit 99c8608

Please sign in to comment.