Labs

It works in my machine!

Archive for October, 2009

Signals

without comments

Robert Penner shows to the world his new Event System called Signals
it’s a cool way for managing Events in AS3 inspired by C# events and signals/slots in Qt.

Concept

  • A Signal is essentially a mini-dispatcher specific to one event, with its own array of listeners.
  • A Signal gives an event a concrete membership in a class.
  • Listeners subscribe to real objects, not to string-based channels.
  • Event string constants are no longer needed.

Features

  • Remove all event listeners:
  • Retrieve the number of listeners
  • Listeners can be added for a one-time call and removed automatically on dispatch:
  • Any object type can be dispatched to listeners. But using IEvent will enable full functionality.
  • A Signal can be initialized with an event class that will validate event objects on dispatch (optional):
  • If the Signal’s event class is specified, each listener is checked on add() to ensure it declares at least one argument.
  • Signals can be placed in interfaces to indicate the events dispatched by a class.
  • Events can bubble recursively through .parent independent of the display list (experimental).
  • Code was developed test-first.

Sintax

comparison:

1
2
3
4
5
// with EventDispatcher
button.addEventListener(MouseEvent.CLICK, onClick);
 
// Signal equivalent; past tense is recommended
button.clicked.add(onClicked);

Create a Signal for a class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
////// in Item class:
// Recommended: make the signal read-only.
protected var _ready:Signal;
public function get ready():Signal { return _ready; }
 
public function Item()
{
	_ready = new Signal(this, ReadyEvent);
}
 
protected function sendReady():void
{
	_ready.dispatch(new ReadyEvent());
}
 
////// in another class:
var item:Item = new Item();
protected function onItemReady(e:ReadyEvent):void { ... }
 
item.ready.add(onItemReady);

more info on Signals

AS3 Signals Getting Stronger
The Community Responds to AS3 Signals
My New AS3 Event System: Signals

more info on AS3 Events

My Critique of AS3 Events – Part 1
AS3 Events – 7 things I’ve learned from community
My Critique of AS3 Events – Part 2

i like this kind of people, never comfortable, always trying to make improvements, changing the way we see things and Robert Penner is not and exception.

Written by Raúl

October 13th, 2009 at 1:24 pm

Posted in AS3

Tagged with ,