Skip to content

Instantly share code, notes, and snippets.

@alecmce
Created July 17, 2011 16:11
Show Gist options
  • Save alecmce/1087733 to your computer and use it in GitHub Desktop.
Save alecmce/1087733 to your computer and use it in GitHub Desktop.
problem for signals?
/**
* 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();
}
}
@alecmce
Copy link
Author

alecmce commented Jul 17, 2011

package org.osflash.signals
{
    public interface IOnceBindableSignal
    {

        function addOnce(listener:Function):ISignalBinding;

        function remove(listener:Function):ISignalBinding;

    }
}

package org.osflash.signals
{
    public interface IBindableSignal extends IOnceBindableSignal
    {

        function add(listener:Function):ISignalBinding;

    }
}

@robertpenner
Copy link

Different approach: remove complete signal and have start() return a new signal each time it's called.

@neilmanuell
Copy link

Oh, that's nice.
I've usually done that in reverse, and passed the listener in with start().
But now that just seems wierd.

@alecmce
Copy link
Author

alecmce commented Jul 17, 2011

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