2019-11-24 14:27:50 +03:00
|
|
|
# Event Bus
|
|
|
|
|
|
|
|
A module to provide eventing capabilities using pub/sub.
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
1. `new[T]()` - create a new `EventBus`
|
|
|
|
2. `EventBus.new[T]()` - create a new `EventBus`
|
2019-11-24 14:27:50 +03:00
|
|
|
|
|
|
|
### Structs:
|
|
|
|
|
|
|
|
**EventBus:**
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
1. `publish(name T, sender voidptr, args voidptr)` - publish an event with provided
|
2020-11-18 20:28:28 +03:00
|
|
|
Params & name
|
2019-11-24 14:27:50 +03:00
|
|
|
2. `clear_all()` - clear all subscribers
|
2023-07-07 22:33:57 +03:00
|
|
|
3. `has_subscriber(name T)` - check if a subscriber to an event exists
|
2019-11-24 14:27:50 +03:00
|
|
|
|
|
|
|
**Subscriber:**
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
1. `subscribe(name T, handler EventHandlerFn)` - subscribe to an event
|
|
|
|
2. `subscribe_once(name T, handler EventHandlerFn)` - subscribe only once to an event
|
|
|
|
3. `subscribe_method(name T, handler EventHandlerFn, receiver voidptr)` - subscribe to
|
2020-11-18 20:28:28 +03:00
|
|
|
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.
|
2023-07-07 22:33:57 +03:00
|
|
|
4. `is_subscribed(name T)` - check if we are subscribed to an event
|
|
|
|
5. `unsubscribe(name T)` - unsubscribe from an event
|
2019-11-24 14:27:50 +03:00
|
|
|
|
|
|
|
**Event Handler Signature:**
|
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
The function given to `subscribe`, `subscribe_method` and `subscribe_once` must match this:
|
2019-11-24 14:27:50 +03:00
|
|
|
|
2020-11-18 20:28:28 +03:00
|
|
|
```v oksyntax
|
2020-11-27 14:03:32 +03:00
|
|
|
fn cb(receiver voidptr, args voidptr, sender voidptr) {
|
2019-11-24 14:27:50 +03:00
|
|
|
}
|
2020-01-22 19:41:08 +03:00
|
|
|
|
|
|
|
// Since V can map structs to voidptr, this also works
|
|
|
|
struct ClickEvent {
|
2020-12-06 00:54:41 +03:00
|
|
|
x int
|
|
|
|
y int
|
2020-01-22 19:41:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example case where publisher sends ClickEvent as args.
|
2020-12-06 00:54:41 +03:00
|
|
|
fn on_press(receiver voidptr, e &ClickEvent, sender voidptr) {
|
|
|
|
println(e.x)
|
|
|
|
// your code here...
|
2019-11-24 14:27:50 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2020-12-06 00:54:41 +03:00
|
|
|
For **usage across modules**
|
2020-11-27 14:03:32 +03:00
|
|
|
[check the example](https://github.com/vlang/v/tree/master/examples/eventbus).
|
2019-11-24 14:27:50 +03:00
|
|
|
|
2023-02-13 11:29:02 +03:00
|
|
|
> **Note**
|
|
|
|
> As a general rule, you will need to **subscribe before publishing**.
|
2019-11-24 14:27:50 +03:00
|
|
|
|
|
|
|
**main.v**
|
|
|
|
|
2020-11-18 20:28:28 +03:00
|
|
|
```v oksyntax
|
2019-11-24 14:27:50 +03:00
|
|
|
module main
|
2020-12-06 00:54:41 +03:00
|
|
|
|
2019-11-24 14:27:50 +03:00
|
|
|
import eventbus
|
|
|
|
|
|
|
|
// initialize it globally
|
|
|
|
const (
|
2023-07-07 22:33:57 +03:00
|
|
|
eb = eventbus.new[string]()
|
2019-11-24 14:27:50 +03:00
|
|
|
)
|
|
|
|
|
2020-12-06 00:54:41 +03:00
|
|
|
fn main() {
|
|
|
|
// get a mutable reference to the subscriber
|
2021-01-25 14:08:43 +03:00
|
|
|
mut sub := eb.subscriber
|
2020-12-06 00:54:41 +03:00
|
|
|
// subscribe to the 'error' event
|
|
|
|
sub.subscribe('error', on_error)
|
|
|
|
// start the work
|
2019-11-24 14:27:50 +03:00
|
|
|
do_work()
|
|
|
|
}
|
|
|
|
|
|
|
|
// the event handler
|
2020-07-28 19:08:16 +03:00
|
|
|
fn on_error(receiver voidptr, e &Error, work &Work) {
|
2023-02-16 12:43:39 +03:00
|
|
|
println('error occurred on ${work.hours}. Error: ${e.message}')
|
2019-11-24 14:27:50 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
**work.v**
|
|
|
|
|
2020-11-18 20:28:28 +03:00
|
|
|
```v oksyntax
|
2019-11-24 14:27:50 +03:00
|
|
|
module main
|
|
|
|
|
2021-06-25 13:08:56 +03:00
|
|
|
import eventbus
|
|
|
|
|
2023-07-07 22:33:57 +03:00
|
|
|
const eb = eventbus.new[string]()
|
2021-06-25 13:08:56 +03:00
|
|
|
|
2020-12-06 00:54:41 +03:00
|
|
|
struct Work {
|
|
|
|
hours int
|
2019-12-18 08:16:33 +03:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:08:56 +03:00
|
|
|
struct AnError {
|
2020-12-06 00:54:41 +03:00
|
|
|
message string
|
2020-01-22 19:41:08 +03:00
|
|
|
}
|
|
|
|
|
2020-12-06 00:54:41 +03:00
|
|
|
fn do_work() {
|
|
|
|
work := Work{20}
|
|
|
|
// get a mutable Params instance & put some data into it
|
2021-06-25 13:08:56 +03:00
|
|
|
error := &AnError{'Error: no internet connection.'}
|
2020-12-06 00:54:41 +03:00
|
|
|
// publish the event
|
|
|
|
eb.publish('error', work, error)
|
2019-11-24 14:27:50 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Notes:
|
|
|
|
|
2020-11-18 20:28:28 +03:00
|
|
|
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-11-24 14:27:50 +03:00
|
|
|
|
2019-12-18 08:16:33 +03:00
|
|
|
**The rationale behind separating Subscriber & Publisher:**
|
2019-11-24 14:27:50 +03:00
|
|
|
|
2020-11-18 20:28:28 +03:00
|
|
|
This is mainly for security because if publisher & subscriber are both passed around,
|
|
|
|
a client can easily publish events acting as the server.
|
2020-07-28 19:08:16 +03:00
|
|
|
So a client should only be able to use the Subscriber methods.
|