From 87cd95fd92bf2adb426271938ede6fe42b8df4f4 Mon Sep 17 00:00:00 2001 From: Igor Oleinikov Date: Fri, 7 Mar 2014 14:07:28 +0400 Subject: [PATCH 1/2] 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; } } From 207d7d0faca60a073d8dcc8c981eedddf9e2879a Mon Sep 17 00:00:00 2001 From: Igor Oleinikov Date: Mon, 10 Mar 2014 21:26:58 +0400 Subject: [PATCH 2/2] promises-a-plus: updated tests of compatibility with implementation libraries --- promises-a-plus/promises-a-plus-tests.ts | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/promises-a-plus/promises-a-plus-tests.ts b/promises-a-plus/promises-a-plus-tests.ts index 5b10b1b6e..5ae93113a 100644 --- a/promises-a-plus/promises-a-plus-tests.ts +++ b/promises-a-plus/promises-a-plus-tests.ts @@ -1,5 +1,8 @@ /// /// +/// +/// +/// var thenNum: PromisesAPlus.Thenable; var thenStr: PromisesAPlus.Thenable; @@ -35,7 +38,44 @@ function testCompatibleWithRxJS() { var obsNum: Rx.Observable; + // implementation usage thenNum = obsNum.toPromise>(impl); thenNum = obsNum.toPromise(impl); obsNum.toPromise(PromiseImpl); } + +function testCompatibleWithES6Promises() { + // from spec to ES6 + var es6ThenNum: Thenable = thenNum; + var es6ThenStr: Thenable = thenStr; + + // from ES6 to spec + thenNum = es6ThenNum; + thenStr = es6ThenStr; + + // implementation + impl = Promise; +} + +function testCompatibleWithQ() { + // from spec to ES6 + var qThenNum: Q.IPromise = thenNum; + var qThenStr: Q.IPromise = thenStr; + + // from ES6 to spec + thenNum = qThenNum; + thenStr = qThenStr; + + // there's no standart implementation with constructor behaviour +} + +function testCompatibleWithWhen() { + var whenPromiseNum: When.Promise; + var whenPromiseStr: When.Promise; + + // from ES6 to spec + thenNum = whenPromiseNum; + thenStr = whenPromiseStr; + + // there's no standart implementation with constructor behaviour +}