From 00ced832b2f0632066c5f1b58af2e32ac6a9d45b Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Tue, 27 Aug 2024 17:54:21 +0300 Subject: [PATCH 1/3] Allow for lazy protocols with use_websocket --- src/use_websocket.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/use_websocket.rs b/src/use_websocket.rs index e7e420c..44a8b4d 100644 --- a/src/use_websocket.rs +++ b/src/use_websocket.rs @@ -344,17 +344,19 @@ where } let web_socket = { - protocols.as_ref().map_or_else( - || WebSocket::new(&url).unwrap_throw(), - |protocols| { - let array = protocols - .iter() - .map(|p| JsValue::from(p.clone())) - .collect::(); - WebSocket::new_with_str_sequence(&url, &JsValue::from(&array)) - .unwrap_throw() - }, - ) + protocols.with_untracked(|protocols| { + protocols.as_ref().map_or_else( + || WebSocket::new(&url).unwrap_throw(), + |protocols| { + let array = protocols + .iter() + .map(|p| JsValue::from(p.clone())) + .collect::(); + WebSocket::new_with_str_sequence(&url, &JsValue::from(&array)) + .unwrap_throw() + }, + ) + }) }; web_socket.set_binary_type(BinaryType::Arraybuffer); set_ready_state.set(ConnectionReadyState::Connecting); @@ -650,7 +652,8 @@ where /// Defaults to `true`. immediate: bool, /// Sub protocols. See [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket#protocols). - protocols: Option>, + #[builder(into)] + protocols: MaybeSignal>>, } impl UseWebSocketOptions { From b765dd579831f661660066bf517343f537269d00 Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Tue, 27 Aug 2024 19:19:47 +0300 Subject: [PATCH 2/3] Added web-sys to websocket feature --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a35148d..9b111e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -336,7 +336,7 @@ use_web_notification = [ "web-sys/NotificationDirection", "web-sys/VisibilityState" ] -use_websocket = ["dep:codee"] +use_websocket = ["dep:web-sys", "dep:codee"] use_window = ["use_document", "dep:web-sys", "web-sys/Navigator", "web-sys/MediaQueryList"] use_window_focus = ["use_event_listener"] use_window_scroll = ["use_event_listener", "use_window"] From b6d7ca340b5c3f8385aa279bdf40099a8e18fca6 Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Wed, 28 Aug 2024 10:09:03 +0300 Subject: [PATCH 3/3] Docs --- src/use_websocket.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/use_websocket.rs b/src/use_websocket.rs index 44a8b4d..ea9ce38 100644 --- a/src/use_websocket.rs +++ b/src/use_websocket.rs @@ -652,6 +652,11 @@ where /// Defaults to `true`. immediate: bool, /// Sub protocols. See [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket#protocols). + /// + /// Can be set as a signal to support protocols only available after the initial render. + /// + /// Note that protocols are only updated on the next websocket open() call, not whenever the signal is updated. + /// Therefore "lazy" protocols should use the `immediate(false)` option and manually call `open()`. #[builder(into)] protocols: MaybeSignal>>, }