2019-11-23 12:24:25 +01:00
|
|
|
module eventbus
|
|
|
|
|
2020-09-25 12:02:32 +02:00
|
|
|
pub type EventHandlerFn = fn (receiver voidptr, args voidptr, sender voidptr)
|
2020-01-22 17:41:08 +01:00
|
|
|
|
2019-11-24 12:27:50 +01:00
|
|
|
pub struct Publisher {
|
2020-01-22 17:41:08 +01:00
|
|
|
mut:
|
2019-11-24 12:27:50 +01:00
|
|
|
registry &Registry
|
|
|
|
}
|
|
|
|
|
2019-11-23 12:24:25 +01:00
|
|
|
pub struct Subscriber {
|
2020-01-22 17:41:08 +01:00
|
|
|
mut:
|
2019-11-23 12:24:25 +01:00
|
|
|
registry &Registry
|
|
|
|
}
|
2019-11-24 12:27:50 +01:00
|
|
|
|
2020-01-22 17:41:08 +01:00
|
|
|
struct Registry {
|
|
|
|
mut:
|
|
|
|
events []EventHandler
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct EventHandler {
|
2021-03-30 14:32:24 +02:00
|
|
|
name string
|
|
|
|
handler EventHandlerFn
|
2020-07-28 17:48:25 +02:00
|
|
|
receiver voidptr = voidptr(0)
|
2021-03-30 14:32:24 +02:00
|
|
|
once bool
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 17:41:08 +01:00
|
|
|
pub struct EventBus {
|
|
|
|
pub mut:
|
|
|
|
registry &Registry
|
|
|
|
publisher &Publisher
|
2019-11-24 12:27:50 +01:00
|
|
|
subscriber &Subscriber
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 17:41:08 +01:00
|
|
|
pub fn new() &EventBus {
|
2019-11-23 12:24:25 +01:00
|
|
|
registry := &Registry{
|
|
|
|
events: []
|
|
|
|
}
|
2021-03-30 14:32:24 +02:00
|
|
|
return &EventBus{registry, &Publisher{registry}, &Subscriber{registry}}
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-24 12:27:50 +01:00
|
|
|
// EventBus Methods
|
2020-01-22 17:41:08 +01:00
|
|
|
pub fn (eb &EventBus) publish(name string, sender voidptr, args voidptr) {
|
2019-11-24 12:27:50 +01:00
|
|
|
mut publisher := eb.publisher
|
2020-01-22 17:41:08 +01:00
|
|
|
publisher.publish(name, sender, args)
|
2019-11-24 12:27:50 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 17:41:08 +01:00
|
|
|
pub fn (eb &EventBus) clear_all() {
|
2019-11-24 12:27:50 +01:00
|
|
|
mut publisher := eb.publisher
|
|
|
|
publisher.clear_all()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (eb &EventBus) has_subscriber(name string) bool {
|
|
|
|
return eb.registry.check_subscriber(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publisher Methods
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut pb Publisher) publish(name string, sender voidptr, args voidptr) {
|
2021-03-30 14:32:24 +02:00
|
|
|
for event in pb.registry.events {
|
2020-01-22 17:41:08 +01:00
|
|
|
if event.name == name {
|
2020-07-28 17:48:25 +02:00
|
|
|
event.handler(event.receiver, args, sender)
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-30 14:32:24 +02:00
|
|
|
pb.registry.events = pb.registry.events.filter(!(it.name == name && it.once))
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut p Publisher) clear_all() {
|
2021-03-30 14:32:24 +02:00
|
|
|
p.registry.events.clear()
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subscriber Methods
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut s Subscriber) subscribe(name string, handler EventHandlerFn) {
|
2021-03-30 14:32:24 +02:00
|
|
|
s.registry.events << EventHandler{
|
2020-01-22 17:41:08 +01:00
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 12:24:25 +01:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut s Subscriber) subscribe_method(name string, handler EventHandlerFn, receiver voidptr) {
|
2021-03-30 14:32:24 +02:00
|
|
|
s.registry.events << EventHandler{
|
2020-01-22 17:41:08 +01:00
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
receiver: receiver
|
|
|
|
}
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 12:32:29 +02:00
|
|
|
// unsubscribe_method unsubscribe a receiver for only one method
|
2021-03-30 14:32:24 +02:00
|
|
|
pub fn (mut s Subscriber) unsubscribe_method(name string, receiver voidptr) {
|
|
|
|
s.registry.events = s.registry.events.filter(!(it.name == name && it.receiver == receiver))
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsubscribe_receiver unsubscribes a receiver from all events
|
|
|
|
pub fn (mut s Subscriber) unsubscribe_receiver(receiver voidptr) {
|
|
|
|
s.registry.events = s.registry.events.filter(it.receiver != receiver)
|
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut s Subscriber) subscribe_once(name string, handler EventHandlerFn) {
|
2021-03-30 14:32:24 +02:00
|
|
|
s.registry.events << EventHandler{
|
2020-01-22 17:41:08 +01:00
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
once: true
|
|
|
|
}
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s &Subscriber) is_subscribed(name string) bool {
|
|
|
|
return s.registry.check_subscriber(name)
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:32:24 +02:00
|
|
|
// is_subscribed_method checks whether a receiver was already subscribed for any events
|
|
|
|
pub fn (s &Subscriber) is_subscribed_method(name string, receiver voidptr) bool {
|
|
|
|
return s.registry.events.any(it.name == name && it.receiver == receiver)
|
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut s Subscriber) unsubscribe(name string, handler EventHandlerFn) {
|
2020-01-22 17:41:08 +01:00
|
|
|
// v := voidptr(handler)
|
2021-03-30 14:32:24 +02:00
|
|
|
s.registry.events = s.registry.events.filter(!(it.name == name && it.handler == handler))
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registry Methods
|
|
|
|
fn (r &Registry) check_subscriber(name string) bool {
|
2021-03-30 14:32:24 +02:00
|
|
|
return r.events.any(it.name == name)
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|