mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 00:59:22 -05:00
fixed use_timeout_fn to be SSR safe
This commit is contained in:
parent
f0d60e5d49
commit
da56771900
3 changed files with 76 additions and 48 deletions
|
@ -3,6 +3,12 @@
|
|||
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.11.3] - 2024-07-31
|
||||
|
||||
### Fix 🍕
|
||||
|
||||
- Made `use_timeout_fn` SSR-safe
|
||||
|
||||
## [0.11.2] - 2024-07-30
|
||||
|
||||
### Change 🔥
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "leptos-use"
|
||||
version = "0.11.2"
|
||||
version = "0.11.3"
|
||||
edition = "2021"
|
||||
authors = ["Marc-Stefan Cassola"]
|
||||
categories = ["gui", "web-programming"]
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
use leptos::leptos_dom::helpers::TimeoutHandle;
|
||||
use leptos::*;
|
||||
use std::cell::Cell;
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Wrapper for `setTimeout` with controls.
|
||||
///
|
||||
|
@ -31,6 +27,11 @@ use std::time::Duration;
|
|||
/// # view! { }
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// ## Server-Side Rendering
|
||||
///
|
||||
/// On the server the callback will never be run. The returned functions are all no-ops and
|
||||
/// `is_pending` will always be `false`.
|
||||
pub fn use_timeout_fn<CbFn, Arg, D>(
|
||||
callback: CbFn,
|
||||
delay: D,
|
||||
|
@ -40,10 +41,20 @@ where
|
|||
Arg: 'static,
|
||||
D: Into<MaybeSignal<f64>>,
|
||||
{
|
||||
let delay = delay.into();
|
||||
|
||||
let (is_pending, set_pending) = create_signal(false);
|
||||
|
||||
let start;
|
||||
let stop;
|
||||
|
||||
#[cfg(not(feature = "ssr"))]
|
||||
{
|
||||
use leptos::leptos_dom::helpers::TimeoutHandle;
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
|
||||
let delay = delay.into();
|
||||
|
||||
let timer = Rc::new(Cell::new(None::<TimeoutHandle>));
|
||||
|
||||
let clear = {
|
||||
|
@ -56,7 +67,7 @@ where
|
|||
}
|
||||
};
|
||||
|
||||
let stop = {
|
||||
stop = {
|
||||
let clear = clear.clone();
|
||||
|
||||
move || {
|
||||
|
@ -65,7 +76,7 @@ where
|
|||
}
|
||||
};
|
||||
|
||||
let start = {
|
||||
start = {
|
||||
let timer = Rc::clone(&timer);
|
||||
let callback = callback.clone();
|
||||
|
||||
|
@ -99,6 +110,17 @@ where
|
|||
};
|
||||
|
||||
on_cleanup(clear);
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
{
|
||||
let _ = set_pending;
|
||||
let _ = callback;
|
||||
let _ = delay;
|
||||
|
||||
start = move |_: Arg| ();
|
||||
stop = move || ();
|
||||
}
|
||||
|
||||
UseTimeoutFnReturn {
|
||||
is_pending: is_pending.into(),
|
||||
|
|
Loading…
Add table
Reference in a new issue