You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by er...@apache.org on 2014/12/10 13:56:12 UTC
[06/13] git commit: [flex-sdk] [refs/heads/develop] - Updated
promises package; all test pass
Updated promises package; all test pass
Signed-off-by: Erik de Bruin <er...@ixsoftware.nl>
Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/c77b4ea4
Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/c77b4ea4
Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/c77b4ea4
Branch: refs/heads/develop
Commit: c77b4ea4cd0c6bf250e6e58ad7b11d5416de3087
Parents: 16fb88a
Author: Erik de Bruin <er...@ixsoftware.nl>
Authored: Tue Dec 9 12:14:37 2014 +0100
Committer: Erik de Bruin <er...@ixsoftware.nl>
Committed: Wed Dec 10 13:30:04 2014 +0100
----------------------------------------------------------------------
.../src/org/apache/flex/promises/Promise.as | 129 +++++++------------
.../apache/flex/promises/interfaces/IPromise.as | 30 -----
.../flex/promises/interfaces/IThenable.as | 2 +-
.../tests/promises/cases/PromisesBasicTests.as | 90 +++++++------
4 files changed, 100 insertions(+), 151 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/c77b4ea4/frameworks/projects/apache/src/org/apache/flex/promises/Promise.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/apache/src/org/apache/flex/promises/Promise.as b/frameworks/projects/apache/src/org/apache/flex/promises/Promise.as
index bfc62fa..29ecd08 100644
--- a/frameworks/projects/apache/src/org/apache/flex/promises/Promise.as
+++ b/frameworks/projects/apache/src/org/apache/flex/promises/Promise.as
@@ -20,14 +20,11 @@
package org.apache.flex.promises
{
-import flash.events.TimerEvent;
-import flash.utils.Timer;
-
import org.apache.flex.promises.enums.PromiseState;
-import org.apache.flex.promises.interfaces.IPromise;
+import org.apache.flex.promises.interfaces.IThenable;
import org.apache.flex.promises.vo.Handler;
-public class Promise implements IPromise
+public class Promise implements IThenable
{
@@ -38,11 +35,11 @@ public class Promise implements IPromise
//--------------------------------------------------------------------------
public function Promise(resolver:Function) {
- this.handlers_ = new Vector.<Handler>();
+ handlers_ = new Vector.<Handler>();
- this.state_ = PromiseState.PENDING;
+ state_ = PromiseState.PENDING;
- this.doResolve_(resolver, this.resolve_, this.reject_);
+ doResolve_(resolver, resolve_, reject_);
}
@@ -68,21 +65,6 @@ public class Promise implements IPromise
//--------------------------------------------------------------------------
//----------------------------------
- // done
- //----------------------------------
-
- public function done(onFulfilled:Function = null, onRejected:Function = null):void
- {
- //var self:Promise = this;
-
- //var timer:Timer = new Timer(0, 1);
- //timer.addEventListener(TimerEvent.TIMER, function ():void {
- this.handle_(new Handler(onFulfilled, onRejected));
- //});
- //timer.start();
- }
-
- //----------------------------------
// doResolve_
//----------------------------------
@@ -126,79 +108,65 @@ public class Promise implements IPromise
}
//----------------------------------
- // getThen_
- //----------------------------------
-
- private function getThen_(value:*):Function
- {
- var type:String = typeof value;
-
- if (value && (value === 'object' || value === 'function'))
- {
- var then:* = value.then;
-
- if (then is Function)
- {
- return then;
- }
- }
-
- return null;
- }
-
- //----------------------------------
// fulfill_
//----------------------------------
private function fulfill_(result:*):void
{
- this.state_ = PromiseState.FULFILLED;
+ state_ = PromiseState.FULFILLED;
- this.value_ = result;
+ value_ = result;
- this.handlers_.forEach(this.handle_);
-
- this.handlers_ = null;
+ processHandlers_();
}
//----------------------------------
// handle_
//----------------------------------
- private function handle_(handler:Object, ...rest):void
+ private function handle_(handler:Handler):void
{
- if (this.state_ === PromiseState.PENDING)
+ if (state_ === PromiseState.PENDING)
{
- trace(this.state_);
- this.handlers_.push(handler);
+ handlers_.push(handler);
}
else
{
- if (this.state_ === PromiseState.FULFILLED && handler.onFulfilled != null)
+ if (state_ === PromiseState.FULFILLED && handler.onFulfilled != null)
{
- handler.onFulfilled(this.value_);
+ handler.onFulfilled(value_);
}
- if (this.state_ === PromiseState.REJECTED && handler.onRejected != null)
+ if (state_ === PromiseState.REJECTED && handler.onRejected != null)
{
- handler.onRejected(this.value_);
+ handler.onRejected(value_);
}
}
}
//----------------------------------
+ // processHandlers_
+ //----------------------------------
+
+ private function processHandlers_():void
+ {
+ for (var i:int = 0, n:int = handlers_.length; i < n; i++)
+ {
+ handle_(handlers_.shift());
+ }
+ }
+
+ //----------------------------------
// reject_
//----------------------------------
private function reject_(error:*):void
{
- this.state_ = PromiseState.REJECTED;
-
- this.value_ = error;
+ state_ = PromiseState.REJECTED;
- this.handlers_.forEach(this.handle_);
+ value_ = error;
- this.handlers_ = null;
+ processHandlers_();
}
//----------------------------------
@@ -209,19 +177,20 @@ public class Promise implements IPromise
{
try
{
- var then:Function = this.getThen_(result);
-
- if (then != null) {
- this.doResolve_(then, this.resolve_, this.reject_);
-
- return;
+ if (result &&
+ (typeof(result) === 'object' || typeof(result) === 'function') &&
+ result.then is Function)
+ {
+ doResolve_(result.then, resolve_, reject_);
+ }
+ else
+ {
+ fulfill_(result);
}
-
- this.fulfill_(result);
}
catch (e:Error)
{
- this.reject_(e);
+ reject_(e);
}
}
@@ -230,13 +199,11 @@ public class Promise implements IPromise
//----------------------------------
public function then(onFulfilled:Function = null,
- onRejected:Function = null):IPromise
+ onRejected:Function = null):IThenable
{
- var self:IPromise = this;
-
- var resolver:Function = function (resolve:Function, reject:Function):* {
- return self.done(function (result:*):* {
- if (onFulfilled is Function)
+ return new Promise(function (resolve:Function, reject:Function):* {
+ handle_(new Handler(function (result:*):* {
+ if (typeof(onFulfilled) === 'function')
{
try
{
@@ -252,7 +219,7 @@ public class Promise implements IPromise
return resolve(result);
}
}, function (error:*):* {
- if (onRejected is Function)
+ if (typeof(onRejected) === 'function')
{
try
{
@@ -267,10 +234,8 @@ public class Promise implements IPromise
{
return reject(error);
}
- });
- };
-
- return new Promise(resolver);
+ }))
+ });
}
}
http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/c77b4ea4/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IPromise.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IPromise.as b/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IPromise.as
deleted file mode 100644
index 7c70b14..0000000
--- a/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IPromise.as
+++ /dev/null
@@ -1,30 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-//
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You under the Apache License, Version 2.0
-// (the "License"); you may not use this file except in compliance with
-// the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-////////////////////////////////////////////////////////////////////////////////
-
-package org.apache.flex.promises.interfaces
-{
-
-public interface IPromise extends IThenable
-{
-
- function done(onFulfilled:Function = null, onRejected:Function = null):void;
-
-}
-
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/c77b4ea4/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IThenable.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IThenable.as b/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IThenable.as
index d154d70..633cdba 100644
--- a/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IThenable.as
+++ b/frameworks/projects/apache/src/org/apache/flex/promises/interfaces/IThenable.as
@@ -23,7 +23,7 @@ package org.apache.flex.promises.interfaces
public interface IThenable
{
- function then(onFulfilled:Function = null, onRejected:Function = null):IPromise;
+ function then(onFulfilled:Function = null, onRejected:Function = null):IThenable;
}
http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/c77b4ea4/frameworks/projects/apache/src/tests/promises/cases/PromisesBasicTests.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/apache/src/tests/promises/cases/PromisesBasicTests.as b/frameworks/projects/apache/src/tests/promises/cases/PromisesBasicTests.as
index 494ff86..4d15250 100644
--- a/frameworks/projects/apache/src/tests/promises/cases/PromisesBasicTests.as
+++ b/frameworks/projects/apache/src/tests/promises/cases/PromisesBasicTests.as
@@ -27,7 +27,6 @@ import flash.utils.setTimeout;
import flexunit.framework.Assert;
import org.apache.flex.promises.Promise;
-import org.apache.flex.promises.interfaces.IPromise;
import org.apache.flex.promises.interfaces.IThenable;
import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertNotNull;
@@ -45,7 +44,7 @@ public class PromisesBasicTests
private var expected_:*;
- private var promise_:IPromise;
+ private var promise_:IThenable;
private var got_:*;
@@ -131,16 +130,15 @@ public class PromisesBasicTests
assertNotNull(promise_);
assertTrue(promise_ is IThenable);
- assertTrue(promise_ is IPromise);
assertTrue(promise_ is Promise);
}
//----------------------------------
- // test_SimpleSyncDone_FulFill
+ // test_SimpleSyncThen_FulFill
//----------------------------------
[Test(async)]
- public function test_SimpleSyncDone_FulFill():void
+ public function test_SimpleSyncThen_FulFill():void
{
Async.handleEvent(this, timer_, TimerEvent.TIMER_COMPLETE, verifyGot_);
@@ -152,15 +150,15 @@ public class PromisesBasicTests
});
expected_ = 'Hello world';
- promise_.done(parseGot_);
+ promise_.then(parseGot_);
}
//----------------------------------
- // test_SimpleSyncDone_Reject
+ // test_SimpleSyncThen_Reject
//----------------------------------
[Test(async)]
- public function test_SimpleSyncDone_Reject():void
+ public function test_SimpleSyncThen_Reject():void
{
Async.handleEvent(this, timer_, TimerEvent.TIMER_COMPLETE, verifyGot_);
@@ -172,23 +170,24 @@ public class PromisesBasicTests
});
expected_ = 'Error: reject';
- promise_.done(null, parseErrorGot_);
+ promise_.then(null, parseErrorGot_);
}
//----------------------------------
- // test_SimpleSyncThen_FulFill
+ // test_SimpleASyncThen_FulFill
//----------------------------------
[Test(async)]
- public function test_SimpleSyncThen_FulFill():void
+ public function test_SimpleASyncThen_FulFill():void
{
Async.handleEvent(this, timer_, TimerEvent.TIMER_COMPLETE, verifyGot_);
timer_.start();
- promise_ = new Promise(function (fulfill:Function = null, reject:Function = null):*
+ this.promise_ = new Promise(function (fulfill:Function = null,
+ reject:Function = null):*
{
- fulfill('Hello world');
+ setTimeout(function ():void { fulfill('Hello world'); }, 10);
});
expected_ = 'Hello world';
@@ -196,94 +195,109 @@ public class PromisesBasicTests
}
//----------------------------------
- // test_SimpleSyncThen_Reject
+ // test_SimpleASyncThen_Reject
//----------------------------------
[Test(async)]
- public function test_SimpleSyncThen_Reject():void
+ public function test_SimpleASyncThen_Reject():void
{
Async.handleEvent(this, timer_, TimerEvent.TIMER_COMPLETE, verifyGot_);
timer_.start();
- promise_ = new Promise(function (fulfill:Function = null, reject:Function = null):*
+ this.promise_ = new Promise(function (fulfill:Function = null,
+ reject:Function = null):*
{
- reject(new Error('reject'));
+ setTimeout(function ():void { reject(new Error('reject')); }, 10);
});
expected_ = 'Error: reject';
promise_.then(null, parseErrorGot_);
}
+
//----------------------------------
- // test_SimpleASyncThen_FulFill
+ // test_MultipleASyncThen_FulFill
//----------------------------------
[Test(async)]
- public function test_SimpleASyncThen_FulFill():void
+ public function test_MultipleASyncThen_FulFill():void
{
Async.handleEvent(this, timer_, TimerEvent.TIMER_COMPLETE, verifyGot_);
timer_.start();
- this.promise_ = new Promise(function (fulfill:Function = null,
- reject:Function = null):*
+ var anotherStep:Function = function (value:*):IThenable
+ {
+ return new Promise(function (fulfill:Function = null, reject:Function = null):*
+ {
+ setTimeout(function ():void { fulfill(value + ' ... again'); }, 10);
+ });
+ }
+
+ promise_ = new Promise(function (fulfill:Function = null, reject:Function = null):*
{
- setTimeout(function ():void { fulfill('Hello world'); }, 0);
+ setTimeout(function ():void { fulfill('Hello world'); }, 10);
});
- expected_ = 'Hello world';
- promise_.then(parseGot_);
+ expected_ = 'Hello world ... again';
+ promise_.then(anotherStep).then(parseGot_);
}
//----------------------------------
- // test_SimpleASyncThen_Reject
+ // test_MultipleASyncThen_RejectLast
//----------------------------------
[Test(async)]
- public function test_SimpleASyncThen_Reject():void
+ public function test_MultipleASyncThen_RejectLast():void
{
Async.handleEvent(this, timer_, TimerEvent.TIMER_COMPLETE, verifyGot_);
timer_.start();
- this.promise_ = new Promise(function (fulfill:Function = null,
- reject:Function = null):*
+ var anotherStep:Function = function (value:*):IThenable
+ {
+ return new Promise(function (fulfill:Function = null, reject:Function = null):*
+ {
+ setTimeout(function ():void { reject(new Error('reject')); }, 10);
+ });
+ }
+
+ promise_ = new Promise(function (fulfill:Function = null, reject:Function = null):*
{
- setTimeout(function ():void { reject(new Error('reject')); }, 0);
+ setTimeout(function ():void { fulfill('Hello world'); }, 10);
});
expected_ = 'Error: reject';
- promise_.then(null, parseErrorGot_);
+ promise_.then(anotherStep).then(null, parseErrorGot_);
}
-
//----------------------------------
- // test_MultipleASyncThen_FulFill
+ // test_MultipleASyncThen_RejectFirst
//----------------------------------
[Test(async)]
- public function test_MultipleASyncThen_FulFill():void
+ public function test_MultipleASyncThen_Reject():void
{
Async.handleEvent(this, timer_, TimerEvent.TIMER_COMPLETE, verifyGot_);
timer_.start();
- var anotherStep:Function = function (value:*):IPromise
+ var anotherStep:Function = function (value:*):IThenable
{
return new Promise(function (fulfill:Function = null, reject:Function = null):*
{
- setTimeout(function ():void { fulfill(value + ' ... again'); }, 0);
+ setTimeout(function ():void { fulfill(value + ' ... again'); }, 10);
});
}
promise_ = new Promise(function (fulfill:Function = null, reject:Function = null):*
{
- setTimeout(function ():void { fulfill('Hello world'); }, 0);
+ setTimeout(function ():void { reject(new Error('reject')); }, 10);
});
- expected_ = 'Hello worlds ... again';
- promise_.then(anotherStep).then(parseGot_);
+ expected_ = 'Error: reject';
+ promise_.then(anotherStep).then(null, parseErrorGot_);
}
}}
\ No newline at end of file