From 5eabab2e437054787a70dcef59f0b06de0c42659 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 22 Nov 2019 03:29:12 -0500 Subject: refactor --- teleterm-web/src/lib.rs | 60 +++++-------------- teleterm-web/src/model.rs | 62 ++++++++++++++++++++ teleterm/static/teleterm_web.js | 108 +++++++++++++++++------------------ teleterm/static/teleterm_web_bg.wasm | Bin 386099 -> 388509 bytes 4 files changed, 131 insertions(+), 99 deletions(-) create mode 100644 teleterm-web/src/model.rs diff --git a/teleterm-web/src/lib.rs b/teleterm-web/src/lib.rs index c78c600..5c7d5c1 100644 --- a/teleterm-web/src/lib.rs +++ b/teleterm-web/src/lib.rs @@ -1,3 +1,4 @@ +mod model; mod prelude; mod ws; @@ -9,64 +10,33 @@ extern "C" { fn log(s: &str); } -const LIST_URL: &str = "http://127.0.0.1:4145/list"; -const WATCH_URL: &str = "ws://127.0.0.1:4145/watch"; - #[allow(clippy::large_enum_variant)] #[derive(Clone)] enum Msg { - List(seed::fetch::ResponseDataResult>), + List(seed::fetch::ResponseDataResult>), Refresh, StartWatching(String), Watch(ws::WebSocketEvent), } -struct WatchConn { - id: String, - #[allow(dead_code)] // no idea why it thinks this is dead - ws: WebSocket, -} - -#[derive(Clone, Debug, serde::Deserialize)] -struct Session { - id: String, - username: String, -} - -#[derive(Default)] -struct Model { - sessions: Vec, - watch_conn: Option, -} - -impl Model { - fn list(&self) -> impl futures::Future { - seed::Request::new(LIST_URL).fetch_json_data(Msg::List) - } - - fn watch(&mut self, id: &str, orders: &mut impl Orders) { - let ws = ws::connect(WATCH_URL, Msg::Watch, orders); - self.watch_conn = Some(WatchConn { - id: id.to_string(), - ws, - }) - } -} - -fn init(_: Url, orders: &mut impl Orders) -> Init { +fn init(_: Url, orders: &mut impl Orders) -> Init { log("init"); - let model = Model::default(); + let model = crate::model::Model::default(); orders.perform_cmd(model.list()); Init::new(model) } -fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders) { +fn update( + msg: Msg, + model: &mut crate::model::Model, + orders: &mut impl Orders, +) { log("update"); match msg { Msg::List(sessions) => match sessions { Ok(sessions) => { log("got sessions"); - model.sessions = sessions; + model.update_sessions(sessions); } Err(e) => { log(&format!("error getting sessions: {:?}", e)); @@ -85,19 +55,19 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders) { } ws::WebSocketEvent::Disconnected(_) => { log("disconnected"); - model.watch_conn = None; + model.watch_disconnect(); } ws::WebSocketEvent::Message(msg) => { log(&format!( "message from id {}: {:?}", - model.watch_conn.as_ref().unwrap().id, + model.watch_id().unwrap(), msg )); } ws::WebSocketEvent::Error(e) => { log(&format!( "error from id {}: {:?}", - model.watch_conn.as_ref().unwrap().id, + model.watch_id().unwrap(), e )); } @@ -105,10 +75,10 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders) { } } -fn view(model: &Model) -> impl View { +fn view(model: &crate::model::Model) -> impl View { log("view"); let mut list = vec![]; - for session in &model.sessions { + for session in model.sessions() { list.push(seed::li![seed::button![ simple_ev(Ev::Click, Msg::StartWatching(session.id.clone())), format!("{}: {}", session.username, session.id), diff --git a/teleterm-web/src/model.rs b/teleterm-web/src/model.rs new file mode 100644 index 0000000..9f7ab05 --- /dev/null +++ b/teleterm-web/src/model.rs @@ -0,0 +1,62 @@ +use crate::prelude::*; + +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, +} + +#[derive(Clone, Debug, serde::Deserialize)] +pub struct Session { + pub id: String, + pub username: String, +} + +#[derive(Default)] +pub struct Model { + sessions: Vec, + watch_conn: Option, +} + +impl Model { + pub(crate) fn list( + &self, + ) -> impl futures::Future { + seed::Request::new(LIST_URL).fetch_json_data(crate::Msg::List) + } + + pub(crate) fn watch( + &mut self, + id: &str, + orders: &mut impl Orders, + ) { + let ws = crate::ws::connect(WATCH_URL, crate::Msg::Watch, orders); + self.watch_conn = Some(WatchConn { + id: id.to_string(), + ws, + }) + } + + pub fn sessions(&self) -> &[Session] { + &self.sessions + } + + pub fn update_sessions(&mut self, sessions: Vec) { + 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/static/teleterm_web.js b/teleterm/static/teleterm_web.js index 45b691a..e8e2555 100644 --- a/teleterm/static/teleterm_web.js +++ b/teleterm/static/teleterm_web.js @@ -18,31 +18,31 @@ function addHeapObject(obj) { return idx; } function __wbg_elem_binding0(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(40)(arg0, arg1, addHeapObject(arg2)); + wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding1(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(45)(arg0, arg1, arg2); + wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding2(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(240)(arg0, arg1, addHeapObject(arg2)); + wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding3(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(40)(arg0, arg1, addHeapObject(arg2)); + wasm.__wbg_function_table.get(49)(arg0, arg1, addHeapObject(arg2)); } function __wbg_elem_binding4(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(40)(arg0, arg1, addHeapObject(arg2)); + wasm.__wbg_function_table.get(54)(arg0, arg1, arg2); } -function __wbg_elem_binding5(arg0, arg1) { - wasm.__wbg_function_table.get(170)(arg0, arg1); +function __wbg_elem_binding5(arg0, arg1, arg2) { + wasm.__wbg_function_table.get(242)(arg0, arg1, addHeapObject(arg2)); } -function __wbg_elem_binding6(arg0, arg1, arg2) { - wasm.__wbg_function_table.get(40)(arg0, arg1, addHeapObject(arg2)); +function __wbg_elem_binding6(arg0, arg1) { + wasm.__wbg_function_table.get(172)(arg0, arg1); } function __wbg_elem_binding7(arg0, arg1, arg2, arg3, arg4) { - wasm.__wbg_function_table.get(267)(arg0, arg1, addHeapObject(arg2), arg3, addHeapObject(arg4)); + wasm.__wbg_function_table.get(269)(arg0, arg1, addHeapObject(arg2), arg3, addHeapObject(arg4)); } function __wbg_elem_binding8(arg0, arg1, arg2, arg3) { - wasm.__wbg_function_table.get(271)(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3)); + wasm.__wbg_function_table.get(273)(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3)); } /** */ @@ -50,20 +50,6 @@ export function start() { wasm.start(); } -function getObject(idx) { return heap[idx]; } - -function dropObject(idx) { - if (idx < 36) return; - heap[idx] = heap_next; - heap_next = idx; -} - -function takeObject(idx) { - const ret = getObject(idx); - dropObject(idx); - return ret; -} - let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); @@ -80,6 +66,20 @@ function getStringFromWasm(ptr, len) { return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len)); } +function getObject(idx) { return heap[idx]; } + +function dropObject(idx) { + if (idx < 36) return; + heap[idx] = heap_next; + heap_next = idx; +} + +function takeObject(idx) { + const ret = getObject(idx); + dropObject(idx); + return ret; +} + let WASM_VECTOR_LEN = 0; let cachedTextEncoder = new TextEncoder('utf-8'); @@ -225,9 +225,16 @@ function init(module) { let result; const imports = {}; imports.wbg = {}; + imports.wbg.__wbindgen_string_new = function(arg0, arg1) { + const ret = getStringFromWasm(arg0, arg1); + return addHeapObject(ret); + }; imports.wbg.__wbindgen_object_drop_ref = function(arg0) { takeObject(arg0); }; + imports.wbg.__wbg_log_93d35dab6e237612 = function(arg0, arg1) { + console.log(getStringFromWasm(arg0, arg1)); + }; imports.wbg.__wbindgen_object_clone_ref = function(arg0) { const ret = getObject(arg0); return addHeapObject(ret); @@ -248,13 +255,6 @@ function init(module) { const ret = JSON.parse(getStringFromWasm(arg0, arg1)); return addHeapObject(ret); }; - imports.wbg.__wbg_log_93d35dab6e237612 = function(arg0, arg1) { - console.log(getStringFromWasm(arg0, arg1)); - }; - imports.wbg.__wbindgen_string_new = function(arg0, arg1) { - const ret = getStringFromWasm(arg0, arg1); - return addHeapObject(ret); - }; imports.wbg.__wbg_new_59cb74e423758ede = function() { const ret = new Error(); return addHeapObject(ret); @@ -864,16 +864,16 @@ function init(module) { imports.wbg.__wbindgen_throw = function(arg0, arg1) { throw new Error(getStringFromWasm(arg0, arg1)); }; - imports.wbg.__wbindgen_closure_wrapper208 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper218 = 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_binding6(a, state.b, arg0); + return __wbg_elem_binding2(a, state.b, arg0); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(41)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; } } @@ -882,16 +882,16 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper214 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper501 = 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_binding4(a, state.b, arg0); + return __wbg_elem_binding6(a, state.b, ); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(41)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(173)(a, state.b); else state.a = a; } } @@ -900,16 +900,16 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper497 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper903 = 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_binding5(a, state.b, ); + return __wbg_elem_binding5(a, state.b, arg0); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(171)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(243)(a, state.b); else state.a = a; } } @@ -918,16 +918,16 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper210 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper212 = 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_binding1(a, state.b, arg0); + return __wbg_elem_binding0(a, state.b, arg0); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(41)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; } } @@ -936,16 +936,16 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper899 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper220 = 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_binding2(a, state.b, arg0); + return __wbg_elem_binding1(a, state.b, arg0); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(241)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; } } @@ -954,7 +954,7 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper206 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper214 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; const real = (arg0) => { state.cnt++; @@ -963,7 +963,7 @@ function init(module) { try { return __wbg_elem_binding3(a, state.b, arg0); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(41)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; } } @@ -972,16 +972,16 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper212 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper216 = 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_binding4(a, state.b, arg0); } finally { - if (--state.cnt === 0) wasm.__wbg_function_table.get(41)(a, state.b); + if (--state.cnt === 0) wasm.__wbg_function_table.get(50)(a, state.b); else state.a = a; } } diff --git a/teleterm/static/teleterm_web_bg.wasm b/teleterm/static/teleterm_web_bg.wasm index 7111b85..ec5717b 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