From 066aec146a075d0091903b490b6969a172b9d270 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 22 Nov 2019 05:13:02 -0500 Subject: thread through the id to the actual message events this is more accurate, since managing the lifetime of the websocket connection is a bit tricky --- teleterm-web/src/lib.rs | 16 ++++--------- teleterm-web/src/model.rs | 15 ++---------- teleterm-web/src/ws.rs | 13 +++++++--- teleterm/static/teleterm_web.js | 45 ++++++++++++++++++----------------- teleterm/static/teleterm_web_bg.wasm | Bin 388582 -> 389447 bytes 5 files changed, 39 insertions(+), 50 deletions(-) diff --git a/teleterm-web/src/lib.rs b/teleterm-web/src/lib.rs index 5c7d5c1..9ec713e 100644 --- a/teleterm-web/src/lib.rs +++ b/teleterm-web/src/lib.rs @@ -16,7 +16,7 @@ enum Msg { List(seed::fetch::ResponseDataResult>), Refresh, StartWatching(String), - Watch(ws::WebSocketEvent), + Watch(String, ws::WebSocketEvent), } fn init(_: Url, orders: &mut impl Orders) -> Init { @@ -49,7 +49,7 @@ fn update( log(&format!("watching {}", id)); model.watch(&id, orders); } - Msg::Watch(event) => match event { + Msg::Watch(id, event) => match event { ws::WebSocketEvent::Connected(_) => { log("connected"); } @@ -58,18 +58,10 @@ fn update( model.watch_disconnect(); } ws::WebSocketEvent::Message(msg) => { - log(&format!( - "message from id {}: {:?}", - model.watch_id().unwrap(), - msg - )); + log(&format!("message from id {}: {:?}", id, msg)); } ws::WebSocketEvent::Error(e) => { - log(&format!( - "error from id {}: {:?}", - model.watch_id().unwrap(), - e - )); + log(&format!("error from id {}: {:?}", id, e)); } }, } diff --git a/teleterm-web/src/model.rs b/teleterm-web/src/model.rs index 0e6f82d..fb36902 100644 --- a/teleterm-web/src/model.rs +++ b/teleterm-web/src/model.rs @@ -4,7 +4,6 @@ const LIST_URL: &str = "http://127.0.0.1:4145/list"; const WATCH_URL: &str = "ws://127.0.0.1:4145/watch"; struct WatchConn { - id: String, #[allow(dead_code)] // no idea why it thinks this is dead ws: WebSocket, } @@ -35,13 +34,11 @@ impl Model { ) { let ws = crate::ws::connect( &format!("{}?id={}", WATCH_URL, id), + id, crate::Msg::Watch, orders, ); - self.watch_conn = Some(WatchConn { - id: id.to_string(), - ws, - }) + self.watch_conn = Some(WatchConn { ws }) } pub fn sessions(&self) -> &[Session] { @@ -52,14 +49,6 @@ impl Model { self.sessions = sessions; } - pub fn watch_id(&self) -> Option<&str> { - if let Some(conn) = &self.watch_conn { - Some(&conn.id) - } else { - None - } - } - pub fn watch_disconnect(&mut self) { self.watch_conn = None; } diff --git a/teleterm-web/src/ws.rs b/teleterm-web/src/ws.rs index a0c30a4..c5f9761 100644 --- a/teleterm-web/src/ws.rs +++ b/teleterm-web/src/ws.rs @@ -11,12 +11,14 @@ pub(crate) enum WebSocketEvent { pub(crate) fn connect( url: &str, - msg: fn(WebSocketEvent) -> crate::Msg, + id: &str, + msg: fn(String, WebSocketEvent) -> crate::Msg, orders: &mut impl Orders, ) -> WebSocket { let ws = WebSocket::new(url).unwrap(); register_ws_handler( + id, WebSocket::set_onopen, WebSocketEvent::Connected, msg, @@ -24,6 +26,7 @@ pub(crate) fn connect( orders, ); register_ws_handler( + id, WebSocket::set_onclose, WebSocketEvent::Disconnected, msg, @@ -31,6 +34,7 @@ pub(crate) fn connect( orders, ); register_ws_handler( + id, WebSocket::set_onmessage, WebSocketEvent::Message, msg, @@ -38,6 +42,7 @@ pub(crate) fn connect( orders, ); register_ws_handler( + id, WebSocket::set_onerror, WebSocketEvent::Error, msg, @@ -49,9 +54,10 @@ pub(crate) fn connect( } fn register_ws_handler( + id: &str, ws_cb_setter: fn(&WebSocket, Option<&js_sys::Function>), msg: F, - ws_msg: fn(WebSocketEvent) -> crate::Msg, + ws_msg: fn(String, WebSocketEvent) -> crate::Msg, ws: &web_sys::WebSocket, orders: &mut impl Orders, ) where @@ -60,8 +66,9 @@ fn register_ws_handler( { let (app, msg_mapper) = (orders.clone_app(), orders.msg_mapper()); + let id = id.to_string(); let closure = Closure::new(move |data| { - app.update(msg_mapper(ws_msg(msg(data)))); + app.update(msg_mapper(ws_msg(id.clone(), msg(data)))); }); ws_cb_setter(ws, Some(closure.as_ref().unchecked_ref())); diff --git a/teleterm/static/teleterm_web.js b/teleterm/static/teleterm_web.js index 36cfe4b..1132418 100644 --- a/teleterm/static/teleterm_web.js +++ b/teleterm/static/teleterm_web.js @@ -1,6 +1,13 @@ let wasm; +function __wbg_elem_binding0(arg0, arg1, arg2) { + wasm.__wbg_function_table.get(54)(arg0, arg1, arg2); +} +function __wbg_elem_binding1(arg0, arg1) { + wasm.__wbg_function_table.get(174)(arg0, arg1); +} + const heap = new Array(32); heap.fill(undefined); @@ -17,23 +24,17 @@ function addHeapObject(obj) { heap[idx] = obj; return idx; } -function __wbg_elem_binding0(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(244)(arg0, arg1, addHeapObject(arg2)); -} -function __wbg_elem_binding1(arg0, arg1) { - wasm.__wbg_function_table.get(174)(arg0, arg1); -} function __wbg_elem_binding2(arg0, arg1, arg2) { wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding3(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(54)(arg0, arg1, arg2); + wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding4(arg0, arg1, arg2) { wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding5(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); + wasm.__wbg_function_table.get(244)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding6(arg0, arg1, arg2) { wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); @@ -871,7 +872,7 @@ function init(module) { const a = state.a; state.a = 0; try { - return __wbg_elem_binding2(a, state.b, arg0); + return __wbg_elem_binding3(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; @@ -882,16 +883,16 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper213 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper505 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; - const real = (arg0) => { + const real = () => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding5(a, state.b, arg0); + return __wbg_elem_binding1(a, state.b, ); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(175)(a, state.b); else state.a = a; } } @@ -900,16 +901,16 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper503 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper219 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; - const real = () => { + const real = (arg0) => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding1(a, state.b, ); + return __wbg_elem_binding6(a, state.b, arg0); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(175)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; } } @@ -925,7 +926,7 @@ function init(module) { const a = state.a; state.a = 0; try { - return __wbg_elem_binding3(a, state.b, arg0); + return __wbg_elem_binding0(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; @@ -936,7 +937,7 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper219 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper213 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; const real = (arg0) => { state.cnt++; @@ -961,7 +962,7 @@ function init(module) { const a = state.a; state.a = 0; try { - return __wbg_elem_binding6(a, state.b, arg0); + return __wbg_elem_binding2(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; @@ -972,14 +973,14 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper905 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper907 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; const real = (arg0) => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding0(a, state.b, arg0); + return __wbg_elem_binding5(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(245)(a, state.b); else state.a = a; diff --git a/teleterm/static/teleterm_web_bg.wasm b/teleterm/static/teleterm_web_bg.wasm index 2ee77b7..44138c5 100644 Binary files a/teleterm/static/teleterm_web_bg.wasm and b/teleterm/static/teleterm_web_bg.wasm differ -- cgit v1.2.3-54-g00ecf