From 4f0e22c01833cff6db757b71f0e8568a80765df5 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Mon, 14 Apr 2014 15:23:05 +0300 Subject: [PATCH] promises refactored --- Promises/promises.ts | 210 ++++++++++++++++++++--------------------- net/http_client.ios.ts | 6 +- 2 files changed, 107 insertions(+), 109 deletions(-) diff --git a/Promises/promises.ts b/Promises/promises.ts index c675e5583..deb1fd8e0 100644 --- a/Promises/promises.ts +++ b/Promises/promises.ts @@ -1,114 +1,112 @@ -export module Promises { - export function when(...promises: Promise[]): Promise { - var all_done = new Deferred(); - var results = []; - var remaining = promises.length; - promises.map( - (p, i) => { - p.then( - function (...args: any[]) { - results[i] = args; - remaining--; - if (!remaining && all_done.status() !== 'rejected') { - all_done.resolve.apply(all_done, results); - } - }, - function () { all_done.reject() } +export function when(...promises: Promise[]): Promise { + var all_done = new Deferred(); + var results = []; + var remaining = promises.length; + + promises.map( + (p, i) => { + p.then( + function (...args: any[]) { + results[i] = args; + remaining--; + if (!remaining && all_done.status() !== 'rejected') { + all_done.resolve.apply(all_done, results); + } + }, + function () { all_done.reject() } + ); + } + ); + + if (!remaining) { + all_done.resolve.apply(all_done, results); + } + + return all_done.promise(); +} + +export class Promise { + + constructor(private deferred: Deferred) { } + + then(callback: Function, error: Function): Promise { + return this.deferred.then(callback, error); + } + + status(): string { return this.deferred.status() } + result(): any[] { return this.deferred.result() } +} + +export class Deferred { + + private resolved: Function[] = []; + private rejected: Function[] = []; + private _status: string; + private _result: any[]; + private _promise: Promise; + + constructor() { + this._promise = new Promise(this); + this._status = 'in progress'; + } + + promise(): Promise { return this._promise } + status(): string { return this._status } + result(): any[] { return this._result } + + resolve(...result: any[]): Deferred { + this._result = result; + this.notify(this.resolved, result); + this.resolved = []; + this._status = 'resolved'; + return this; + } + + reject(...result: any[]): Deferred { + this._result = result; + this.notify(this.rejected, result); + this.rejected = []; + this._status = 'rejected'; + return this; + } + + then(callback: Function, error: Function): Promise { + var d = new Deferred(); + + this.resolved.push(this.wrap(d, callback, 'resolve')); + + if (error) { + this.rejected.push(this.wrap(d, error, 'reject')); + } + + if (this._status === 'resolved') { + this.resolve.apply(this, this.result); + } + else if (this._status === 'rejected' && error) { + this.reject.apply(this, this.result); + } + + return d.promise(); + } + + private wrap(d: Deferred, f: Function, method: string): Function { + return function (...args: any[]) { + var result = f.apply(f, args); + if (result && result instanceof Promise) { + result.then( + function () { d.resolve() }, + function () { d.reject() } ); } - ); - - if (!remaining) { - all_done.resolve.apply(all_done, results); + else { + d[method].apply(d, typeof result === 'array' ? result : [result]); + } + } } - return all_done.promise(); + private notify(funcs: Function[], result: any[]): void { + funcs.map((f) => { f.apply(f, result) }); } - - export class Promise { - - constructor(private deferred: Deferred) { } - - then(callback: Function, error: Function): Promise { - return this.deferred.then(callback, error); - } - - status(): string { return this.deferred.status() } - result(): any[] { return this.deferred.result() } - } - - export class Deferred { - - private resolved: Function[] = []; - private rejected: Function[] = []; - private _status: string; - private _result: any[]; - private _promise: Promise; - - constructor() { - this._promise = new Promise(this); - this._status = 'in progress'; - } - - promise(): Promise { return this._promise } - status(): string { return this._status } - result(): any[] { return this._result } - - resolve(...result: any[]): Deferred { - this._result = result; - this.notify(this.resolved, result); - this.resolved = []; - this._status = 'resolved'; - return this; - } - - reject(...result: any[]): Deferred { - this._result = result; - this.notify(this.rejected, result); - this.rejected = []; - this._status = 'rejected'; - return this; - } - - then(callback: Function, error: Function): Promise { - var d = new Deferred(); - - this.resolved.push(this.wrap(d, callback, 'resolve')); - - if (error) { - this.rejected.push(this.wrap(d, error, 'reject')); - } - - if (this._status === 'resolved') { - this.resolve.apply(this, this.result); - } - else if (this._status === 'rejected' && error) { - this.reject.apply(this, this.result); - } - - return d.promise(); - } - - private wrap(d: Deferred, f: Function, method: string): Function { - return function (...args: any[]) { - var result = f.apply(f, args); - if (result && result instanceof Promise) { - result.then( - function () { d.resolve() }, - function () { d.reject() } - ); - } - else { - d[method].apply(d, typeof result === 'array' ? result : [result]); - } - } - } - - private notify(funcs: Function[], result: any[]): void { - funcs.map((f) => { f.apply(f, result) }); - } - } - } \ No newline at end of file diff --git a/net/http_client.ios.ts b/net/http_client.ios.ts index 674abeb54..a5b72c62b 100644 --- a/net/http_client.ios.ts +++ b/net/http_client.ios.ts @@ -3,7 +3,7 @@ */ import image_module = require("Image/image"); -import promises_module = require("promises/promises"); +import promises = require("promises/promises"); export class HttpClient { /** @@ -24,7 +24,7 @@ export class HttpClient { } public static getString(url : string) { - var d = new promises_module.Promises.Deferred(); + var d = new promises.Deferred(); new HttpClient().getString(url, r => d.resolve(r), e => d.reject(e)); return d.promise(); } @@ -56,7 +56,7 @@ export class HttpClient { private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void) { if (!successCallback && !errorCallback) { - var d = new promises_module.Promises.Deferred(); + var d = new promises.Deferred(); HttpClient.getUrl(url, r => d.resolve(r), e => d.reject(e)); return d.promise(); }