2019-11-23 14:24:25 +03:00
|
|
|
module eventbus
|
|
|
|
|
2020-09-25 13:02:32 +03:00
|
|
|
pub type EventHandlerFn = fn (receiver voidptr, args voidptr, sender voidptr)
|
2020-01-22 19:41:08 +03:00
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
pub struct Publisher[T] {
|
2020-01-22 19:41:08 +03:00
|
|
|
mut:
|
2023-07-07 22:33:57 +03:00
|
|
|
registry &Registry[T] = unsafe { nil }
|
2019-11-24 14:27:50 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
pub struct Subscriber[T] {
|
2020-01-22 19:41:08 +03:00
|
|
|
mut:
|
2023-07-07 22:33:57 +03:00
|
|
|
registry &Registry[T] = unsafe { nil }
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
2019-11-24 14:27:50 +03:00
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
struct Registry[T] {
|
2020-01-22 19:41:08 +03:00
|
|
|
mut:
|
2023-07-07 22:33:57 +03:00
|
|
|
events []EventHandler[T]
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
struct EventHandler[T] {
|
|
|
|
name T
|
2023-08-03 21:06:32 +03:00
|
|
|
handler EventHandlerFn = unsafe { nil }
|
|
|
|
receiver voidptr = unsafe { nil }
|
2021-03-30 15:32:24 +03:00
|
|
|
once bool
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
pub struct EventBus[T] {
|
2020-01-22 19:41:08 +03:00
|
|
|
pub mut:
|
2023-07-07 22:33:57 +03:00
|
|
|
registry &Registry[T] = unsafe { nil }
|
|
|
|
publisher &Publisher[T] = unsafe { nil }
|
|
|
|
subscriber &Subscriber[T] = unsafe { nil }
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// EventBus.new[T] create a new eventbus with event type T.
|
|
|
|
pub fn EventBus.new[T]() &EventBus[T] {
|
|
|
|
registry := &Registry[T]{
|
2019-11-23 14:24:25 +03:00
|
|
|
events: []
|
|
|
|
}
|
2023-07-07 22:33:57 +03:00
|
|
|
return &EventBus[T]{registry, &Publisher[T]{registry}, &Subscriber[T]{registry}}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// new[T] create a new eventbus with event type T.
|
|
|
|
pub fn new[T]() &EventBus[T] {
|
|
|
|
registry := &Registry[T]{
|
|
|
|
events: []
|
|
|
|
}
|
|
|
|
return &EventBus[T]{registry, &Publisher[T]{registry}, &Subscriber[T]{registry}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// publish publish an event with provided Params & name.
|
|
|
|
pub fn (eb &EventBus[T]) publish(name T, sender voidptr, args voidptr) {
|
2019-11-24 14:27:50 +03:00
|
|
|
mut publisher := eb.publisher
|
2020-01-22 19:41:08 +03:00
|
|
|
publisher.publish(name, sender, args)
|
2019-11-24 14:27:50 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// clear_all clear all subscribers.
|
|
|
|
pub fn (eb &EventBus[T]) clear_all() {
|
2019-11-24 14:27:50 +03:00
|
|
|
mut publisher := eb.publisher
|
|
|
|
publisher.clear_all()
|
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// has_subscriber check if a subscriber to an event exists.
|
|
|
|
pub fn (eb &EventBus[T]) has_subscriber(name T) bool {
|
2019-11-24 14:27:50 +03:00
|
|
|
return eb.registry.check_subscriber(name)
|
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// publish publish an event with provided Params & name.
|
|
|
|
fn (mut pb Publisher[T]) publish(name T, sender voidptr, args voidptr) {
|
2021-03-30 15:32:24 +03:00
|
|
|
for event in pb.registry.events {
|
2020-01-22 19:41:08 +03:00
|
|
|
if event.name == name {
|
2020-07-28 18:48:25 +03:00
|
|
|
event.handler(event.receiver, args, sender)
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-30 15:32:24 +03:00
|
|
|
pb.registry.events = pb.registry.events.filter(!(it.name == name && it.once))
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// clear_all clear all subscribers.
|
|
|
|
fn (mut p Publisher[T]) clear_all() {
|
2021-03-30 15:32:24 +03:00
|
|
|
p.registry.events.clear()
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// subscribe subscribe to an event `name`.
|
|
|
|
pub fn (mut s Subscriber[T]) subscribe(name T, handler EventHandlerFn) {
|
|
|
|
s.registry.events << EventHandler[T]{
|
2020-01-22 19:41:08 +03:00
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// subscribe_method subscribe to an event `name` and also set the `receiver` as a parameter.
|
|
|
|
pub fn (mut s Subscriber[T]) subscribe_method(name T, handler EventHandlerFn, receiver voidptr) {
|
|
|
|
s.registry.events << EventHandler[T]{
|
2020-01-22 19:41:08 +03:00
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
receiver: receiver
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// unsubscribe_method unsubscribe a receiver for only one method.
|
|
|
|
pub fn (mut s Subscriber[T]) unsubscribe_method(name T, receiver voidptr) {
|
2021-03-30 15:32:24 +03:00
|
|
|
s.registry.events = s.registry.events.filter(!(it.name == name && it.receiver == receiver))
|
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// unsubscribe_receiver unsubscribes a receiver from all events.
|
|
|
|
pub fn (mut s Subscriber[T]) unsubscribe_receiver(receiver voidptr) {
|
2021-03-30 15:32:24 +03:00
|
|
|
s.registry.events = s.registry.events.filter(it.receiver != receiver)
|
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// subscribe_once subscribe only once to an event `name`.
|
|
|
|
pub fn (mut s Subscriber[T]) subscribe_once(name T, handler EventHandlerFn) {
|
|
|
|
s.registry.events << EventHandler[T]{
|
2020-01-22 19:41:08 +03:00
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
once: true
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// is_subscribed check if we are subscribed to an event `name`.
|
|
|
|
pub fn (s &Subscriber[T]) is_subscribed(name T) bool {
|
2019-11-23 14:24:25 +03:00
|
|
|
return s.registry.check_subscriber(name)
|
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// is_subscribed_method checks whether a receiver was already subscribed for any events.
|
|
|
|
pub fn (s &Subscriber[T]) is_subscribed_method(name T, receiver voidptr) bool {
|
2021-03-30 15:32:24 +03:00
|
|
|
return s.registry.events.any(it.name == name && it.receiver == receiver)
|
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
// unsubscribe unsubscribe from an event `name`.
|
|
|
|
pub fn (mut s Subscriber[T]) unsubscribe(name T, handler EventHandlerFn) {
|
2020-01-22 19:41:08 +03:00
|
|
|
// v := voidptr(handler)
|
2021-03-30 15:32:24 +03:00
|
|
|
s.registry.events = s.registry.events.filter(!(it.name == name && it.handler == handler))
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registry Methods
|
2023-07-07 22:33:57 +03:00
|
|
|
fn (r &Registry[T]) check_subscriber(name T) bool {
|
2021-03-30 15:32:24 +03:00
|
|
|
return r.events.any(it.name == name)
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|