eventbus: update test and docs too

pull/6008/head^2
Delyan Angelov 2020-07-28 19:08:16 +03:00
parent 0539b2a7ea
commit e06448b616
2 changed files with 10 additions and 6 deletions

View File

@ -18,7 +18,7 @@ A module to provide eventing capabilities using pub/sub.
1. `subscribe(name string, handler EventHandlerFn)` - subscribe to an event 1. `subscribe(name string, handler EventHandlerFn)` - subscribe to an event
2. `subscribe_once(name string, handler EventHandlerFn)` - subscribe only once to an event 2. `subscribe_once(name string, handler EventHandlerFn)` - subscribe only once to an event
3. `subscribe_method(name string, handler EventHandlerFn, reciever voidptr)` - subscribe to an event and also recieve the `reciever` as a parameter. Since it's not yet possible to send methods as parameters, this is the workaround. 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 4. `is_subscribed(name string)` - check if we are subscribed to an event
5. `unsubscribe(name string)` - unsubscribe from an event 5. `unsubscribe(name string)` - unsubscribe from an event
@ -27,7 +27,7 @@ A module to provide eventing capabilities using pub/sub.
The function given to `subscribe`, `subscribe_method` and `subscribe_once` must match this: The function given to `subscribe`, `subscribe_method` and `subscribe_once` must match this:
```v ```v
fn(voidptr, voidptr, voidptr){ fn(receiver voidptr, args voidptr, sender voidptr){
} }
@ -38,7 +38,7 @@ struct ClickEvent {
} }
// Example case where publisher sends ClickEvent as args. // Example case where publisher sends ClickEvent as args.
fn onPress(sender voidptr, e &ClickEvent){ fn onPress(receiver voidptr, e &ClickEvent, sender voidptr){
println(e.x) println(e.x)
//your code here... //your code here...
} }
@ -71,7 +71,7 @@ fn main(){
} }
// the event handler // the event handler
fn on_error(work &Work, e &Error) { fn on_error(receiver voidptr, e &Error, work &Work) {
println('error occured on ${work.hours}. Error: ${e.message}') println('error occured on ${work.hours}. Error: ${e.message}')
} }
``` ```
@ -106,4 +106,6 @@ fn do_work(){
**The rationale behind separating Subscriber & Publisher:** **The rationale behind separating Subscriber & Publisher:**
This is mainly for security because the 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. 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.

View File

@ -28,6 +28,8 @@ fn test_eventbus(){
assert !eb.subscriber.is_subscribed("on_test") assert !eb.subscriber.is_subscribed("on_test")
} }
fn on_test(sender voidptr, ev &EventData, x voidptr) { fn on_test(receiver voidptr, ev &EventData, sender voidptr) {
assert receiver == 0
assert sender != 0
assert ev.data == "hello" assert ev.data == "hello"
} }