From 87cd95fd92bf2adb426271938ede6fe42b8df4f4 Mon Sep 17 00:00:00 2001 From: Igor Oleinikov Date: Fri, 7 Mar 2014 14:07:28 +0400 Subject: [PATCH] promises-a-plus: added initial defintions rx.js: modified IPromise in Async module to match Promises/A+ --- promises-a-plus/promises-a-plus-tests.ts | 41 ++++++++++++++++++++++++ promises-a-plus/promises-a-plus.d.ts | 16 +++++++++ rx.js/rx.async.d.ts | 6 ++-- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 promises-a-plus/promises-a-plus-tests.ts create mode 100644 promises-a-plus/promises-a-plus.d.ts diff --git a/promises-a-plus/promises-a-plus-tests.ts b/promises-a-plus/promises-a-plus-tests.ts new file mode 100644 index 000000000..5b10b1b6e --- /dev/null +++ b/promises-a-plus/promises-a-plus-tests.ts @@ -0,0 +1,41 @@ +/// +/// + +var thenNum: PromisesAPlus.Thenable; +var thenStr: PromisesAPlus.Thenable; +var thenBool: PromisesAPlus.Thenable; + +var impl: PromisesAPlus.PromiseImpl; + +declare class PromiseImpl { + constructor(resolver: (resolvePromise: (value: R) => void, rejectPromise: (reason: any) => void) => void); + + then(onFulfill: (value: R) => PromiseImpl, onReject: (error: any) => PromiseImpl): PromiseImpl; + then(onFulfill: (value: R) => PromiseImpl, onReject?: (error: any) => U): PromiseImpl; + then(onFulfill: (value: R) => U, onReject: (error: any) => PromiseImpl): PromiseImpl; + then(onFulfill?: (value: R) => U, onReject?: (error: any) => U): PromiseImpl; +} + +function testCompatibleWithPromiseImpl() { + var pNum: PromiseImpl = thenNum; + + thenNum = pNum; + + impl = PromiseImpl; +} + +function testCompatibleWithRxJS() { + // from spec to Rx + var rxThenNum: Rx.IPromise = thenNum; + var rxThenStr: Rx.IPromise = thenStr; + + // from Rx to spec + thenNum = rxThenNum; + thenStr = rxThenStr; + + var obsNum: Rx.Observable; + + thenNum = obsNum.toPromise>(impl); + thenNum = obsNum.toPromise(impl); + obsNum.toPromise(PromiseImpl); +} diff --git a/promises-a-plus/promises-a-plus.d.ts b/promises-a-plus/promises-a-plus.d.ts new file mode 100644 index 000000000..7a3bee776 --- /dev/null +++ b/promises-a-plus/promises-a-plus.d.ts @@ -0,0 +1,16 @@ +declare module PromisesAPlus { + interface PromiseCtor { + (resolver: (resolvePromise: (value: T) => void, rejectPromise: (reason: any) => void) => void): Thenable; + } + + interface PromiseImpl { + new (resolver: (resolvePromise: (value: T) => void, rejectPromise: (reason: any) => void) => void): Thenable; + } + + interface Thenable { + then(onFulfill: (value: R) => Thenable, onReject: (error: any) => Thenable): Thenable; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Thenable; + then(onFulfill: (value: R) => U, onReject: (error: any) => Thenable): Thenable; + then(onFulfill?: (value: R) => U, onReject?: (error: any) => U): Thenable; + } +} diff --git a/rx.js/rx.async.d.ts b/rx.js/rx.async.d.ts index cdd93b9a8..7238acafb 100644 --- a/rx.js/rx.async.d.ts +++ b/rx.js/rx.async.d.ts @@ -104,9 +104,9 @@ declare module Rx { * Promise A+ */ export interface IPromise { - then(onFulfilled?: (value: T) => IPromise, onRejected?: (reason: any) => IPromise): IPromise; - then(onFulfilled?: (value: T) => IPromise, onRejected?: (reason: any) => R): IPromise; - then(onFulfilled?: (value: T) => R, onRejected?: (reason: any) => IPromise): IPromise; + then(onFulfilled: (value: T) => IPromise, onRejected: (reason: any) => IPromise): IPromise; + then(onFulfilled: (value: T) => IPromise, onRejected?: (reason: any) => R): IPromise; + then(onFulfilled: (value: T) => R, onRejected: (reason: any) => IPromise): IPromise; then(onFulfilled?: (value: T) => R, onRejected?: (reason: any) => R): IPromise; } }