Repository: flex-asjs Updated Branches: refs/heads/develop 4fafd89a9 -> 5ecbcd701
Partial Promise implementation taken from the flex-sdk. Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/5ecbcd70 Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/5ecbcd70 Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/5ecbcd70 Branch: refs/heads/develop Commit: 5ecbcd701ce57a7feba4a6165872f5f60cde7e02 Parents: 4fafd89 Author: Harbs <[email protected]> Authored: Sun Mar 5 13:02:09 2017 +0200 Committer: Harbs <[email protected]> Committed: Sun Mar 5 13:02:09 2017 +0200 ---------------------------------------------------------------------- .../projects/Core/src/main/flex/Promise.as | 277 +++++++++++++++++++ .../apache/flex/promises/enums/PromiseState.as | 71 +++++ .../flex/promises/interfaces/IThenable.as | 31 +++ .../flex/org/apache/flex/promises/vo/Handler.as | 68 +++++ 4 files changed, 447 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5ecbcd70/frameworks/projects/Core/src/main/flex/Promise.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Core/src/main/flex/Promise.as b/frameworks/projects/Core/src/main/flex/Promise.as new file mode 100644 index 0000000..82076f3 --- /dev/null +++ b/frameworks/projects/Core/src/main/flex/Promise.as @@ -0,0 +1,277 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 +{ + + import org.apache.flex.promises.enums.PromiseState; + import org.apache.flex.promises.interfaces.IThenable; + import org.apache.flex.promises.vo.Handler; + + public class Promise implements IThenable + { + + + //-------------------------------------------------------------------------- + // + // Constructor + // + //-------------------------------------------------------------------------- + + public function Promise(resolver:Function) + { + handlers_ = new Vector.<Handler>(); + + state_ = PromiseState.PENDING; + + doResolve_(resolver, resolve_, reject_); + } + + public static function all(promises:Array):Promise + { + //TODO implement all() + return null; + } + + public static function race(promises:Array):Promise + { + //TODO implement race() + return null; + } + + public static function reject(reason:*):Promise + { + //TODO implement reject() + return null; + } + + public static function resolve(value:*):Promise + { + //TODO implement resolve() + return null; + } + + //-------------------------------------------------------------------------- + // + // Variables + // + //-------------------------------------------------------------------------- + + private var handlers_:Vector.<Handler>; + + private var state_:PromiseState; + + private var value_:*; + + + + //-------------------------------------------------------------------------- + // + // Methods + // + //-------------------------------------------------------------------------- + + //---------------------------------- + // doResolve_ + //---------------------------------- + + private function doResolve_(fn:Function, onFulfilled:Function, + onRejected:Function):void + { + var done:Boolean = false; + + try + { + fn(function (value:*):void { + if (done) + { + return; + } + + done = true; + + onFulfilled(value); + }, function (reason:*):void { + if (done) + { + return; + } + + done = true; + + onRejected(reason); + }); + } + catch (e:Error) + { + if (done) + { + return; + } + + done = true; + + onRejected(e); + } + } + + //---------------------------------- + // fulfill_ + //---------------------------------- + + private function fulfill_(result:*):void + { + state_ = PromiseState.FULFILLED; + + value_ = result; + + processHandlers_(); + } + + //---------------------------------- + // handle_ + //---------------------------------- + + private function handle_(handler:Handler):void + { + if (state_ === PromiseState.PENDING) + { + handlers_.push(handler); + } + else + { + if (state_ === PromiseState.FULFILLED && + handler.onFulfilled != null) + { + handler.onFulfilled(value_); + } + + if (state_ === PromiseState.REJECTED && + handler.onRejected != null) + { + 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 + { + state_ = PromiseState.REJECTED; + + value_ = error; + + processHandlers_(); + } + + //---------------------------------- + // resolve_ + //---------------------------------- + + private function resolve_(result:*):void + { + try + { + if (result && + (typeof(result) === 'object' || + typeof(result) === 'function') && + result.then is Function) + { + doResolve_(result.then, resolve_, reject_); + } + else + { + fulfill_(result); + } + } + catch (e:Error) + { + reject_(e); + } + } + + //---------------------------------- + // then + //---------------------------------- + public function then(onFulfilled:Function = null, + onRejected:Function = null):IThenable + { + return new Promise(function (resolve:Function, reject:Function):* { + handle_(new Handler(function (result:*):* { + if (typeof(onFulfilled) === 'function') + { + try + { + return resolve(onFulfilled(result)); + } + catch (e:Error) + { + return reject(e); + } + } + else + { + return resolve(result); + } + }, function (error:*):* { + if (typeof(onRejected) === 'function') + { + try + { + return resolve(onRejected(error)); + } + catch (e:Error) + { + return reject(e); + } + } + else + { + return reject(error); + } + })) + }); + } + + //---------------------------------- + // catch + //---------------------------------- + public function catch(onRejected:Function = null):IThenable + { + //TODO implement catch + return null; + } + + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5ecbcd70/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/enums/PromiseState.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/enums/PromiseState.as b/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/enums/PromiseState.as new file mode 100644 index 0000000..e9540f3 --- /dev/null +++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/enums/PromiseState.as @@ -0,0 +1,71 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.enums +{ + + public class PromiseState + { + + //-------------------------------------------------------------------------- + // + // Class constants + // + //-------------------------------------------------------------------------- + + public static const BLOCKED:PromiseState = new PromiseState("blocked"); + public static const FULFILLED:PromiseState = new PromiseState("fulfilled"); + public static const PENDING:PromiseState = new PromiseState("pending"); + public static const REJECTED:PromiseState = new PromiseState("rejected"); + + //-------------------------------------------------------------------------- + // + // Constructor + // + //-------------------------------------------------------------------------- + + public function PromiseState(stringValue:String) { + this.stringValue = stringValue; + } + + //-------------------------------------------------------------------------- + // + // Variables + // + //-------------------------------------------------------------------------- + + private var stringValue:String; + + //-------------------------------------------------------------------------- + // + // Methods + // + //-------------------------------------------------------------------------- + + //---------------------------------- + // toString + //---------------------------------- + + public function toString():String + { + return stringValue; + } + + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5ecbcd70/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/interfaces/IThenable.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/interfaces/IThenable.as b/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/interfaces/IThenable.as new file mode 100644 index 0000000..5a4e009 --- /dev/null +++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/interfaces/IThenable.as @@ -0,0 +1,31 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 IThenable + { + + function then(onFulfilled:Function = null, + onRejected:Function = null):IThenable; + + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5ecbcd70/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/vo/Handler.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/vo/Handler.as b/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/vo/Handler.as new file mode 100644 index 0000000..0b6a40a --- /dev/null +++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/promises/vo/Handler.as @@ -0,0 +1,68 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.vo +{ + + public final class Handler + { + + //-------------------------------------------------------------------------- + // + // Constructor + // + //-------------------------------------------------------------------------- + + public function Handler(onFulfilled:Function = null, + onRejected:Function = null) + { + if (onFulfilled != null) + { + this.onFulfilled = onFulfilled; + } + + if (onRejected != null) + { + this.onRejected = onRejected; + } + } + + + + //-------------------------------------------------------------------------- + // + // Properties + // + //-------------------------------------------------------------------------- + + //---------------------------------- + // onFulfilled + //---------------------------------- + + public var onFulfilled:Function; + + //---------------------------------- + // onRejected + //---------------------------------- + + public var onRejected:Function; + + } + +} \ No newline at end of file
