v/vlib/eventbus/eventbus.v

139 lines
2.6 KiB
V
Raw Normal View History

2019-11-23 12:24:25 +01:00
module eventbus
pub struct Publisher {
mut:
registry &Registry
}
2019-11-23 12:24:25 +01:00
pub struct Subscriber {
mut:
registry &Registry
}
2019-11-23 12:24:25 +01:00
struct Registry{
mut:
names []string
events []voidptr
once []string
}
struct EventHandler {
2019-12-18 06:16:33 +01:00
func fn(voidptr, Params)
2019-11-23 12:24:25 +01:00
}
pub struct EventBus{
2019-12-18 06:16:33 +01:00
pub mut:
2019-11-23 12:24:25 +01:00
registry &Registry
publisher &Publisher
2019-11-23 12:24:25 +01:00
pub:
subscriber &Subscriber
2019-11-23 12:24:25 +01:00
}
pub fn new() &EventBus{
registry := &Registry{
names: []
events: []
once: []
}
return &EventBus{
registry,
&Publisher{registry},
&Subscriber{registry}
2019-11-23 12:24:25 +01:00
}
}
// EventBus Methods
2019-12-18 06:16:33 +01:00
pub fn (eb &EventBus) publish(name string, sender voidptr, p Params) {
mut publisher := eb.publisher
2019-12-18 06:16:33 +01:00
publisher.publish(name, sender, p)
}
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
2019-12-18 06:16:33 +01:00
fn (pb mut Publisher) publish(name string, sender voidptr, p Params){
//p.put_custom("sender", "any", sender) //add sender to params
for i, n in pb.registry.names {
2019-11-23 12:24:25 +01:00
if name == n {
eh := pb.registry.events[i]
once_index := pb.registry.once.index(pb.registry.names[i])
2019-11-23 12:24:25 +01:00
if once_index > -1 {
pb.registry.events.delete(i)
pb.registry.names.delete(i)
pb.registry.once.delete(once_index)
2019-11-23 12:24:25 +01:00
}
2019-12-18 06:16:33 +01:00
invoke(eh, sender, p)
2019-11-23 12:24:25 +01:00
}
}
}
fn (p mut Publisher) clear_all(){
2019-12-18 06:16:33 +01:00
if p.registry.names.len == 0 {return}
for i := p.registry.names.len - 1; i >= 0; i-- {
p.registry.delete_entry(i)
2019-11-23 12:24:25 +01:00
}
}
// Subscriber Methods
2019-12-18 06:16:33 +01:00
pub fn (s mut Subscriber) subscribe(name string, handler fn(voidptr, Params)){
2019-11-23 12:24:25 +01:00
s.registry.names << name
v := voidptr(handler)
s.registry.events << v
}
2019-12-18 06:16:33 +01:00
pub fn (s mut Subscriber) subscribe_once(name string, handler fn(voidptr, Params)){
2019-11-23 12:24:25 +01:00
s.subscribe(name, handler)
s.registry.once << name
}
pub fn (s &Subscriber) is_subscribed(name string) bool {
return s.registry.check_subscriber(name)
}
2019-12-18 06:16:33 +01:00
pub fn (s mut Subscriber) unsubscribe(name string, handler fn(voidptr, Params)){
2019-11-23 12:24:25 +01:00
v := voidptr(handler)
for i, n in s.registry.names {
if name == n {
eh := s.registry.events[i]
if eh == v {
s.registry.delete_entry(i)
}
}
}
}
// Registry Methods
fn (r &Registry) check_subscriber(name string) bool {
for n in r.names {
if name == n {return true}
}
return false
}
fn (r mut Registry) delete_entry(index int) {
once_index := r.once.index(r.names[index])
if once_index > -1 {
r.once.delete(once_index)
}
r.events.delete(index)
r.names.delete(index)
}
// Helper Functions
2019-12-18 06:16:33 +01:00
fn invoke(p, sender voidptr, arr Params){
2019-11-23 12:24:25 +01:00
handler := EventHandler{p}.func
2019-12-18 06:16:33 +01:00
handler(sender, arr)
2019-12-01 08:33:26 +01:00
}