ResettableEventBus class
Wraps an EventBus to hold on to any HandlerRegistrations, so that they can easily all be cleared at once.
class ResettableEventBus extends EventBus {
final EventBus _wrapped;
final Set<HandlerRegistration> _registrations = new Set<HandlerRegistration>();
ResettableEventBus(this._wrapped);
HandlerRegistration addHandler(EventType type, EventHandler handler) {
HandlerRegistration rtn = _wrapped.addHandler(type, handler);
return _doRegisterHandler(rtn);
}
HandlerRegistration addHandlerToSource(EventType type, Object source, EventHandler handler) {
HandlerRegistration rtn = _wrapped.addHandlerToSource(type, source, handler);
return _doRegisterHandler(rtn);
}
void fireEvent(IEvent event) {
_wrapped.fireEvent(event);
}
void fireEventFromSource(IEvent event, Object source) {
_wrapped.fireEventFromSource(event, source);
}
/**
* Remove all handlers that have been added through this wrapper.
*/
void removeHandlers() {
while (_registrations.length > 0) {
HandlerRegistration r = _registrations.first;
/*
* must remove before we call removeHandler. Might have come from nested
* ResettableEventBus
*/
_registrations.remove(r);
r.removeHandler();
}
}
/**
* Visible for testing.
*/
int getRegistrationSize() {
return _registrations.length;
}
HandlerRegistration _doRegisterHandler(HandlerRegistration registration) {
_registrations.add(registration);
return new _ResettableHandlerRegistration(this, registration);
}
void _doUnregisterHandler(HandlerRegistration registration) {
if (_registrations.contains(registration)) {
registration.removeHandler();
_registrations.remove(registration);
}
}
}
Extends
EventBus > ResettableEventBus
Constructors
Methods
HandlerRegistration addHandler(EventType type, EventHandler handler) #
Adds an unfiltered handler to receive events of this type from all sources.
It is rare to call this method directly. More typically an Event
subclass will provide a static <code>register</code> method, or a widget
will accept handlers directly.
@param <H> The type of handler @param type the event type associated with this handler @param handler the handler @return the handler registration, can be stored in order to remove the
handler later
HandlerRegistration addHandler(EventType type, EventHandler handler) {
HandlerRegistration rtn = _wrapped.addHandler(type, handler);
return _doRegisterHandler(rtn);
}
HandlerRegistration addHandlerToSource(EventType type, Object source, EventHandler handler) #
Adds a handler to receive events of this type from the given source.
It is rare to call this method directly. More typically a Event
subclass will provide a static <code>register</code> method, or a widget
will accept handlers directly.
@param <H> The type of handler @param type the event type associated with this handler @param source the source associated with this handler @param handler the handler @return the handler registration, can be stored in order to remove the
handler later
HandlerRegistration addHandlerToSource(EventType type, Object source, EventHandler handler) {
HandlerRegistration rtn = _wrapped.addHandlerToSource(type, source, handler);
return _doRegisterHandler(rtn);
}
void fireEvent(IEvent event) #
Fires the event from no source. Only unfiltered handlers will receive it.
Any exceptions thrown by handlers will be bundled into a UmbrellaException and then re-thrown after all handlers have completed. An exception thrown by a handler will not prevent other handlers from executing.
@throws UmbrellaException wrapping exceptions thrown by handlers
@param event the event to fire
void fireEvent(IEvent event) {
_wrapped.fireEvent(event);
}
void fireEventFromSource(IEvent event, Object source) #
Fires the given event to the handlers listening to the event's type.
Any exceptions thrown by handlers will be bundled into a UmbrellaException and then re-thrown after all handlers have completed. An exception thrown by a handler will not prevent other handlers from executing.
@throws UmbrellaException wrapping exceptions thrown by handlers
@param event the event to fire
void fireEventFromSource(IEvent event, Object source) {
_wrapped.fireEventFromSource(event, source);
}
int getRegistrationSize() #
Visible for testing.
int getRegistrationSize() {
return _registrations.length;
}
void removeHandlers() #
Remove all handlers that have been added through this wrapper.
void removeHandlers() {
while (_registrations.length > 0) {
HandlerRegistration r = _registrations.first;
/*
* must remove before we call removeHandler. Might have come from nested
* ResettableEventBus
*/
_registrations.remove(r);
r.removeHandler();
}
}