Skip to content
Nathan S edited this page Jun 16, 2020 · 2 revisions

borealis offers an Event class that allows sending and listening to various events across the views. An instance of that class corresponds to a specific event in the view (or anywhere else).

Subscribing to an event

To subscribe to an event, simply call subscribe on the event. You need to give it the callback that is to be executed when the event fires (std::function instance). Subscribing to an event gives you back a subscription identifier, that you can store and later use to cancel that particular subscription.

Each event is of a specific type. Different types change the parameters of the callback function. For instance, GenericEvent takes a single view pointer. VoidEvent takes no parameter. You must match the parameters of the callback when subscribing to an event!

Creating an event

To create an event, you first need to declare its type. You can skip this step if you're using one of the built-in event types. Creating an event type is done with typedef, like so:

typedef Event<bool success> ProcessEvent;

That declares the event type ProcessEvent that takes a single boolean value as callback parameter. Let's say it can be used to notify the end of a background process.

Then, you need to make an instance of this new type to actually create your event. Here, we are going to do multiple ones to demonstrate the difference between event type and actual events:

ProcessEvent downloadFinishedEvent;
ProcessEvent copyFinishedEvent;

Finally, when you want to fire one of the events, simply call fire() on them. You have to give the parameters for the called callbacks, which is a single boolean here:

try
{
    // do the download...
    downloadFinishedEvent.fire(true);
}
catch (const std::exception& e)
{
    downloadFinishedEvent.fire(false);
}
Clone this wiki locally