diff options
Diffstat (limited to 'v_windows/v/old/vlib/eventbus')
-rw-r--r-- | v_windows/v/old/vlib/eventbus/README.md | 122 | ||||
-rw-r--r-- | v_windows/v/old/vlib/eventbus/eventbus.v | 121 | ||||
-rw-r--r-- | v_windows/v/old/vlib/eventbus/eventbus_test.v | 109 |
3 files changed, 352 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/eventbus/README.md b/v_windows/v/old/vlib/eventbus/README.md new file mode 100644 index 0000000..93047db --- /dev/null +++ b/v_windows/v/old/vlib/eventbus/README.md @@ -0,0 +1,122 @@ +# Event Bus + +A module to provide eventing capabilities using pub/sub. + +## API + +1. `new()` - create a new `EventBus` + +### Structs: + +**EventBus:** + +1. `publish(name string, sender voidptr, args voidptr)` - publish an event with provided + Params & name +2. `clear_all()` - clear all subscribers +3. `has_subscriber(name string)` - check if a subscriber to an event exists + +**Subscriber:** + +1. `subscribe(name string, handler EventHandlerFn)` - subscribe to an event +2. `subscribe_once(name string, handler EventHandlerFn)` - subscribe only once to an event +3. `subscribe_method(name string, handler EventHandlerFn, receiver voidptr)` - subscribe to + an event and also set the `receiver` as a parameter. + Since it's not yet possible to send methods as parameters, this is a workaround. +4. `is_subscribed(name string)` - check if we are subscribed to an event +5. `unsubscribe(name string)` - unsubscribe from an event + +**Event Handler Signature:** + +The function given to `subscribe`, `subscribe_method` and `subscribe_once` must match this: + +```v oksyntax +fn cb(receiver voidptr, args voidptr, sender voidptr) { +} + +// Since V can map structs to voidptr, this also works +struct ClickEvent { + x int + y int +} + +// Example case where publisher sends ClickEvent as args. +fn on_press(receiver voidptr, e &ClickEvent, sender voidptr) { + println(e.x) + // your code here... +} +``` + +## Usage + +For **usage across modules** +[check the example](https://github.com/vlang/v/tree/master/examples/eventbus). + +_Note: As a general rule, you will need to **subscribe before publishing**._ + +**main.v** + +```v oksyntax +module main + +import eventbus + +// initialize it globally +const ( + eb = eventbus.new() +) + +fn main() { + // get a mutable reference to the subscriber + mut sub := eb.subscriber + // subscribe to the 'error' event + sub.subscribe('error', on_error) + // start the work + do_work() +} + +// the event handler +fn on_error(receiver voidptr, e &Error, work &Work) { + println('error occured on ${work.hours}. Error: $e.message') +} +``` + +**work.v** + +```v oksyntax +module main + +import eventbus + +const eb = eventbus.new() + +struct Work { + hours int +} + +struct AnError { + message string +} + +fn do_work() { + work := Work{20} + // get a mutable Params instance & put some data into it + error := &AnError{'Error: no internet connection.'} + // publish the event + eb.publish('error', work, error) +} +``` + +### Notes: + +1. Each `EventBus` instance has it's own registry (i.e. there is no global event registry + so you can't just subscribe to an event wherever you are. +2. Each `EventBus` has a `Subscriber` instance which will need to be either exposed or you can make + small public helper functions specific to your module like (`onPress`, `onError`) and etc. +3. The `eventbus` module has some helpers to ease getting/setting of Params + (since V doesn't support empty interfaces yet or reflection) so use them (see usage above). + +**The rationale behind separating Subscriber & Publisher:** + +This is mainly for security because if publisher & subscriber are both passed around, +a client can easily publish events acting as the server. +So a client should only be able to use the Subscriber methods. diff --git a/v_windows/v/old/vlib/eventbus/eventbus.v b/v_windows/v/old/vlib/eventbus/eventbus.v new file mode 100644 index 0000000..69cf118 --- /dev/null +++ b/v_windows/v/old/vlib/eventbus/eventbus.v @@ -0,0 +1,121 @@ +module eventbus + +pub type EventHandlerFn = fn (receiver voidptr, args voidptr, sender voidptr) + +pub struct Publisher { +mut: + registry &Registry +} + +pub struct Subscriber { +mut: + registry &Registry +} + +struct Registry { +mut: + events []EventHandler +} + +struct EventHandler { + name string + handler EventHandlerFn + receiver voidptr = voidptr(0) + once bool +} + +pub struct EventBus { +pub mut: + registry &Registry + publisher &Publisher + subscriber &Subscriber +} + +pub fn new() &EventBus { + registry := &Registry{ + events: [] + } + return &EventBus{registry, &Publisher{registry}, &Subscriber{registry}} +} + +// EventBus Methods +pub fn (eb &EventBus) publish(name string, sender voidptr, args voidptr) { + mut publisher := eb.publisher + publisher.publish(name, sender, args) +} + +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 (mut pb Publisher) publish(name string, sender voidptr, args voidptr) { + for event in pb.registry.events { + if event.name == name { + event.handler(event.receiver, args, sender) + } + } + pb.registry.events = pb.registry.events.filter(!(it.name == name && it.once)) +} + +fn (mut p Publisher) clear_all() { + p.registry.events.clear() +} + +// Subscriber Methods +pub fn (mut s Subscriber) subscribe(name string, handler EventHandlerFn) { + s.registry.events << EventHandler{ + name: name + handler: handler + } +} + +pub fn (mut s Subscriber) subscribe_method(name string, handler EventHandlerFn, receiver voidptr) { + s.registry.events << EventHandler{ + name: name + handler: handler + receiver: receiver + } +} + +// unsubscribe_method unsubscribe a receiver for only one method +pub fn (mut s Subscriber) unsubscribe_method(name string, receiver voidptr) { + s.registry.events = s.registry.events.filter(!(it.name == name && it.receiver == receiver)) +} + +// unsubscribe_receiver unsubscribes a receiver from all events +pub fn (mut s Subscriber) unsubscribe_receiver(receiver voidptr) { + s.registry.events = s.registry.events.filter(it.receiver != receiver) +} + +pub fn (mut s Subscriber) subscribe_once(name string, handler EventHandlerFn) { + s.registry.events << EventHandler{ + name: name + handler: handler + once: true + } +} + +pub fn (s &Subscriber) is_subscribed(name string) bool { + return s.registry.check_subscriber(name) +} + +// is_subscribed_method checks whether a receiver was already subscribed for any events +pub fn (s &Subscriber) is_subscribed_method(name string, receiver voidptr) bool { + return s.registry.events.any(it.name == name && it.receiver == receiver) +} + +pub fn (mut s Subscriber) unsubscribe(name string, handler EventHandlerFn) { + // v := voidptr(handler) + s.registry.events = s.registry.events.filter(!(it.name == name && it.handler == handler)) +} + +// Registry Methods +fn (r &Registry) check_subscriber(name string) bool { + return r.events.any(it.name == name) +} diff --git a/v_windows/v/old/vlib/eventbus/eventbus_test.v b/v_windows/v/old/vlib/eventbus/eventbus_test.v new file mode 100644 index 0000000..e27af88 --- /dev/null +++ b/v_windows/v/old/vlib/eventbus/eventbus_test.v @@ -0,0 +1,109 @@ +import eventbus + +struct EventData { + data string +} + +struct FakeReceiver { + ok bool +} + +fn test_eventbus() { + ev_data := &EventData{'hello'} + mut eb := eventbus.new() + eb.subscriber.subscribe_once('on_test', on_test) + assert eb.has_subscriber('on_test') + assert eb.subscriber.is_subscribed('on_test') + eb.publish('on_test', eb, ev_data) + assert !eb.has_subscriber('on_test') + assert !eb.subscriber.is_subscribed('on_test') + eb.subscriber.subscribe('on_test', on_test) + assert eb.has_subscriber('on_test') + assert eb.subscriber.is_subscribed('on_test') + eb.clear_all() + assert !eb.has_subscriber('on_test') + assert !eb.subscriber.is_subscribed('on_test') +} + +fn test_subscribe_method() { + // Does not really test subscribe_method idinvidually though + // given + mut eb := eventbus.new() + r := FakeReceiver{} + + assert !eb.subscriber.is_subscribed_method('on_test_with_receiver', r) + // when + eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r) + + // then + assert eb.subscriber.is_subscribed_method('on_test_with_receiver', r) +} + +fn test_unsubscribe_method() { + // given + mut eb := eventbus.new() + r := FakeReceiver{} + r2 := FakeReceiver{} + + // when + eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r) + eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r2) + eb.subscriber.unsubscribe_method('on_test_with_receiver', r) + + // then + assert !eb.subscriber.is_subscribed_method('on_test_with_receiver', r) + assert eb.subscriber.is_subscribed_method('on_test_with_receiver', r2) +} + +fn test_publish() { + // given + ev_data := &EventData{'hello'} + mut eb := eventbus.new() + + // when + eb.subscriber.subscribe_once('on_test', on_test) + eb.subscriber.subscribe_once('on_test', on_test) + eb.publish('on_test', eb, ev_data) + + // then + assert !eb.subscriber.is_subscribed('on_test') +} + +fn test_publish_with_receiver() { + // given + mut eb := eventbus.new() + ev_data := &EventData{'hello'} + r := FakeReceiver{} + + // when + eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r) + eb.publish('on_test_with_receiver', eb, ev_data) + + // then asserts are in on_test_with_receiver, don't know how to be sure + // that it has been properly called... +} + +fn test_unsubscribe_reveiver() { + // given + mut eb := eventbus.new() + r := &FakeReceiver{} + + // when + eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r) + eb.subscriber.subscribe_method('on_test', on_test, r) + eb.subscriber.unsubscribe_receiver(r) + assert !eb.subscriber.is_subscribed_method('on_test_with_receiver', r) + assert !eb.subscriber.is_subscribed_method('on_test', r) +} + +fn on_test(receiver voidptr, ev &EventData, sender voidptr) { + assert receiver == 0 + assert sender != 0 + assert ev.data == 'hello' +} + +fn on_test_with_receiver(receiver &FakeReceiver, ev &EventData, sender voidptr) { + assert receiver.ok == false + assert sender != 0 + assert ev.data == 'hello' +} |