eventbus: removed check on publish, always use receiver, args, sender order for callbacks (#5940)

pull/5963/head^2
Carlos Esquerdo Bernat 2020-07-28 17:48:25 +02:00 committed by GitHub
parent 9c028bb299
commit 0539b2a7ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 19 deletions

View File

@ -1,6 +1,6 @@
module eventbus
pub type EventHandlerFn fn(voidptr, voidptr, voidptr)
pub type EventHandlerFn fn(receiver voidptr, args voidptr, sender voidptr)
pub struct Publisher {
mut:
@ -20,7 +20,7 @@ mut:
struct EventHandler {
name string
handler EventHandlerFn
receiver voidptr
receiver voidptr = voidptr(0)
once bool
}
@ -64,11 +64,7 @@ fn (mut pb Publisher) publish(name string, sender voidptr, args voidptr) {
if event.once {
pb.registry.events.delete(i)
}
if event.receiver != 0 {
event.handler(event.receiver, args, sender)
} else {
event.handler(sender, args, voidptr(0))
}
}
}
}
@ -87,7 +83,6 @@ pub fn (mut s Subscriber) subscribe(name string, handler EventHandlerFn) {
s.registry.events << EventHandler {
name: name
handler: handler
receiver: voidptr(0)
}
}
@ -103,7 +98,6 @@ pub fn (mut s Subscriber) subscribe_once(name string, handler EventHandlerFn) {
s.registry.events << EventHandler {
name: name
handler: handler
receiver: voidptr(0)
once: true
}
}

View File

@ -4,8 +4,8 @@ import time
struct Test {
mut:
connected bool = false
sended_msg []string = []
recieved_msg []string = []
sent_messages []string = []
received_messages []string = []
}
fn test_ws() {
@ -25,7 +25,7 @@ fn ws_test(uri string) {
go ws.listen()
text := ['ws test', '{"vlang": "test0\n192"}']
for msg in text {
test.sended_msg << msg
test.sent_messages << msg
len := ws.write(msg.str, msg.len, .text_frame)
assert msg.len <= len
// sleep to give time to recieve response before send a new one
@ -35,13 +35,13 @@ fn ws_test(uri string) {
time.sleep_ms(500)
assert test.connected == true
assert test.sended_msg.len == test.recieved_msg.len
for x, msg in test.sended_msg {
assert msg == test.recieved_msg[x]
assert test.sent_messages.len == test.received_messages.len
for x, msg in test.sent_messages {
assert msg == test.received_messages[x]
}
}
fn on_open(mut test Test, y voidptr, ws &websocket.Client) {
fn on_open(mut test Test, x voidptr, ws &websocket.Client) {
println('websocket opened.')
test.connected = true
}
@ -50,17 +50,17 @@ fn on_message(mut test Test, msg &websocket.Message, ws &websocket.Client) {
typ := msg.opcode
if typ == .text_frame {
println('Message: ${cstring_to_vstring(msg.payload)}')
test.recieved_msg << cstring_to_vstring(msg.payload)
test.received_messages << cstring_to_vstring(msg.payload)
} else {
println('Binary message: $msg')
}
}
fn on_close(mut test Test, y voidptr, ws &websocket.Client) {
fn on_close(x, y voidptr, ws &websocket.Client) {
println('websocket closed.')
}
fn on_error(ws &websocket.Client, x, y voidptr) {
fn on_error(x, y voidptr, ws &websocket.Client) {
println('we have an error.')
assert false
}