v/vlib/eventbus/eventbus.v

136 lines
2.5 KiB
V
Raw Normal View History

2019-11-23 12:24:25 +01:00
module eventbus
2020-01-22 17:41:08 +01:00
pub type EventHandlerFn fn(voidptr, voidptr, voidptr)
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
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
pub:
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-01-22 17:41:08 +01: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 {
pb.registry.events.delete(i)
2019-11-23 12:24:25 +01:00
}
2020-01-22 17:41:08 +01:00
if event.receiver != voidptr(0) {
event.handler(event.receiver, args, sender)
} else {
event.handler(sender, args, voidptr(0))
}
2019-11-23 12:24:25 +01:00
}
}
}
2020-01-22 17:41:08 +01: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 12:24:25 +01:00
}
}
// Subscriber Methods
2020-01-22 17:41:08 +01: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 12:24:25 +01:00
2020-01-22 17:41:08 +01: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 12:24:25 +01:00
}
2020-01-22 17:41:08 +01: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 12:24:25 +01:00
}
pub fn (s &Subscriber) is_subscribed(name string) bool {
return s.registry.check_subscriber(name)
}
2020-01-22 17:41:08 +01: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 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
}