From 94b41166fd66741ec015eefe6046a46dd7f5b38c Mon Sep 17 00:00:00 2001 From: Tim John Date: Fri, 24 Feb 2017 12:29:49 +0000 Subject: [PATCH 1/4] Promise.isDispatched now exposed as read-only var It's sometimes necessary, or at least useful to check whether a promise signal has already been dispatched. --- src/org/osflash/signals/Promise.as | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/org/osflash/signals/Promise.as b/src/org/osflash/signals/Promise.as index 4644c41..2a2fc64 100644 --- a/src/org/osflash/signals/Promise.as +++ b/src/org/osflash/signals/Promise.as @@ -1,20 +1,20 @@ package org.osflash.signals { import flash.errors.IllegalOperationError; - + import org.osflash.signals.ISlot; import org.osflash.signals.OnceSignal; public class Promise extends OnceSignal { - private var isDispatched:Boolean; + private var _isDispatched:Boolean; private var valueObjects:Array; /** @inheritDoc */ override public function addOnce(listener:Function):ISlot { var slot:ISlot = super.addOnce(listener); - if (isDispatched) + if (_isDispatched) { slot.execute(valueObjects); slot.remove(); @@ -29,16 +29,21 @@ package org.osflash.signals */ override public function dispatch(...valueObjects):void { - if (isDispatched) + if (_isDispatched) { throw new IllegalOperationError("You cannot dispatch() a Promise more than once"); } else { - isDispatched = true; + _isDispatched = true; this.valueObjects = valueObjects; super.dispatch.apply(this, valueObjects); } } + + public function get isDispatched():Boolean + { + return _isDispatched; + } } } From 8b396c4d43c76fc3f2657d4c3424bd6d28fd880c Mon Sep 17 00:00:00 2001 From: Timothy John Date: Thu, 9 Mar 2017 14:59:38 +0000 Subject: [PATCH 2/4] Correct implementation of Constructor to allow specification of value object classes --- src/org/osflash/signals/Promise.as | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/org/osflash/signals/Promise.as b/src/org/osflash/signals/Promise.as index 2a2fc64..be2049b 100644 --- a/src/org/osflash/signals/Promise.as +++ b/src/org/osflash/signals/Promise.as @@ -10,6 +10,25 @@ package org.osflash.signals private var _isDispatched:Boolean; private var valueObjects:Array; + /** + * Creates a Promise instance to dispatch value objects. + * @param valueClasses Any number of class references that enable type checks in dispatch(). + * For example, new Signal(String, uint) + * would allow: signal.dispatch("the Answer", 42) + * but not: signal.dispatch(true, 42.5) + * nor: signal.dispatch() + * + * NOTE: In AS3, subclasses cannot call super.apply(null, valueClasses), + * but this constructor has logic to support super(valueClasses). + */ + public function Promise(...valueClasses) + { + // Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses). + valueClasses = (valueClasses.length == 1 && valueClasses[0] is Array) ? valueClasses[0]:valueClasses; + + super(valueClasses); + } + /** @inheritDoc */ override public function addOnce(listener:Function):ISlot { From 795e808abbce5fb6ce4a1e584275024b1ccefc7f Mon Sep 17 00:00:00 2001 From: Timothy John Date: Thu, 11 Oct 2018 11:29:19 +0100 Subject: [PATCH 3/4] Added Promise.ignoreSubsequentDispatches property to avoid error catching when dispatching more than once. --- src/org/osflash/signals/Promise.as | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/org/osflash/signals/Promise.as b/src/org/osflash/signals/Promise.as index be2049b..c5d8ad1 100644 --- a/src/org/osflash/signals/Promise.as +++ b/src/org/osflash/signals/Promise.as @@ -9,7 +9,10 @@ package org.osflash.signals { private var _isDispatched:Boolean; private var valueObjects:Array; - + + /** Whether to ignore any subsequent calls to dispatch(). By default, subsequent calls will throw an error. */ + public var ignoreSubsequentDipatches:Boolean = false; + /** * Creates a Promise instance to dispatch value objects. * @param valueClasses Any number of class references that enable type checks in dispatch(). @@ -50,7 +53,10 @@ package org.osflash.signals { if (_isDispatched) { - throw new IllegalOperationError("You cannot dispatch() a Promise more than once"); + if (!ignoreSubsequentDipatches) + { + throw new IllegalOperationError("You cannot dispatch() a Promise more than once"); + } } else { From 2656d91a10eb70b80a566fbd4551cd40303bc021 Mon Sep 17 00:00:00 2001 From: tim-bbav Date: Tue, 19 Dec 2023 17:01:32 +0000 Subject: [PATCH 4/4] New interface IPriorityOnceSignal. Promise now implements IPriorityOnceSignal. --- .../osflash/signals/IPriorityOnceSignal.as | 21 +++++ src/org/osflash/signals/Promise.as | 93 ++++++++++++------- 2 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 src/org/osflash/signals/IPriorityOnceSignal.as diff --git a/src/org/osflash/signals/IPriorityOnceSignal.as b/src/org/osflash/signals/IPriorityOnceSignal.as new file mode 100644 index 0000000..412d514 --- /dev/null +++ b/src/org/osflash/signals/IPriorityOnceSignal.as @@ -0,0 +1,21 @@ +package org.osflash.signals +{ + public interface IPriorityOnceSignal extends IOnceSignal + { + /** + * Subscribes a one-time listener for this signal. + * The signal will remove the listener automatically the first time it is called, + * after the dispatch to all listeners is complete. + * @param listener A function with an argument + * that matches the type of event dispatched by the signal. + * If eventClass is not specified, the listener and dispatch() can be called without an argument. + * @param priority The priority level of the event listener. + * The priority is designated by a signed 32-bit integer. + * The higher the number, the higher the priority. + * All listeners with priority n are processed before listeners of priority n-1. + * @return a ISlot, which contains the Function passed as the parameter + * @see ISlot + */ + function addOnceWithPriority(listener:Function, priority:int = 0):ISlot + } +} \ No newline at end of file diff --git a/src/org/osflash/signals/Promise.as b/src/org/osflash/signals/Promise.as index c5d8ad1..afaec9b 100644 --- a/src/org/osflash/signals/Promise.as +++ b/src/org/osflash/signals/Promise.as @@ -1,41 +1,47 @@ package org.osflash.signals { import flash.errors.IllegalOperationError; - + import org.osflash.signals.ISlot; import org.osflash.signals.OnceSignal; - public class Promise extends OnceSignal + public class Promise extends OnceSignal implements IPriorityOnceSignal { private var _isDispatched:Boolean; private var valueObjects:Array; - - /** Whether to ignore any subsequent calls to dispatch(). By default, subsequent calls will throw an error. */ - public var ignoreSubsequentDipatches:Boolean = false; - - /** - * Creates a Promise instance to dispatch value objects. - * @param valueClasses Any number of class references that enable type checks in dispatch(). - * For example, new Signal(String, uint) - * would allow: signal.dispatch("the Answer", 42) - * but not: signal.dispatch(true, 42.5) - * nor: signal.dispatch() - * - * NOTE: In AS3, subclasses cannot call super.apply(null, valueClasses), - * but this constructor has logic to support super(valueClasses). - */ - public function Promise(...valueClasses) - { - // Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses). - valueClasses = (valueClasses.length == 1 && valueClasses[0] is Array) ? valueClasses[0]:valueClasses; - - super(valueClasses); - } - + + /** Whether to ignore any subsequent calls to dispatch(). By default, subsequent calls will throw an error. */ + public var ignoreSubsequentDipatches:Boolean = false; + + /** + * Creates a Promise instance to dispatch value objects. + * @param valueClasses Any number of class references that enable type checks in dispatch(). + * For example, new Signal(String, uint) + * would allow: signal.dispatch("the Answer", 42) + * but not: signal.dispatch(true, 42.5) + * nor: signal.dispatch() + * + * NOTE: In AS3, subclasses cannot call super.apply(null, valueClasses), + * but this constructor has logic to support super(valueClasses). + */ + public function Promise(...valueClasses) + { + // Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses). + valueClasses = (valueClasses.length == 1 && valueClasses[0] is Array) ? valueClasses[0] : valueClasses; + + super(valueClasses); + } + /** @inheritDoc */ override public function addOnce(listener:Function):ISlot { - var slot:ISlot = super.addOnce(listener); + return addOnceWithPriority(listener); + } + + public function addOnceWithPriority(listener:Function, priority:int = 0):ISlot + { + var slot:ISlot = registerListenerWithPriority(listener, true, priority); + if (_isDispatched) { slot.execute(valueObjects); @@ -43,6 +49,23 @@ package org.osflash.signals } return slot; + } + + override protected function registerListener(listener:Function, once:Boolean = false):ISlot + { + return registerListenerWithPriority(listener, once); + } + + protected function registerListenerWithPriority(listener:Function, once:Boolean = false, priority:int = 0):ISlot + { + if (registrationPossible(listener, once)) + { + const slot:ISlot = new Slot(listener, this, once, priority); + slots = slots.insertWithPriority(slot); + return slot; + } + + return slots.find(listener); } /** @@ -53,10 +76,10 @@ package org.osflash.signals { if (_isDispatched) { - if (!ignoreSubsequentDipatches) - { - throw new IllegalOperationError("You cannot dispatch() a Promise more than once"); - } + if (!ignoreSubsequentDipatches) + { + throw new IllegalOperationError("You cannot dispatch() a Promise more than once"); + } } else { @@ -65,10 +88,10 @@ package org.osflash.signals super.dispatch.apply(this, valueObjects); } } - - public function get isDispatched():Boolean - { - return _isDispatched; - } + + public function get isDispatched():Boolean + { + return _isDispatched; + } } }