Merge remote-tracking branch 'origin/main' into use_webtransport

# Conflicts:
#	CHANGELOG.md
#	src/use_websocket.rs
This commit is contained in:
Maccesch 2023-08-12 23:21:47 +01:00
commit 3cd1728134
2 changed files with 19 additions and 6 deletions

View file

@ -40,6 +40,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `use_element_visibility` didn't work in some cases on Chrome properly. This has been fixed. - `use_element_visibility` didn't work in some cases on Chrome properly. This has been fixed.
## [0.6.3] - 2023-08-12
### Fixes 🍕
- `use_websocket` panicked after unmount
## [0.6.2] - 2023-08-03 ## [0.6.2] - 2023-08-03
### Fixes 🍕 ### Fixes 🍕

View file

@ -2,6 +2,7 @@
use cfg_if::cfg_if; use cfg_if::cfg_if;
use leptos::{leptos_dom::helpers::TimeoutHandle, *}; use leptos::{leptos_dom::helpers::TimeoutHandle, *};
use std::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration; use std::time::Duration;
@ -119,7 +120,8 @@ pub fn use_websocket_with_options(
let reconnect_timer_ref: StoredValue<Option<TimeoutHandle>> = store_value(None); let reconnect_timer_ref: StoredValue<Option<TimeoutHandle>> = store_value(None);
let reconnect_times_ref: StoredValue<u64> = store_value(0); let reconnect_times_ref: StoredValue<u64> = store_value(0);
let unmounted_ref = store_value(false);
let unmounted = Rc::new(Cell::new(false));
let connect_ref: StoredValue<Option<Rc<dyn Fn()>>> = store_value(None); let connect_ref: StoredValue<Option<Rc<dyn Fn()>>> = store_value(None);
@ -153,6 +155,7 @@ pub fn use_websocket_with_options(
connect_ref.set_value({ connect_ref.set_value({
let ws = ws_ref.get_value(); let ws = ws_ref.get_value();
let url = url; let url = url;
let unmounted = Rc::clone(&unmounted);
Some(Rc::new(move || { Some(Rc::new(move || {
reconnect_timer_ref.set_value(None); reconnect_timer_ref.set_value(None);
@ -179,10 +182,11 @@ pub fn use_websocket_with_options(
// onopen handler // onopen handler
{ {
let unmounted = Rc::clone(&unmounted);
let on_open = Rc::clone(&on_open); let on_open = Rc::clone(&on_open);
let onopen_closure = Closure::wrap(Box::new(move |e: Event| { let onopen_closure = Closure::wrap(Box::new(move |e: Event| {
if unmounted_ref.get_value() { if unmounted.get() {
return; return;
} }
@ -197,11 +201,12 @@ pub fn use_websocket_with_options(
// onmessage handler // onmessage handler
{ {
let unmounted = Rc::clone(&unmounted);
let on_message = Rc::clone(&on_message); let on_message = Rc::clone(&on_message);
let on_message_bytes = Rc::clone(&on_message_bytes); let on_message_bytes = Rc::clone(&on_message_bytes);
let onmessage_closure = Closure::wrap(Box::new(move |e: MessageEvent| { let onmessage_closure = Closure::wrap(Box::new(move |e: MessageEvent| {
if unmounted_ref.get_value() { if unmounted.get() {
return; return;
} }
@ -235,10 +240,11 @@ pub fn use_websocket_with_options(
// onerror handler // onerror handler
{ {
let unmounted = Rc::clone(&unmounted);
let on_error = Rc::clone(&on_error); let on_error = Rc::clone(&on_error);
let onerror_closure = Closure::wrap(Box::new(move |e: Event| { let onerror_closure = Closure::wrap(Box::new(move |e: Event| {
if unmounted_ref.get_value() { if unmounted.get() {
return; return;
} }
@ -256,10 +262,11 @@ pub fn use_websocket_with_options(
// onclose handler // onclose handler
{ {
let unmounted = Rc::clone(&unmounted);
let on_close = Rc::clone(&on_close); let on_close = Rc::clone(&on_close);
let onclose_closure = Closure::wrap(Box::new(move |e: CloseEvent| { let onclose_closure = Closure::wrap(Box::new(move |e: CloseEvent| {
if unmounted_ref.get_value() { if unmounted.get() {
return; return;
} }
@ -330,7 +337,7 @@ pub fn use_websocket_with_options(
// clean up (unmount) // clean up (unmount)
on_cleanup(move || { on_cleanup(move || {
unmounted_ref.set_value(true); unmounted.set(true);
close(); close();
}); });