-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add own abstraction layer over tokio::time::Interval, using gloo-time…
…rs for WASM
- Loading branch information
Showing
13 changed files
with
124 additions
and
174 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,20 @@ | ||
[package] | ||
name = "nimiq-time" | ||
version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
description = "tokio::time for Rust and WASM" | ||
homepage.workspace = true | ||
repository.workspace = true | ||
categories.workspace = true | ||
keywords.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
gloo-timers = { version = "0.2", features = ["futures"] } | ||
send_wrapper = { version = "0.6", features = ["futures"] } | ||
tokio = { version = "1.36", features = ["time"] } | ||
tokio-stream = { version = "0.1", features = ["time"] } |
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,17 @@ | ||
use std::convert::TryInto; | ||
|
||
use gloo_timers::future::IntervalStream; | ||
use send_wrapper::SendWrapper; | ||
|
||
pub type Interval = SendWrapper<IntervalStream>; | ||
|
||
pub fn interval(duration: std::time::Duration) -> Interval { | ||
// gloo-timers does not have a way to create an interval that fires its first tick immediately | ||
interval_at(duration) | ||
} | ||
|
||
pub fn interval_at(duration: std::time::Duration) -> Interval { | ||
SendWrapper::new(IntervalStream::new( | ||
duration.as_millis().try_into().unwrap(), | ||
)) | ||
} |
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,9 @@ | ||
#[cfg(target_family = "wasm")] | ||
mod gloo; | ||
#[cfg(not(target_family = "wasm"))] | ||
mod tokio; | ||
|
||
#[cfg(target_family = "wasm")] | ||
pub use gloo::*; | ||
#[cfg(not(target_family = "wasm"))] | ||
pub use tokio::*; |
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,10 @@ | ||
use tokio::time::{interval as tokio_interval, interval_at as tokio_interval_at, Instant}; | ||
pub use tokio_stream::wrappers::IntervalStream as Interval; | ||
|
||
pub fn interval(duration: std::time::Duration) -> Interval { | ||
Interval::new(tokio_interval(duration)) | ||
} | ||
|
||
pub fn interval_at(duration: std::time::Duration) -> Interval { | ||
Interval::new(tokio_interval_at(Instant::now() + duration, duration)) | ||
} |
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