2019-11-23 14:24:25 +03:00
|
|
|
module eventbus
|
|
|
|
|
2019-11-24 14:27:50 +03:00
|
|
|
|
|
|
|
pub struct Publisher {
|
|
|
|
mut:
|
|
|
|
registry &Registry
|
|
|
|
}
|
|
|
|
|
2019-11-23 14:24:25 +03:00
|
|
|
pub struct Subscriber {
|
|
|
|
mut:
|
|
|
|
registry &Registry
|
|
|
|
}
|
2019-11-24 14:27:50 +03:00
|
|
|
|
2019-11-23 14:24:25 +03:00
|
|
|
struct Registry{
|
|
|
|
mut:
|
|
|
|
names []string
|
|
|
|
events []voidptr
|
|
|
|
once []string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EventHandler {
|
|
|
|
func fn(Params)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EventBus{
|
|
|
|
mut:
|
|
|
|
registry &Registry
|
2019-11-24 14:27:50 +03:00
|
|
|
publisher &Publisher
|
2019-11-23 14:24:25 +03:00
|
|
|
pub:
|
2019-11-24 14:27:50 +03:00
|
|
|
subscriber &Subscriber
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new() &EventBus{
|
|
|
|
registry := &Registry{
|
|
|
|
names: []
|
|
|
|
events: []
|
|
|
|
once: []
|
|
|
|
}
|
|
|
|
return &EventBus{
|
|
|
|
registry,
|
2019-11-24 14:27:50 +03:00
|
|
|
&Publisher{registry},
|
|
|
|
&Subscriber{registry}
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:27:50 +03:00
|
|
|
// EventBus Methods
|
|
|
|
|
|
|
|
pub fn (eb &EventBus) publish(name string, p Params) {
|
|
|
|
mut publisher := eb.publisher
|
|
|
|
publisher.publish(name, 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
|
|
|
|
|
|
|
|
fn (pb mut Publisher) publish(name string, p Params){
|
|
|
|
for i, n in pb.registry.names {
|
2019-11-23 14:24:25 +03:00
|
|
|
if name == n {
|
2019-11-24 14:27:50 +03:00
|
|
|
eh := pb.registry.events[i]
|
2019-11-23 14:24:25 +03:00
|
|
|
invoke(eh, p)
|
2019-11-24 14:27:50 +03:00
|
|
|
once_index := pb.registry.once.index(pb.registry.names[i])
|
2019-11-23 14:24:25 +03:00
|
|
|
if once_index > -1 {
|
2019-11-24 14:27:50 +03:00
|
|
|
pb.registry.events.delete(i)
|
|
|
|
pb.registry.names.delete(i)
|
|
|
|
pb.registry.once.delete(once_index)
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:27:50 +03:00
|
|
|
fn (p mut Publisher) clear_all(){
|
|
|
|
for i, n in p.registry.names {
|
|
|
|
p.registry.delete_entry(i)
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscriber Methods
|
|
|
|
|
|
|
|
pub fn (s mut Subscriber) subscribe(name string, handler fn(Params)){
|
|
|
|
s.registry.names << name
|
|
|
|
v := voidptr(handler)
|
|
|
|
s.registry.events << v
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s mut Subscriber) subscribe_once(name string, handler fn(Params)){
|
|
|
|
s.subscribe(name, handler)
|
|
|
|
s.registry.once << name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s &Subscriber) is_subscribed(name string) bool {
|
|
|
|
return s.registry.check_subscriber(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s mut Subscriber) unsubscribe(name string, handler fn(Params)){
|
|
|
|
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
|
|
|
|
|
|
|
|
fn invoke(p voidptr, arr Params){
|
|
|
|
handler := EventHandler{p}.func
|
|
|
|
handler(arr)
|
2019-12-01 10:33:26 +03:00
|
|
|
}
|