seb 0.3.1
Simple event bus — The simple thread-safety multithreaded event system
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
Simple event bus — Thread-Safe Multithreaded Event System
This repository contains a simple implementation of a thread-safe, multithreaded event system in the D programming language.
Overview
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.
Usage
Subscribing to Events
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");
});
Publishing Events
Event can be published to the bus by calling the publish
function:
EventBus.publish(new TestEvent);
Starting and Stopping Event Dispatching
To start dispatching events, call the startDispatching
method:
EventBus.startDispatching();
To stop dispatching events, call the stopDispatching
method:
EventBus.stopDispatching();
Event cancellation
EventBus.subscribe!KeyPressEvent((event) {
if(event.keyCode == 42) {
event.cancel;
}
});
After the event is cancelled, it is lazily removed from the queue.
Example
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!
TO DO
- [X] Cancelable events
- [ ] Rewrite to actor model
- [ ] Subscriber priority
- [ ] Unsubscribe
- [ ] Middleware
- [ ] Subscribe to an event from another thread
- [ ] Unittests
- 0.3.1 released a year ago
- LightHouseSoftware/seb
- ESL 1.0
- Copyright © 2023, LightHouse Software
- Authors:
- Dependencies:
- none
- Versions:
-
0.3.1 2023-Aug-17 0.3.0 2023-Aug-15 0.2.1 2023-Aug-14 0.2.0 2023-Aug-14 ~main 2023-Aug-17 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
0 downloads total
-
- Score:
- 0.8
- Short URL:
- seb.dub.pm