public class Informer
extends java.lang.Object
Simple sample:
public class Foo {
@Subscriber
public void onEvent(SomeEvent event) {
//...
}
}
Foo foo = new Foo();
Informer informer = new Informer();
informer.registerStrong(foo);
// ...
informer.post(new SomeEvent());
It is possible subscriber exceptions and catch unprocessed events, support for Event hierarchies
(e.g. subscrive to event superclass or any type with Object). See post(Object), registerStrong(Object), registerWeak(Object) for further details.
Created by sergeych on 13/02/16.
| Modifier and Type | Class and Description |
|---|---|
class |
Informer.ExceptionContext
Context to carry subscriber exception information
|
static interface |
Informer.ExceptionListener
Listener for exceptions thrown by subscribers
|
| Constructor and Description |
|---|
Informer() |
Informer(Informer.ExceptionListener listener)
Create informer that listens to the subscriber exceptions which are otherwise ignored with no
report.
|
| Modifier and Type | Method and Description |
|---|---|
void |
post(java.lang.Object event)
Synchronously post an event.
|
void |
postAfter(java.lang.Object event,
long millis)
Post an event to subscribers after the specified timeout, see
post(Object) for
details. |
void |
register(java.lang.Object subscriber,
boolean registerWeak)
Register a subscriber object as a strong or a weak reference.
|
void |
registerStrong(java.lang.Object subscriber)
Register subscriber as a strong reference, and it will be retained at least as long as it is
registered and the Informer instance is not collected.
|
void |
registerWeak(java.lang.Object subscriber)
Register a subscriber object as a weak reference.
|
boolean |
unregister(java.lang.Object subscriber)
Unregister subscriber.
|
public Informer()
public Informer(Informer.ExceptionListener listener)
listener - public void post(java.lang.Object event)
event - public void postAfter(java.lang.Object event,
long millis)
post(Object) for
details. Subscribers will be informed adter specified interval from some other thread. The
call returns immediately.event - event to postmillis - timeout to wait before postingpublic void registerWeak(java.lang.Object subscriber)
register(Object, boolean) for
more.subscriber - public void registerStrong(java.lang.Object subscriber)
register(Object, boolean)
for more.subscriber - public boolean unregister(java.lang.Object subscriber)
subscriber - to unregister.public void register(java.lang.Object subscriber,
boolean registerWeak)
The old subscription of this object if any will be removed. e.g. you can re-register it as
weak, then the strong registration will be thrown. The same, no matter how many time the
subscriber was registered, its @Subscriber methods will be called only once per post(Object) invocation.
To receive events, subscriber class declares one or more public methods with @Subscriber annotation and the single argument. Suchm methods should return void or boolean, in which case returning true instructs notifier to not to pass this event to any other subscribers. Subscriber receive only objects that can be casted to its declared argument type, this way you can have subscribers to process different types of events.
It is allowed to have several subsciber methods ofr the same type of argument in the same class. No restrictions :)
In the cases there is no suitable subscribers for some evvent, it will be reposted inside the
LostEvent instance that could be subscribed as usual.
Any exceptions inside the subscriber are ignored
See Subscriber for details on event processing.
subscriber - instance, should be of the public class.registerWeak -