2020-04-26 14:49:31 +03:00
|
|
|
import eventbus
|
2019-11-23 14:24:25 +03:00
|
|
|
|
2020-01-22 19:41:08 +03:00
|
|
|
struct EventData {
|
2020-10-15 00:39:09 +03:00
|
|
|
data string
|
2020-01-22 19:41:08 +03:00
|
|
|
}
|
|
|
|
|
2020-10-15 00:39:09 +03:00
|
|
|
fn test_eventbus() {
|
2020-01-22 19:41:08 +03:00
|
|
|
ev_data := &EventData{'hello'}
|
2019-11-23 14:24:25 +03:00
|
|
|
mut eb := eventbus.new()
|
2020-10-15 00:39:09 +03:00
|
|
|
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')
|
2019-11-23 14:24:25 +03:00
|
|
|
eb.clear_all()
|
2020-10-15 00:39:09 +03:00
|
|
|
assert !eb.has_subscriber('on_test')
|
|
|
|
assert !eb.subscriber.is_subscribed('on_test')
|
2019-11-23 14:24:25 +03:00
|
|
|
}
|
|
|
|
|
2020-07-28 19:08:16 +03:00
|
|
|
fn on_test(receiver voidptr, ev &EventData, sender voidptr) {
|
|
|
|
assert receiver == 0
|
|
|
|
assert sender != 0
|
2020-10-15 00:39:09 +03:00
|
|
|
assert ev.data == 'hello'
|
2020-01-22 23:34:38 +03:00
|
|
|
}
|