My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Distributor  
an alternate event system
Featured
Updated Jun 20, 2010 by maxkugland@gmail.com

The distributor package provides you with an event system which is a bit different from the default flash EventDispatcher. It doesn't replace EventDispatcher but is a nice alternative in many cases.

The distributor key features are:

  • a compact API which is easy to use
  • a finalize method which removes all listeners at once. No more removeListener boilerplate code
  • typed access to the relevant information, i.e. the event source and the event type. ever had to cast event.target to IEventDispatcher?
  • optional passing of arbitrary objects to the listener functions.
  • typesafe event types

Here is some code:

Firstly the class which distributes the events:

public class Source extends Distributor implements IDistributor {
	public function test() : void {
		distribute(new SourceEvent(this, SourceEvent.TEST_1));
		distribute(new SourceEvent(this, SourceEvent.TEST_2));
	}
}

By extending Distribtor the Source class inherits the ability to distribute events. Note that inheritance is not mandatory as the there is an IDistributor interface. Distribtor also works very well via composition but it's more code to type. In the test method is outlined how events are distributed.

The SourceEvent class looks like this:

public class SourceEvent extends Devent implements IDevent {
	public static const TEST_1 : Dtype = new Dtype("test1");
	public static const TEST_2 : Dtype = new Dtype("test2");

	public function SourceEvent(source : IDistributor, type : Dtype) {
		super(source, type);
	}
}

SourceEvent offers two event types, TEST_1 and TEST_2. In it's constructor Source expects

  • a source argument (this is the class which distributes the event)
  • and the event type.
These arguments are passed to the Devent class from which SourceEvent inherits. Devent is just a convenience IDevent implementation. It only stores these properties as class members.

Now the last class in the trio, the Target class:

public class Target {
	public function Target() {
		var source : Source = new Source();
		source.register(SourceEvent.TEST_1, onTest);
		source.register(SourceEvent.TEST_2, onTest2, "Some message");
		
		source.test();
	}

	private function onTest(e : SourceEvent) : void {
		source.unregister(SourceEvent.TEST_1, onTest);
		trace("received event of type " + e.type + " from: " + e.source);
	}

	private function onTest2(e : SourceEvent, msg : String) : void {
		trace(msg);
		trace(e.source is Source);
		e.source.finalize();
	}
}

The Target class creates the Source class and registeres two functions which are invoked if Source distributes an event of the apropriate type. Optionally you can pass along any object as third argument of the register method to the event listener function. This is sometimes nice, because it keeps out state by avoiding unneccessary class members. In the onTest method the trace statement demonstrates how to unregister from an event and traces the event type and source. onTest2 outputs the message which was passed during event registration. Note that anything can be passed along, not just a String as in this example. From the information contained in distributed events you can access the class which distributed the event and for instance invoke it's finalize method to unregister all event listeners.


Sign in to add a comment
Powered by Google Project Hosting