feat(web-async): add portable time module - #38
Merged
Merged
Conversation
Add `web_async::time`, a thin cfg-switched re-export of the tokio::time surface (sleep, sleep_until, interval, interval_at, timeout, timeout_at, Sleep, Interval, Timeout, MissedTickBehavior, Instant) plus Duration. Native (and wasi) re-export tokio::time directly. Browser wasm routes through wasmtimer, which implements the same API on performance.now() + setTimeout. tokio's own clock is std::time::Instant::now(), which panics on wasm32 (no clock), and its timers need a runtime time driver that spawn_local doesn't provide, so tokio::time can't be used there directly. This is the sibling of the existing spawn split: code can use web_async::time in place of tokio::time and run unchanged on both targets. wasmtimer is a wasm-only dependency, so native builds are unaffected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Merged
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
web_async::timeso code can use timers andInstanton both native and the browser, the same wayweb_async::spawnalready abstracts task spawning.Why
tokio::timedoes not work onwasm32-unknown-unknown:std::time::Instant::now(), which panics on wasm (no clock).wasm_bindgen_futures::spawn_local(whatweb_async::spawnuses on wasm) does not provide.So any crate that sleeps, ticks on an interval, or reads an
Instantcurrently can't compile-and-run on both targets. This came up makingmoq-netrun in the browser, but it's the same problem for anything in this ecosystem.What
time.rsis a thin cfg-switched re-export mirroringtokio::time1:1:tokio::time::{Instant, Sleep, Interval, Timeout, MissedTickBehavior, sleep, sleep_until, interval, interval_at, timeout, timeout_at}wasmtimer, which implements that API onperformance.now()+setTimeout, pluswasmtimer::std::Instant.Durationre-exported for convenience.Callers swap
tokio::timeforweb_async::timeand the same code runs on both.Notes:
wasmtimeris under[target.'cfg(target_arch = "wasm32")'.dependencies], so native builds never pull it. Native just gains tokio'stimefeature.spawn.rs.tokio::time::pause/advance) is intentionally not re-exported: it lives behind tokio'stest-utilfeature, which shouldn't leak into normal builds, and only runs in native tests.Test
cargo fmt --check,cargo clippy -- -D warningsclean (native).wasm32-unknown-unknown.🤖 Generated with Claude Code