Guest User

Untitled

a guest
Feb 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. /**
  2. * This example sets up an asynchronous process that will complete with a
  3. * complete signal
  4. */
  5. public class Example
  6. {
  7. private var _complete:Signal;
  8.  
  9. public function Example()
  10. {
  11. _complete = new Signal(Example);
  12. }
  13.  
  14. public function start():void
  15. {
  16. t = new Timer(50, 1);
  17. t.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
  18. t.start();
  19. }
  20.  
  21. private function onTimerComplete(event:TimerEvent):void
  22. {
  23. onComplete();
  24. }
  25.  
  26. /**
  27. * when complete, I really, really don't want any previous listeners that were added
  28. * instead of addOnced to be around so I'm going to add a layer of precaution by removing
  29. * all my listeners after dispatch
  30. */
  31. protected function onComplete():void
  32. {
  33. _complete.dispatch(this);
  34. _complete.removeAll(); // this line clearly breaks the use-case below, but I need it
  35. }
  36.  
  37. public function get complete():ISignal
  38. {
  39. return _complete;
  40. }
  41. }
  42.  
  43. /**
  44. * This class will construct the example, listen for the onComplete and start again. The problem is that
  45. * the second time the onComplete will never be hit.
  46. */
  47. public class Use
  48. {
  49.  
  50. private var _example:Example;
  51.  
  52. public function Use()
  53. {
  54. _example = new Example();
  55. _example.complete.addOnce(onComplete);
  56. _example.start();
  57. }
  58.  
  59. private function onComplete(example:Example):void
  60. {
  61. trace("tada!");
  62. _example.complete.addOnce(onComplete);
  63. _example.start();
  64. }
  65.  
  66. }
Add Comment
Please, Sign In to add comment