2019-11-24 12:27:50 +01:00
# Event Bus
A module to provide eventing capabilities using pub/sub.
## API
1. `new()` - create a new `EventBus`
### Structs:
**EventBus:**
2019-12-18 06:16:33 +01:00
1. `publish(string, voidptr, Params)` - publish an event with provided Params & name
2019-11-24 12:27:50 +01:00
2. `clear_all()` - clear all subscribers
3. `has_subscriber(string)` - check if a subscriber to an event exists
**Subscriber:**
2019-12-18 06:16:33 +01:00
1. `subscribe(string, fn(voidptr, Params))` - subscribe to an event
2. `subscribe_once(string, fn(voidptr, Params))` - subscribe only once to an event
2019-11-24 12:27:50 +01:00
3. `is_subscribed(string)` - check if we are subscribed to an event
4. `unsubscribe(string)` - unsubscribe from an event
**Event Handler Signature:**
The function given to `subscribe` and `subscribe_once` must match this:
```v
2019-12-18 06:16:33 +01:00
fn(voidptr, Params){
2019-11-24 12:27:50 +01:00
}
// Example
2019-12-18 06:16:33 +01:00
fn onPress(sender voidptr, p Params){
2019-11-24 12:27:50 +01:00
//your code here...
}
```
## Usage
For **usage across modules** [check the example ](https://github.com/vlang/v/tree/master/examples/eventbus ).
2019-12-18 06:16:33 +01:00
_Note: As a general rule, you will need to **subscribe before publishing** ._
2019-11-24 12:27:50 +01:00
**main.v**
```v
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
2019-12-18 06:16:33 +01:00
fn on_error(sender voidptr, p eventbus.Params) {
//cast the sender to the real type
//you can also make this mutable if required.
work := *(*Work(sender)) //a little verbose but works
error := p.get_string("error")
println('error occured on ${work.hours}. Error: ${error}')
2019-11-24 12:27:50 +01:00
}
```
**work.v**
```v
module main
import (
eventbus
)
2019-12-18 06:16:33 +01:00
struct Work{
hours int
}
2019-11-24 12:27:50 +01:00
fn do_work(){
2019-12-18 06:16:33 +01:00
work := Work{20}
2019-11-24 12:27:50 +01:00
// get a mutable Params instance & put some data into it
mut params := eventbus.Params{}
params.put_string("error", "Error: no internet connection.")
// publish the event
2019-12-18 06:16:33 +01:00
eb.publish("error", work, params)
2019-11-24 12:27:50 +01:00
}
```
### How to use `Params`:
```v
mut params := eventbus.Params{}
2019-12-18 06:16:33 +01:00
params.put_string("string", "some_string")
2019-11-24 12:27:50 +01:00
params.put_int("int", 20)
2019-12-18 06:16:33 +01:00
params.put_bool("bool", true)
// add maps and arrays of any type like this
arr := [1,2,3]
params.put_array("array", arr)
mp := {"hello": "world"}
params.put_map("map", mp)
//get and use the params like this
assert params.get_string("string") == "some_string"
assert params.get_int("int") == 20
assert params.get_bool("bool") == true
g_arr := params.get_array("array", 0)
assert g_arr[0] == 1
g_m := params.get_map("map", "")
assert g_m["hello"] == "world"
2019-11-24 12:27:50 +01:00
```
2019-12-18 06:16:33 +01:00
#### Caution when putting arrays:
Currently putting arrays and maps directly as parameters in `put_array` doesn't work, so make a variable first and use that.
2019-11-24 12:27:50 +01:00
### 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).
2019-12-18 06:16:33 +01:00
**The rationale behind separating Subscriber & Publisher:**
2019-11-24 12:27:50 +01:00
2019-12-18 06:16:33 +01:00
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.