Omnibus

Pub Package Build Status Coverage Status

Omnibus is a single threaded message bus designed for MolView. Omnibus is distributed under the MIT license.

Getting started

Emitting events

To emit an event you have to create a class that inherits OmnibusEvent. Your event class has to declare type and instanceType. Any event data should be stored in data. You can declare data yourself or use the generic data object from the OmnibusData class although this is not recommended. More details can be found in the documentation. Below is an example from the unit tests.

class TextEvent extends OmnibusEvent
{
    static final String type = 'test.TextEvent';
    String get instanceType => type;

    TextEvent(String text) : super(text);
}

Listen to events

To listen to events you have to create a class that inherits OmnibusListener. Your listener class has to implement the probe and receive methods. More details can be found in the documentation. Below is an example from the unit tests.

class TextListener extends OmnibusListener
{
    bool probe(OmnibusEvent event)
    {
        return event.data.endsWith('!');
    }

    Future<OmnibusReply> receive(OmnibusEvent event) async
    {
        Completer completer = new Completer<OmnibusReply>();

        Timer timer = new Timer(
            new Duration(milliseconds: 100),
            () => completer.complete(new OmnibusReply(
                event.data.substring(0, event.data.length - 1))));

        return completer.future;
    }
}

Receive replies

Often you want to receive replies from listeners that listen to your event in order to establish some kind of 1-to-n communication system. To do this, you have to to create a class that inherits OmnibusReply like in the example below.

class TextReply extends OmnibusReply
{
    static final String type = 'test.TextReply';
    String get instanceType => type;

    TextReply(String text) : super(text);
}

Any listener can use this class to reply to any event. It's up to you to fetch the replies and process them. Since an event is emitted asynchronously, you have to await the replies like in the example below.

Set<OmnibusReply> replies = await bus.emit(new TextEvent('Hello World!'));

Reply to events

You can reply to events by returning an OmnibusReply in the OmnibusListener receive method. An OmnibusReply is only useful if the receiver knows about the reply. Therefore you should never define a reply yourself. Always use the reply class defined by the receiver.

Libraries

omnibus

Omnibus library