v/vlib/eventbus/eventbus.v

129 lines
2.4 KiB
V
Raw Normal View History

2019-11-23 12:24:25 +01:00
module eventbus
pub type EventHandlerFn = fn (receiver voidptr, args voidptr, sender voidptr)
2020-01-22 17:41:08 +01:00
pub struct Publisher {
2020-01-22 17:41:08 +01:00
mut:
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
}
2020-01-22 17:41:08 +01:00
struct Registry {
mut:
events []EventHandler
2019-11-23 12:24:25 +01:00
}
struct EventHandler {
2020-01-22 17:41:08 +01:00
name string
handler EventHandlerFn
receiver voidptr = voidptr(0)
2020-01-22 17:41:08 +01: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
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: []
}
return &EventBus{
2020-01-22 17:41:08 +01:00
registry,&Publisher{
registry},&Subscriber{
registry}
2019-11-23 12:24:25 +01:00
}
}
// EventBus Methods
2020-01-22 17:41:08 +01:00
pub fn (eb &EventBus) publish(name string, sender voidptr, args voidptr) {
mut publisher := eb.publisher
2020-01-22 17:41:08 +01:00
publisher.publish(name, sender, args)
}
2020-01-22 17:41:08 +01:00
pub fn (eb &EventBus) clear_all() {
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) {
2020-01-22 17:41:08 +01:00
for i, event in pb.registry.events {
if event.name == name {
if event.once {
pb.registry.events.delete(i)
2019-11-23 12:24:25 +01:00
}
event.handler(event.receiver, args, sender)
2019-11-23 12:24:25 +01:00
}
}
}
2020-05-17 13:51:18 +02:00
fn (mut p Publisher) clear_all() {
2020-01-22 17:41:08 +01:00
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 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) {
2020-01-22 17:41:08 +01:00
s.registry.events << EventHandler {
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) {
2020-01-22 17:41:08 +01:00
s.registry.events << EventHandler {
name: name
handler: handler
receiver: receiver
}
2019-11-23 12:24:25 +01:00
}
2020-05-17 13:51:18 +02:00
pub fn (mut s Subscriber) subscribe_once(name string, handler EventHandlerFn) {
2020-01-22 17:41:08 +01:00
s.registry.events << EventHandler {
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)
}
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)
for i, event in s.registry.events {
if event.name == name {
if event.handler == handler {
s.registry.events.delete(i)
2019-11-23 12:24:25 +01:00
}
}
}
}
// Registry Methods
fn (r &Registry) check_subscriber(name string) bool {
2020-01-22 17:41:08 +01:00
for event in r.events {
if event.name == name {
return true
}
2019-11-23 12:24:25 +01:00
}
return false
}