use_websocket now returns a signal for the websocket instance

This commit is contained in:
Maccesch 2024-09-05 00:54:08 +01:00
parent 1a189cf7a4
commit 82e7655d14
3 changed files with 18 additions and 11 deletions

View file

@ -3,6 +3,13 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 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). 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 ## [0.13.3] - 2024-09-02
### Fix 🍕 ### Fix 🍕

View file

@ -1,6 +1,6 @@
[package] [package]
name = "leptos-use" name = "leptos-use"
version = "0.13.3" version = "0.13.4"
edition = "2021" edition = "2021"
authors = ["Marc-Stefan Cassola"] authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"] categories = ["gui", "web-programming"]

View file

@ -287,7 +287,7 @@ where
let (ready_state, set_ready_state) = create_signal(ConnectionReadyState::Closed); let (ready_state, set_ready_state) = create_signal(ConnectionReadyState::Closed);
let (message, set_message) = create_signal(None); let (message, set_message) = create_signal(None);
let ws_ref: StoredValue<Option<WebSocket>> = store_value(None); let ws_signal = RwSignal::new(None::<WebSocket>);
let reconnect_timer_ref: StoredValue<Option<TimeoutHandle>> = store_value(None); let reconnect_timer_ref: StoredValue<Option<TimeoutHandle>> = store_value(None);
@ -309,8 +309,8 @@ where
if !manually_closed_ref.get_value() if !manually_closed_ref.get_value()
&& !reconnect_limit.is_exceeded_by(reconnect_times_ref.get_value()) && !reconnect_limit.is_exceeded_by(reconnect_times_ref.get_value())
&& ws_ref && ws_signal
.get_value() .get_untracked()
.map_or(false, |ws: WebSocket| ws.ready_state() != WebSocket::OPEN) .map_or(false, |ws: WebSocket| ws.ready_state() != WebSocket::OPEN)
{ {
reconnect_timer_ref.set_value( reconnect_timer_ref.set_value(
@ -339,7 +339,7 @@ where
Some(Rc::new(move || { Some(Rc::new(move || {
reconnect_timer_ref.set_value(None); 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(); let _ = web_socket.close();
} }
@ -533,7 +533,7 @@ where
onclose_closure.forget(); onclose_closure.forget();
} }
ws_ref.set_value(Some(web_socket)); ws_signal.set(Some(web_socket));
})) }))
}); });
} }
@ -542,7 +542,7 @@ where
let send_str = { let send_str = {
Box::new(move |data: &str| { Box::new(move |data: &str| {
if ready_state.get_untracked() == ConnectionReadyState::Open { 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); let _ = web_socket.send_with_str(data);
} }
} }
@ -552,7 +552,7 @@ where
// Send bytes // Send bytes
let send_bytes = move |data: &[u8]| { let send_bytes = move |data: &[u8]| {
if ready_state.get_untracked() == ConnectionReadyState::Open { 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); let _ = web_socket.send_with_u8_array(data);
} }
} }
@ -590,7 +590,7 @@ where
move || { move || {
manually_closed_ref.set_value(true); 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(); let _ = web_socket.close();
} }
} }
@ -612,7 +612,7 @@ where
UseWebSocketReturn { UseWebSocketReturn {
ready_state: ready_state.into(), ready_state: ready_state.into(),
message: message.into(), message: message.into(),
ws: ws_ref.get_value(), ws: ws_signal.into(),
open, open,
close, close,
send, send,
@ -717,7 +717,7 @@ where
/// Latest message received from `WebSocket`. /// Latest message received from `WebSocket`.
pub message: Signal<Option<Rx>>, pub message: Signal<Option<Rx>>,
/// The `WebSocket` instance. /// The `WebSocket` instance.
pub ws: Option<WebSocket>, pub ws: Signal<Option<WebSocket>>,
/// Opens the `WebSocket` connection /// Opens the `WebSocket` connection
pub open: OpenFn, pub open: OpenFn,
/// Closes the `WebSocket` connection /// Closes the `WebSocket` connection