-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: `use_interval` * update readme * clippy * clean up
- Loading branch information
Showing
10 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "interval" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
dioxus-sdk = { workspace = true, features = ["interval"] } | ||
dioxus = { workspace = true, features = ["web"] } | ||
|
||
log = "0.4.6" | ||
|
||
# WebAssembly Debug | ||
wasm-logger = "0.2.0" | ||
console_error_panic_hook = "0.1.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[application] | ||
|
||
# App (Project) Name | ||
name = "interval" | ||
|
||
# Dioxus App Default Platform | ||
# desktop, web, mobile, ssr | ||
default_platform = "web" | ||
|
||
# `build` & `serve` dist path | ||
out_dir = "dist" | ||
|
||
# resource (public) file folder | ||
asset_dir = "public" | ||
|
||
[web.app] | ||
|
||
# HTML title tag content | ||
title = "dioxus | ⛺" | ||
|
||
[web.watcher] | ||
|
||
# when watcher trigger, regenerate the `index.html` | ||
reload_html = true | ||
|
||
# which files or dirs will be watcher monitoring | ||
watch_path = ["src", "public"] | ||
|
||
# include `assets` in web platform | ||
[web.resource] | ||
|
||
# CSS style file | ||
style = [] | ||
|
||
# Javascript code file | ||
script = [] | ||
|
||
[web.resource.dev] | ||
|
||
# Javascript code file | ||
# serve: [dev-server] only | ||
script = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# use_interval | ||
|
||
Learn how to use `use_interval`. | ||
|
||
Run: | ||
|
||
```dioxus serve``` |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use dioxus::prelude::*; | ||
use dioxus_sdk::utils::interval::use_interval; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
// init debug tool for WebAssembly | ||
wasm_logger::init(wasm_logger::Config::default()); | ||
console_error_panic_hook::set_once(); | ||
|
||
launch(app); | ||
} | ||
|
||
fn app() -> Element { | ||
let mut count = use_signal(|| 0); | ||
|
||
use_interval(Duration::from_millis(100), move || { | ||
count += 1; | ||
}); | ||
|
||
rsx!( p { "{count}" } ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::time::Duration; | ||
|
||
use dioxus::prelude::{use_hook, Writable}; | ||
|
||
#[derive(Clone, PartialEq, Copy)] | ||
pub struct UseInterval { | ||
inner: dioxus::prelude::Signal<InnerUseInterval>, | ||
} | ||
|
||
struct InnerUseInterval { | ||
#[cfg(target_family = "wasm")] | ||
pub(crate) interval: Option<gloo_timers::callback::Interval>, | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
pub(crate) interval: Option<dioxus::prelude::Task>, | ||
} | ||
|
||
#[cfg(target_family = "wasm")] | ||
impl Drop for InnerUseInterval { | ||
fn drop(&mut self) { | ||
if let Some(interval) = self.interval.take() { | ||
interval.cancel(); | ||
} | ||
} | ||
} | ||
|
||
impl UseInterval { | ||
/// Cancel the interval | ||
pub fn cancel(&mut self) { | ||
if let Some(interval) = self.inner.write().interval.take() { | ||
interval.cancel(); | ||
} | ||
} | ||
} | ||
|
||
/// Repeatedly calls a function every a certain period. | ||
pub fn use_interval(period: Duration, action: impl FnMut() + 'static) -> UseInterval { | ||
let inner = use_hook(|| { | ||
let mut action = Box::new(action); | ||
|
||
#[cfg(target_family = "wasm")] | ||
return dioxus::prelude::Signal::new(InnerUseInterval { | ||
interval: Some(gloo_timers::callback::Interval::new( | ||
period.as_millis() as u32, | ||
move || { | ||
action(); | ||
}, | ||
)), | ||
}); | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
dioxus::prelude::Signal::new(InnerUseInterval { | ||
interval: Some(dioxus::prelude::spawn(async move { | ||
let mut interval = tokio::time::interval(period); | ||
loop { | ||
interval.tick().await; | ||
action(); | ||
} | ||
})), | ||
}) | ||
}); | ||
|
||
UseInterval { inner } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,9 @@ cfg_if::cfg_if! { | |
pub mod window; | ||
} | ||
} | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "interval")] { | ||
pub mod interval; | ||
} | ||
} |