This repository contains a simple implementation of a thread-safe, multithreaded event system in the D programming language.
The event system consists of an SEBSingleton
class that manages event publication and subscription. The event bus can have multiple subscribers that listen for events. When an event is published, all subscribers will receive the event and can react accordingly.
The event bus uses D's std.parallelism
library to handle events in parallel across multiple threads, ensuring efficient utilization of system resources.
Please note that the library does not provide a subscription to an event not from the main thread. Perhaps we will do this in the future.
Subscribers can register a delegate function that will be called whenever an event of a particular type is posted:
// register a handler function for the TestEvent
EventBus.subscribe!TestEvent((event) {
writeln("Test event has occurred");
});
Event can be published to the bus by calling the publish
function:
EventBus.publish(new TestEvent);
To start dispatching events, call the startDispatching
method:
EventBus.startDispatching();
To stop dispatching events, call the stopDispatching
method:
EventBus.stopDispatching();
EventBus.subscribe!KeyPressEvent((event) {
if(event.keyCode == 42) {
event.cancel;
}
});
After the event is cancelled, it is lazily removed from the queue.
Here is a complete example of using the event system:
import seb;
import std.stdio;
class TestEvent : Event {}
class KeyPressEvent : Event
{
int keyCode;
this(int code)
{
keyCode = code;
}
}
void main()
{
// Register event handlers
EventBus.subscribe!TestEvent((event) {
writeln("Test event has occurred");
});
EventBus.subscribe!KeyPressEvent((event) {
writeln("Key with code ", event.keyCode, " has been pressed!");
});
// Start dispatching events
EventBus.startDispatching();
// Publish events
EventBus.publish(new TestEvent);
EventBus.publish(new KeyPressEvent(42));
scope (exit)
{
EventBus.stopDispatching;
}
}
This will print:
Test event has occurred
Key with code 42 has been pressed!
- Cancelable events
- Rewrite to actor model
- Subscriber priority
- Unsubscribe
- Middleware
- Subscribe to an event from another thread
- Unittests