Created
July 17, 2011 16:11
-
-
Save alecmce/1087733 to your computer and use it in GitHub Desktop.
problem for signals?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This example sets up an asynchronous process that will complete with a | |
* complete signal | |
*/ | |
public class Example | |
{ | |
private var _complete:Signal; | |
public function Example() | |
{ | |
_complete = new Signal(Example); | |
} | |
public function start():void | |
{ | |
t = new Timer(50, 1); | |
t.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete); | |
t.start(); | |
} | |
private function onTimerComplete(event:TimerEvent):void | |
{ | |
onComplete(); | |
} | |
/** | |
* when complete, I really, really don't want any previous listeners that were added | |
* instead of addOnced to be around so I'm going to add a layer of precaution by removing | |
* all my listeners after dispatch | |
*/ | |
protected function onComplete():void | |
{ | |
_complete.dispatch(this); | |
_complete.removeAll(); // this line clearly breaks the use-case below, but I need it | |
} | |
public function get complete():ISignal | |
{ | |
return _complete; | |
} | |
} | |
/** | |
* This class will construct the example, listen for the onComplete and start again. The problem is that | |
* the second time the onComplete will never be hit. | |
*/ | |
public class Use | |
{ | |
private var _example:Example; | |
public function Use() | |
{ | |
_example = new Example(); | |
_example.complete.addOnce(onComplete); | |
_example.start(); | |
} | |
private function onComplete(example:Example):void | |
{ | |
trace("tada!"); | |
_example.complete.addOnce(onComplete); | |
_example.start(); | |
} | |
} |
Author
alecmce
commented
Jul 17, 2011
Different approach: remove complete signal and have start() return a new signal each time it's called.
Oh, that's nice.
I've usually done that in reverse, and passed the listener in with start().
But now that just seems wierd.
Thanks. I'm trying to avoid object constructions, which makes me reluctant to go down this path.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment