diff --git a/CHANGELOG.md b/CHANGELOG.md index ab33695..cbe9c4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.13.4] - 2024-09-05 + +### Fix 🍕 + +- `use_websocket` now returns a signal for the websocket instance so the user can actually use it. Before it always + returned `None`. + ## [0.13.3] - 2024-09-02 ### Fix 🍕 diff --git a/Cargo.toml b/Cargo.toml index 449515f..cc4fd30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leptos-use" -version = "0.13.3" +version = "0.13.4" edition = "2021" authors = ["Marc-Stefan Cassola"] categories = ["gui", "web-programming"] diff --git a/src/use_websocket.rs b/src/use_websocket.rs index ea9ce38..25473fc 100644 --- a/src/use_websocket.rs +++ b/src/use_websocket.rs @@ -287,7 +287,7 @@ where let (ready_state, set_ready_state) = create_signal(ConnectionReadyState::Closed); let (message, set_message) = create_signal(None); - let ws_ref: StoredValue> = store_value(None); + let ws_signal = RwSignal::new(None::); let reconnect_timer_ref: StoredValue> = store_value(None); @@ -309,8 +309,8 @@ where if !manually_closed_ref.get_value() && !reconnect_limit.is_exceeded_by(reconnect_times_ref.get_value()) - && ws_ref - .get_value() + && ws_signal + .get_untracked() .map_or(false, |ws: WebSocket| ws.ready_state() != WebSocket::OPEN) { reconnect_timer_ref.set_value( @@ -339,7 +339,7 @@ where Some(Rc::new(move || { reconnect_timer_ref.set_value(None); - if let Some(web_socket) = ws_ref.get_value() { + if let Some(web_socket) = ws_signal.get_untracked() { let _ = web_socket.close(); } @@ -533,7 +533,7 @@ where onclose_closure.forget(); } - ws_ref.set_value(Some(web_socket)); + ws_signal.set(Some(web_socket)); })) }); } @@ -542,7 +542,7 @@ where let send_str = { Box::new(move |data: &str| { if ready_state.get_untracked() == ConnectionReadyState::Open { - if let Some(web_socket) = ws_ref.get_value() { + if let Some(web_socket) = ws_signal.get_untracked() { let _ = web_socket.send_with_str(data); } } @@ -552,7 +552,7 @@ where // Send bytes let send_bytes = move |data: &[u8]| { if ready_state.get_untracked() == ConnectionReadyState::Open { - if let Some(web_socket) = ws_ref.get_value() { + if let Some(web_socket) = ws_signal.get_untracked() { let _ = web_socket.send_with_u8_array(data); } } @@ -590,7 +590,7 @@ where move || { manually_closed_ref.set_value(true); - if let Some(web_socket) = ws_ref.get_value() { + if let Some(web_socket) = ws_signal.get_untracked() { let _ = web_socket.close(); } } @@ -612,7 +612,7 @@ where UseWebSocketReturn { ready_state: ready_state.into(), message: message.into(), - ws: ws_ref.get_value(), + ws: ws_signal.into(), open, close, send, @@ -717,7 +717,7 @@ where /// Latest message received from `WebSocket`. pub message: Signal>, /// The `WebSocket` instance. - pub ws: Option, + pub ws: Signal>, /// Opens the `WebSocket` connection pub open: OpenFn, /// Closes the `WebSocket` connection