/src/org/osflash/signals/Signal.as

http://github.com/robertpenner/as3-signals · ActionScript · 51 lines · 16 code · 4 blank · 31 comment · 2 complexity · f342ac5e077569ef84856b6a692731c9 MD5 · raw file

  1. package org.osflash.signals
  2. {
  3. /**
  4. * Allows the valueClasses to be set in MXML, e.g.
  5. * <signals:Signal id="nameChanged">{[String, uint]}</signals:Signal>
  6. */
  7. [DefaultProperty("valueClasses")]
  8. /**
  9. * Signal dispatches events to multiple listeners.
  10. * It is inspired by C# events and delegates, and by
  11. * <a target="_top" href="http://en.wikipedia.org/wiki/Signals_and_slots">signals and slots</a>
  12. * in Qt.
  13. * A Signal adds event dispatching functionality through composition and interfaces,
  14. * rather than inheriting from a dispatcher.
  15. * <br/><br/>
  16. * Project home: <a target="_top" href="http://github.com/robertpenner/as3-signals/">http://github.com/robertpenner/as3-signals/</a>
  17. */
  18. public class Signal extends OnceSignal implements ISignal
  19. {
  20. /**
  21. * Creates a Signal instance to dispatch value objects.
  22. * @param valueClasses Any number of class references that enable type checks in dispatch().
  23. * For example, new Signal(String, uint)
  24. * would allow: signal.dispatch("the Answer", 42)
  25. * but not: signal.dispatch(true, 42.5)
  26. * nor: signal.dispatch()
  27. *
  28. * NOTE: In AS3, subclasses cannot call super.apply(null, valueClasses),
  29. * but this constructor has logic to support super(valueClasses).
  30. */
  31. public function Signal(...valueClasses)
  32. {
  33. // Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses).
  34. valueClasses = (valueClasses.length == 1 && valueClasses[0] is Array) ? valueClasses[0]:valueClasses;
  35. super(valueClasses);
  36. }
  37. /**
  38. * @inheritDoc
  39. * @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
  40. * @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
  41. */
  42. public function add(listener:Function):ISlot
  43. {
  44. return registerListener(listener);
  45. }
  46. }
  47. }