mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 05:18:39 +08:00
Promises added and implemented experimentally in WebClient
This commit is contained in:
@ -146,6 +146,8 @@
|
|||||||
<TypeScriptCompile Include="Camera\index.ts" />
|
<TypeScriptCompile Include="Camera\index.ts" />
|
||||||
<TypeScriptCompile Include="Application\index.ts" />
|
<TypeScriptCompile Include="Application\index.ts" />
|
||||||
<TypeScriptCompile Include="android17.d.ts" />
|
<TypeScriptCompile Include="android17.d.ts" />
|
||||||
|
<TypeScriptCompile Include="Promises\promises.ts" />
|
||||||
|
<TypeScriptCompile Include="Promises\index.ts" />
|
||||||
<Content Include="_references.ts" />
|
<Content Include="_references.ts" />
|
||||||
<TypeScriptCompile Include="Console\console.android.ts">
|
<TypeScriptCompile Include="Console\console.android.ts">
|
||||||
<DependentUpon>console.d.ts</DependentUpon>
|
<DependentUpon>console.d.ts</DependentUpon>
|
||||||
|
2
Promises/index.ts
Normal file
2
Promises/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
declare var module, require;
|
||||||
|
module.exports = require("Promises/promises");
|
114
Promises/promises.ts
Normal file
114
Promises/promises.ts
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
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() }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
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() }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
d[method].apply(d, typeof result === 'array' ? result : [result]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private notify(funcs: Function[], result: any[]): void {
|
||||||
|
funcs.map((f) => { f.apply(f, result) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
8
WebClient/web_client.d.ts
vendored
8
WebClient/web_client.d.ts
vendored
@ -4,8 +4,8 @@
|
|||||||
import image_module = require("Image/image");
|
import image_module = require("Image/image");
|
||||||
|
|
||||||
export declare class Client {
|
export declare class Client {
|
||||||
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void)
|
private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void)
|
||||||
getString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void)
|
getString(url: string, successCallback?: (result: string) => void, errorCallback?: (e: Error) => void)
|
||||||
getJSON(url: string, successCallback: (result: Object) => void, errorCallback?: (e: Error) => void)
|
getJSON(url: string, successCallback?: (result: Object) => void, errorCallback?: (e: Error) => void)
|
||||||
getImage(url: string, successCallback: (result: image_module.Image) => void, errorCallback?: (e: Error) => void)
|
getImage(url: string, successCallback?: (result: image_module.Image) => void, errorCallback?: (e: Error) => void)
|
||||||
}
|
}
|
@ -3,6 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import image_module = require("Image/image");
|
import image_module = require("Image/image");
|
||||||
|
import promises_module = require("Promises/promises");
|
||||||
|
|
||||||
export class Client {
|
export class Client {
|
||||||
/**
|
/**
|
||||||
@ -22,6 +23,12 @@ export class Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static getString(url : string) {
|
||||||
|
var d = new promises_module.Promises.Deferred();
|
||||||
|
new Client().getString(url, r => d.resolve(r), e => d.reject(e));
|
||||||
|
return d.promise();
|
||||||
|
}
|
||||||
|
|
||||||
public getJSON(url: string, successCallback: (result: Object) => void, errorCallback?: (e: Error) => void) {
|
public getJSON(url: string, successCallback: (result: Object) => void, errorCallback?: (e: Error) => void) {
|
||||||
try {
|
try {
|
||||||
this.getString(url, function (data) {
|
this.getString(url, function (data) {
|
||||||
@ -46,7 +53,18 @@ export class Client {
|
|||||||
}, errorCallback);
|
}, errorCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void) {
|
||||||
|
if (!successCallback && !errorCallback)
|
||||||
|
{
|
||||||
|
var d = new promises_module.Promises.Deferred();
|
||||||
|
Client.getUrl(url, r => d.resolve(r), e => d.reject(e));
|
||||||
|
return d.promise();
|
||||||
|
}
|
||||||
|
|
||||||
|
Client.getUrl(url, successCallback, errorCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getUrl(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
||||||
try {
|
try {
|
||||||
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
|
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
|
||||||
var queue = Foundation.NSOperationQueue.mainQueue();
|
var queue = Foundation.NSOperationQueue.mainQueue();
|
||||||
|
Reference in New Issue
Block a user