1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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 module eventbus
pub type EventHandlerFn fn(voidptr, voidptr, voidptr) pub type EventHandlerFn fn(receiver voidptr, args voidptr, sender voidptr)
pub struct Publisher { pub struct Publisher {
mut: mut:
@ -20,7 +20,7 @@ mut:
struct EventHandler { struct EventHandler {
name string name string
handler EventHandlerFn handler EventHandlerFn
receiver voidptr receiver voidptr = voidptr(0)
once bool once bool
} }
@ -64,11 +64,7 @@ fn (mut pb Publisher) publish(name string, sender voidptr, args voidptr) {
if event.once { if event.once {
pb.registry.events.delete(i) pb.registry.events.delete(i)
} }
if event.receiver != 0 {
event.handler(event.receiver, args, sender) 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 { s.registry.events << EventHandler {
name: name name: name
handler: handler handler: handler
receiver: voidptr(0)
} }
} }
@ -103,7 +98,6 @@ pub fn (mut s Subscriber) subscribe_once(name string, handler EventHandlerFn) {
s.registry.events << EventHandler { s.registry.events << EventHandler {
name: name name: name
handler: handler handler: handler
receiver: voidptr(0)
once: true once: true
} }
} }

View File

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