2019-11-23 14:24:25 +03:00
|
|
|
module eventbus
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
pub type EventHandlerFn fn(voidptr, voidptr, voidptr)
|
|
|
|
|
2019-11-24 14:27:50 +03:00
|
|
|
pub struct Publisher {
|
2020-01-22 19:41:08 +03:00
|
|
|
mut:
|
2019-11-24 14:27:50 +03:00
|
|
|
registry &Registry
|
|
|
|
}
|
|
|
|
|
2019-11-23 14:24:25 +03:00
|
|
|
pub struct Subscriber {
|
2020-01-22 19:41:08 +03:00
|
|
|
mut:
|
2019-11-23 14:24:25 +03:00
|
|
|
registry &Registry
|
|
|
|
}
|
2019-11-24 14:27:50 +03:00
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
struct Registry {
|
|
|
|
mut:
|
|
|
|
events []EventHandler
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct EventHandler {
|
2020-01-22 19:41:08 +03:00
|
|
|
name string
|
|
|
|
handler EventHandlerFn
|
|
|
|
receiver voidptr
|
|
|
|
once bool
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
pub struct EventBus {
|
|
|
|
pub mut:
|
|
|
|
registry &Registry
|
|
|
|
publisher &Publisher
|
|
|
|
pub:
|
2019-11-24 14:27:50 +03:00
|
|
|
subscriber &Subscriber
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
pub fn new() &EventBus {
|
2019-11-23 14:24:25 +03:00
|
|
|
registry := &Registry{
|
|
|
|
events: []
|
|
|
|
}
|
|
|
|
return &EventBus{
|
2020-01-22 19:41:08 +03:00
|
|
|
registry,&Publisher{
|
|
|
|
registry},&Subscriber{
|
|
|
|
registry}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:27:50 +03:00
|
|
|
// EventBus Methods
|
2020-01-22 19:41:08 +03:00
|
|
|
pub fn (eb &EventBus) publish(name string, 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
|
|
|
}
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
pub fn (eb &EventBus) clear_all() {
|
2019-11-24 14:27:50 +03: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-01-22 19:41:08 +03:00
|
|
|
fn (pb mut Publisher) publish(name string, sender voidptr, args voidptr) {
|
|
|
|
for i, event in pb.registry.events {
|
|
|
|
if event.name == name {
|
|
|
|
if event.once {
|
2019-11-24 14:27:50 +03:00
|
|
|
pb.registry.events.delete(i)
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
2020-05-01 13:12:21 +03:00
|
|
|
if event.receiver != 0 {
|
|
|
|
event.handler(event.receiver, args, sender)
|
2020-01-22 19:41:08 +03:00
|
|
|
} else {
|
|
|
|
event.handler(sender, args, voidptr(0))
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
fn (p mut Publisher) clear_all() {
|
|
|
|
if p.registry.events.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := p.registry.events.len - 1; i >= 0; i-- {
|
|
|
|
p.registry.events.delete(i)
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscriber Methods
|
2020-01-22 19:41:08 +03:00
|
|
|
pub fn (s mut Subscriber) subscribe(name string, handler EventHandlerFn) {
|
|
|
|
s.registry.events << EventHandler {
|
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
receiver: voidptr(0)
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
pub fn (s mut Subscriber) subscribe_method(name string, handler EventHandlerFn, receiver voidptr) {
|
|
|
|
s.registry.events << EventHandler {
|
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
receiver: receiver
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
pub fn (s mut Subscriber) subscribe_once(name string, handler EventHandlerFn) {
|
|
|
|
s.registry.events << EventHandler {
|
|
|
|
name: name
|
|
|
|
handler: handler
|
|
|
|
receiver: voidptr(0)
|
|
|
|
once: true
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s &Subscriber) is_subscribed(name string) bool {
|
|
|
|
return s.registry.check_subscriber(name)
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
pub fn (s mut Subscriber) unsubscribe(name string, handler EventHandlerFn) {
|
|
|
|
// v := voidptr(handler)
|
|
|
|
for i, event in s.registry.events {
|
|
|
|
if event.name == name {
|
|
|
|
if event.handler == handler {
|
|
|
|
s.registry.events.delete(i)
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registry Methods
|
|
|
|
fn (r &Registry) check_subscriber(name string) bool {
|
2020-01-22 19:41:08 +03:00
|
|
|
for event in r.events {
|
|
|
|
if event.name == name {
|
|
|
|
return true
|
|
|
|
}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|