mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-20 07:26:11 +08:00
promises changed to different (generic) library
This commit is contained in:
@ -10,8 +10,8 @@ export class http {
|
||||
/**
|
||||
* Gets string from url.
|
||||
*/
|
||||
public static getString(url: string): promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
public static getString(url: string): promises.Promise<string> {
|
||||
var d = promises.defer<string>();
|
||||
http.get(url, r => d.resolve(r), e => d.reject(e));
|
||||
return d.promise();
|
||||
}
|
||||
@ -19,8 +19,8 @@ export class http {
|
||||
/**
|
||||
* Gets JSON from url.
|
||||
*/
|
||||
public static getJSON(url: string): promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
public static getJSON<T>(url: string): promises.Promise<T> {
|
||||
var d = promises.defer<T>();
|
||||
http.get(url, r => d.resolve(JSON.parse(r)), e => d.reject(e));
|
||||
return d.promise();
|
||||
}
|
||||
@ -28,8 +28,8 @@ export class http {
|
||||
/**
|
||||
* Gets image from url.
|
||||
*/
|
||||
public static getImage(url: string): promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
public static getImage(url: string): promises.Promise<image_module.Image> {
|
||||
var d = promises.defer<image_module.Image>();
|
||||
http.get(url, r => {
|
||||
var image = new image_module.Image();
|
||||
image.loadFromBitmap(r);
|
||||
|
6
net/http_client.d.ts
vendored
6
net/http_client.d.ts
vendored
@ -5,7 +5,7 @@ import image_module = require("Image/image");
|
||||
import promises = require("promises/promises");
|
||||
|
||||
export declare class http {
|
||||
private static getString(url: string): promises.Promise;
|
||||
private static getJSON(url: string): promises.Promise;
|
||||
private static getImage(url: string): promises.Promise;
|
||||
static getString(url: string): promises.Promise<string>;
|
||||
static getJSON<T>(url: string): promises.Promise<T>;
|
||||
static getImage(url: string): promises.Promise<image_module.Image>;
|
||||
}
|
@ -9,8 +9,8 @@ export class http {
|
||||
/**
|
||||
* Gets string from url.
|
||||
*/
|
||||
public static getString(url : string) : promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
public static getString(url: string): promises.Promise<string> {
|
||||
var d = promises.defer<string>();
|
||||
http.get(url, r => d.resolve(Foundation.NSString.initWithDataEncoding(r, 4).toString()), e => d.reject(e));
|
||||
return d.promise();
|
||||
}
|
||||
@ -18,8 +18,8 @@ export class http {
|
||||
/**
|
||||
* Gets JSON from url.
|
||||
*/
|
||||
public static getJSON(url: string) : promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
public static getJSON<T>(url: string): promises.Promise<T> {
|
||||
var d = promises.defer<T>();
|
||||
http.get(url, r => d.resolve(JSON.parse(Foundation.NSString.initWithDataEncoding(r, 4).toString())), e => d.reject(e));
|
||||
return d.promise();
|
||||
}
|
||||
@ -27,8 +27,8 @@ export class http {
|
||||
/**
|
||||
* Gets image from url.
|
||||
*/
|
||||
public static getImage(url: string) : promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
public static getImage(url: string): promises.Promise<image_module.Image> {
|
||||
var d = promises.defer<image_module.Image>();
|
||||
http.get(url, r => {
|
||||
var image = new image_module.Image();
|
||||
image.loadFromData(r);
|
||||
|
@ -1,110 +1,449 @@
|
||||
export function when(...promises: Promise[]): Promise {
|
||||
var all_done = new Deferred();
|
||||
var results = [];
|
||||
var remaining = promises.length;
|
||||
/**
|
||||
Module P: Generic Promises for TypeScript
|
||||
|
||||
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() }
|
||||
);
|
||||
Project, documentation, and license: https://github.com/pragmatrix/Promise
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Returns a new "Deferred" value that may be resolved or rejected.
|
||||
*/
|
||||
|
||||
export function defer<Value>(): Deferred<Value> {
|
||||
return new DeferredI<Value>();
|
||||
}
|
||||
|
||||
/**
|
||||
Converts a value to a resolved promise.
|
||||
*/
|
||||
|
||||
export function resolve<Value>(v: Value): Promise<Value> {
|
||||
return defer<Value>().resolve(v).promise();
|
||||
}
|
||||
|
||||
/**
|
||||
Returns a rejected promise.
|
||||
*/
|
||||
|
||||
export function reject<Value>(err: Rejection): Promise<Value> {
|
||||
return defer<Value>().reject(err).promise();
|
||||
}
|
||||
|
||||
/**
|
||||
http://en.wikipedia.org/wiki/Anamorphism
|
||||
|
||||
Given a seed value, unfold calls the unspool function, waits for the returned promise to be resolved, and then
|
||||
calls it again if a next seed value was returned.
|
||||
|
||||
All the values of all promise results are collected into the resulting promise which is resolved as soon
|
||||
the last generated element value is resolved.
|
||||
*/
|
||||
|
||||
export function unfold<Seed, Element>(
|
||||
unspool: (current: Seed) => { promise: Promise<Element>; next?: Seed },
|
||||
seed: Seed)
|
||||
: Promise<Element[]> {
|
||||
var d = defer<Element[]>();
|
||||
var elements: Element[] = new Array<Element>();
|
||||
|
||||
unfoldCore<Seed, Element>(elements, d, unspool, seed)
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
function unfoldCore<Seed, Element>(
|
||||
elements: Element[],
|
||||
deferred: Deferred<Element[]>,
|
||||
unspool: (current: Seed) => { promise: Promise<Element>; next?: Seed },
|
||||
seed: Seed): void {
|
||||
var result = unspool(seed);
|
||||
if (!result) {
|
||||
deferred.resolve(elements);
|
||||
return;
|
||||
}
|
||||
|
||||
// fastpath: don't waste stack space if promise resolves immediately.
|
||||
|
||||
while (result.next && result.promise.status == Status.Resolved) {
|
||||
elements.push(result.promise.result);
|
||||
result = unspool(result.next);
|
||||
if (!result) {
|
||||
deferred.resolve(elements);
|
||||
return;
|
||||
}
|
||||
);
|
||||
|
||||
if (!remaining) {
|
||||
all_done.resolve.apply(all_done, results);
|
||||
}
|
||||
|
||||
return all_done.promise();
|
||||
result.promise
|
||||
.done(v => {
|
||||
elements.push(v);
|
||||
if (!result.next)
|
||||
deferred.resolve(elements);
|
||||
else
|
||||
unfoldCore<Seed, Element>(elements, deferred, unspool, result.next);
|
||||
})
|
||||
.fail(e => {
|
||||
deferred.reject(e);
|
||||
});
|
||||
}
|
||||
|
||||
export class Promise {
|
||||
/**
|
||||
The status of a Promise. Initially a Promise is Unfulfilled and may
|
||||
change to Rejected or Resolved.
|
||||
|
||||
Once a promise is either Rejected or Resolved, it can not change its
|
||||
status anymore.
|
||||
*/
|
||||
|
||||
constructor(private deferred: Deferred) { }
|
||||
export enum Status {
|
||||
Unfulfilled,
|
||||
Rejected,
|
||||
Resolved
|
||||
}
|
||||
|
||||
then(callback: Function, error?: Function): Promise {
|
||||
return this.deferred.then(callback, error);
|
||||
/**
|
||||
If a promise gets rejected, at least a message that indicates the error or
|
||||
reason for the rejection must be provided.
|
||||
*/
|
||||
|
||||
export interface Rejection {
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
Both Promise<T> and Deferred<T> share these properties.
|
||||
*/
|
||||
|
||||
export interface PromiseState<Value> {
|
||||
/// The current status of the promise.
|
||||
status: Status;
|
||||
|
||||
/// If the promise got resolved, the result of the promise.
|
||||
result?: Value;
|
||||
|
||||
/// If the promise got rejected, the rejection message.
|
||||
error?: Rejection;
|
||||
}
|
||||
|
||||
/**
|
||||
A Promise<Value> supports basic composition and registration of handlers that are called when the
|
||||
promise is fulfilled.
|
||||
|
||||
When multiple handlers are registered with done(), fail(), or always(), they are called in the
|
||||
same order.
|
||||
*/
|
||||
|
||||
export interface Promise<Value> extends PromiseState<Value> {
|
||||
/**
|
||||
Returns a promise that represents a promise chain that consists of this
|
||||
promise and the promise that is returned by the function provided.
|
||||
The function receives the value of this promise as soon it is resolved.
|
||||
|
||||
If this promise fails, the function is never called and the returned promise
|
||||
will also fail.
|
||||
*/
|
||||
then<T2>(f: (v: Value) => Promise<T2>): Promise<T2>;
|
||||
then<T2>(f: (v: Value) => T2): Promise<T2>;
|
||||
|
||||
/// Add a handler that is called when the promise gets resolved.
|
||||
done(f: (v: Value) => void): Promise<Value>;
|
||||
/// Add a handler that is called when the promise gets rejected.
|
||||
fail(f: (err: Rejection) => void): Promise<Value>;
|
||||
/// Add a handler that is called when the promise gets fulfilled (either resolved or rejected).
|
||||
always(f: (v?: Value, err?: Rejection) => void): Promise<Value>;
|
||||
}
|
||||
|
||||
/**
|
||||
Deferred<Value> supports the explicit resolving and rejecting of the
|
||||
promise and the registration of fulfillment handlers.
|
||||
|
||||
A Deferred<Value> should be only visible to the function that initially sets up
|
||||
an asynchronous process. Callers of that function should only see the Promise<Value> that
|
||||
is returned by promise().
|
||||
*/
|
||||
|
||||
export interface Deferred<Value> extends PromiseState<Value> {
|
||||
/// Returns the encapsulated promise of this deferred instance.
|
||||
/// The returned promise supports composition but removes the ability to resolve or reject
|
||||
/// the promise.
|
||||
promise(): Promise<Value>;
|
||||
|
||||
/// Resolve the promise.
|
||||
resolve(result: Value): Deferred<Value>;
|
||||
/// Reject the promise.
|
||||
reject(err: Rejection): Deferred<Value>;
|
||||
|
||||
done(f: (v: Value) => void): Deferred<Value>;
|
||||
fail(f: (err: Rejection) => void): Deferred<Value>;
|
||||
always(f: (v?: Value, err?: Rejection) => void): Deferred<Value>;
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a promise that gets resolved when all the promises in the argument list get resolved.
|
||||
As soon one of the arguments gets rejected, the resulting promise gets rejected.
|
||||
If no promises were provided, the resulting promise is immediately resolved.
|
||||
*/
|
||||
|
||||
export function when(...promises: Promise<any>[]): Promise<any[]> {
|
||||
var allDone = defer<any[]>();
|
||||
if (!promises.length) {
|
||||
allDone.resolve([]);
|
||||
return allDone.promise();
|
||||
}
|
||||
|
||||
status(): string { return this.deferred.status() }
|
||||
result(): any[] { return this.deferred.result() }
|
||||
var resolved = 0;
|
||||
var results = [];
|
||||
|
||||
promises.forEach((p, i) => {
|
||||
p
|
||||
.done(v => {
|
||||
results[i] = v;
|
||||
++resolved;
|
||||
if (resolved === promises.length && allDone.status !== Status.Rejected)
|
||||
allDone.resolve(results);
|
||||
})
|
||||
.fail(e => {
|
||||
if (allDone.status !== Status.Rejected)
|
||||
allDone.reject(new Error("when: one or more promises were rejected"));
|
||||
});
|
||||
});
|
||||
|
||||
return allDone.promise();
|
||||
}
|
||||
|
||||
export class Deferred {
|
||||
/**
|
||||
Implementation of a promise.
|
||||
|
||||
private resolved: Function[] = [];
|
||||
private rejected: Function[] = [];
|
||||
private _status: string;
|
||||
private _result: any[];
|
||||
private _promise: Promise;
|
||||
The Promise<Value> instance is a proxy to the Deferred<Value> instance.
|
||||
*/
|
||||
|
||||
class PromiseI<Value> implements Promise<Value>
|
||||
{
|
||||
constructor(public deferred: DeferredI<Value>)
|
||||
{ }
|
||||
|
||||
get status(): Status { return this.deferred.status; }
|
||||
get result(): Value { return this.deferred.result; }
|
||||
get error(): Rejection { return this.deferred.error; }
|
||||
|
||||
done(f: (v: Value) => void): Promise<Value> {
|
||||
this.deferred.done(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
fail(f: (err: Rejection) => void): Promise<Value> {
|
||||
this.deferred.fail(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
always(f: (v?: Value, err?: Rejection) => void): Promise<Value> {
|
||||
this.deferred.always(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
then<T2>(f: (v: Value) => any): Promise<T2> {
|
||||
return this.deferred.then<any>(f);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Implementation of a deferred.
|
||||
*/
|
||||
|
||||
class DeferredI<Value> implements Deferred<Value>{
|
||||
|
||||
private _resolved: (v: Value) => void = _ => { };
|
||||
private _rejected: (err: Rejection) => void = _ => { };
|
||||
|
||||
private _status: Status = Status.Unfulfilled;
|
||||
private _result: Value;
|
||||
private _error: Rejection = { message: "" };
|
||||
private _promise: Promise<Value>;
|
||||
|
||||
constructor() {
|
||||
this._promise = new Promise(this);
|
||||
this._status = 'in progress';
|
||||
this._promise = new PromiseI<Value>(this);
|
||||
}
|
||||
|
||||
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;
|
||||
promise(): Promise<Value> {
|
||||
return this._promise;
|
||||
}
|
||||
|
||||
reject(...result: any[]): Deferred {
|
||||
this._result = result;
|
||||
this.notify(this.rejected, result);
|
||||
this.rejected = [];
|
||||
this._status = 'rejected';
|
||||
return this;
|
||||
get status(): Status {
|
||||
return this._status;
|
||||
}
|
||||
|
||||
then(callback: Function, error: Function): Promise {
|
||||
var d = new Deferred();
|
||||
get result(): Value {
|
||||
if (this._status != Status.Resolved)
|
||||
throw new Error("Promise: result not available");
|
||||
return this._result;
|
||||
}
|
||||
|
||||
this.resolved.push(this.wrap(d, callback, 'resolve'));
|
||||
get error(): Rejection {
|
||||
if (this._status != Status.Rejected)
|
||||
throw new Error("Promise: rejection reason not available");
|
||||
return this._error;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
this.rejected.push(this.wrap(d, error, 'reject'));
|
||||
}
|
||||
then<Result>(f: (v: Value) => any): Promise<Result> {
|
||||
var d = defer<Result>();
|
||||
|
||||
if (this._status === 'resolved') {
|
||||
this.resolve.apply(this, this.result);
|
||||
}
|
||||
else if (this._status === 'rejected' && error) {
|
||||
this.reject.apply(this, this.result);
|
||||
}
|
||||
this
|
||||
.done(v => {
|
||||
var promiseOrValue = f(v);
|
||||
|
||||
// todo: need to find another way to check if r is really of interface
|
||||
// type Promise<any>, otherwise we would not support other
|
||||
// implementations here.
|
||||
if (promiseOrValue instanceof PromiseI) {
|
||||
var p = <Promise<Result>> promiseOrValue;
|
||||
p.done(v2 => d.resolve(v2))
|
||||
.fail(err => d.reject(err));
|
||||
return p;
|
||||
}
|
||||
|
||||
d.resolve(promiseOrValue);
|
||||
})
|
||||
.fail(err => d.reject(err));
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
done(f: (v: Value) => void): Deferred<Value> {
|
||||
if (this.status === Status.Resolved) {
|
||||
f(this._result);
|
||||
return this;
|
||||
}
|
||||
|
||||
private notify(funcs: Function[], result: any[]): void {
|
||||
funcs.map((f) => { f.apply(f, result) });
|
||||
if (this.status !== Status.Unfulfilled)
|
||||
return this;
|
||||
|
||||
var prev = this._resolved;
|
||||
this._resolved = v => { prev(v); f(v); }
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
fail(f: (err: Rejection) => void): Deferred<Value> {
|
||||
if (this.status === Status.Rejected) {
|
||||
f(this._error);
|
||||
return this;
|
||||
}
|
||||
|
||||
if (this.status !== Status.Unfulfilled)
|
||||
return this;
|
||||
|
||||
var prev = this._rejected;
|
||||
this._rejected = e => { prev(e); f(e); }
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
always(f: (v?: Value, err?: Rejection) => void): Deferred<Value> {
|
||||
this
|
||||
.done(v => f(v))
|
||||
.fail(err => f(null, err));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
resolve(result: Value) {
|
||||
if (this._status !== Status.Unfulfilled)
|
||||
throw new Error("tried to resolve a fulfilled promise");
|
||||
|
||||
this._result = result;
|
||||
this._status = Status.Resolved;
|
||||
this._resolved(result);
|
||||
|
||||
this.detach();
|
||||
return this;
|
||||
}
|
||||
|
||||
reject(err: Rejection) {
|
||||
if (this._status !== Status.Unfulfilled)
|
||||
throw new Error("tried to reject a fulfilled promise");
|
||||
|
||||
this._error = err;
|
||||
this._status = Status.Rejected;
|
||||
this._rejected(err);
|
||||
|
||||
this.detach();
|
||||
return this;
|
||||
}
|
||||
|
||||
private detach() {
|
||||
this._resolved = _ => { };
|
||||
this._rejected = _ => { };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Promise Generators and Iterators.
|
||||
*/
|
||||
|
||||
export interface Generator<E> {
|
||||
(): Iterator<E>;
|
||||
}
|
||||
|
||||
export interface Iterator<E> {
|
||||
advance(): Promise<boolean>;
|
||||
current: E;
|
||||
}
|
||||
|
||||
export function generator<E>(g: () => () => Promise<E>): Generator<E> {
|
||||
return () => iterator<E>(g());
|
||||
};
|
||||
|
||||
export function iterator<E>(f: () => Promise<E>): Iterator<E> {
|
||||
return new IteratorI<E>(f);
|
||||
}
|
||||
|
||||
class IteratorI<E> implements Iterator<E>
|
||||
{
|
||||
current: E = undefined;
|
||||
|
||||
constructor(private f: () => Promise<E>)
|
||||
{ }
|
||||
|
||||
advance(): Promise<boolean> {
|
||||
var res = this.f();
|
||||
return res.then(value => {
|
||||
if (isUndefined(value))
|
||||
return false;
|
||||
|
||||
this.current = value;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Iterator functions.
|
||||
*/
|
||||
|
||||
export function each<E>(gen: Generator<E>, f: (e: E) => void): Promise<{}> {
|
||||
var d = defer();
|
||||
eachCore(d, gen(), f);
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
function eachCore<E>(fin: Deferred<{}>, it: Iterator<E>, f: (e: E) => void): void {
|
||||
it.advance()
|
||||
.done(hasValue => {
|
||||
if (!hasValue) {
|
||||
fin.resolve({});
|
||||
return;
|
||||
}
|
||||
|
||||
f(it.current)
|
||||
eachCore<E>(fin, it, f);
|
||||
})
|
||||
.fail(err => fin.reject(err));
|
||||
}
|
||||
|
||||
/**
|
||||
std
|
||||
*/
|
||||
|
||||
export function isUndefined(v) {
|
||||
return typeof v === 'undefined';
|
||||
}
|
||||
|
Reference in New Issue
Block a user