An extension for Robotlegs v2.
The Relaxed Event Map allows to register listeners with the shared event dispatcher for events that already have been dispatched. It facilitates in dealing with racing conditions, where e.g. a model has already sent an update event before a mediator that listens for that event has been instantiated.
- Requires a Robotlegs version greater than v2.0.0, depends on a change made in commit 0a9e40b
- This is a direct port from Stray's robotlegs-utilities-relaxedeventmap for Robotlegs v1
It is necessary to tell the Relaxed Event Map to remember an event and store the last dispatched instance.
relaxedEventMap.rememberEvent(FooEvent.Foo, FooEvent);
If you register a listener for this event it will be called even if the event has already been dispatched.
eventDispatcher.dispatchEvent(new FooEvent(FooEvent.FOO));
relaxedEventMap.mapRelaxedListener(FooEvent.Foo, listener, FooEvent); //handler will be called immediately, i.e. synchronously
relaxedEventMap.unmapRelaxedListener(FooEvent.Foo, listener, FooEvent);
You can pass a key as a fourth parameter to the mapping method and unmap all listeners at once with unmapListenersFor
.
relaxedEventMap.mapRelaxedListener(SomeDataEvent.DATA_SET_UPDATED, updatedHandler, SomeDataEvent, this); //'this' used as key
relaxedEventMap.mapRelaxedListener(SomeDataEvent.DATA_SET_DELETED, deletedHandler, SomeDataEvent, this); //'this' used as key
relaxedEventMap.unmapListenersFor(this); //'this' used as key
This extension requires the following extensions:
- EventDispatcherExtension
_context = new Context().install(
EventDispatcherExtension,
RelaxedEventMapExtension);
Or, assuming that the EventDispatcherExtension has already been installed:
_context.install(RelaxedEventMapExtension);
An instance of IRelaxedEventMap is mapped into the context during extension installation. This instance can be injected into clients and used as below.
[Inject]
public var relaxedEventMap:IRelaxedEventMap;