-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fc268d
commit 6fb325a
Showing
9 changed files
with
182 additions
and
127 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
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,88 @@ | ||
use std::future::Future; | ||
use web_time::Duration; | ||
use wgpu::WasmNotSend; | ||
|
||
/// Wrap a future to yield back to the browser macrotasks | ||
/// queue each time it yields (using a setTimeout). | ||
/// | ||
/// In the future this could work better with scheduler.yield. | ||
pub fn with_timeout_yield<F: Future<Output = T> + WasmNotSend + 'static, T: Send + 'static>( | ||
future: F, | ||
min_time_between_yields: Duration, | ||
) -> impl Future<Output = T> + WasmNotSend + 'static { | ||
#[cfg(not(target_family = "wasm"))] | ||
{ | ||
let _ = min_time_between_yields; | ||
future | ||
} | ||
|
||
#[cfg(target_family = "wasm")] | ||
{ | ||
use gloo_timers::future::TimeoutFuture; | ||
use std::{ | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
use web_time::Instant; | ||
|
||
struct WithTimeoutYield<F> { | ||
future: Pin<Box<F>>, | ||
timeout: Option<TimeoutFuture>, | ||
last_yield: Option<Instant>, | ||
min_duration: Duration, | ||
} | ||
|
||
impl<F: Future> Future for WithTimeoutYield<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.get_mut(); | ||
|
||
if let Some(timeout) = &mut this.timeout { | ||
match std::pin::pin!(timeout).poll(cx) { | ||
Poll::Ready(_) => { | ||
this.timeout = None; | ||
this.last_yield = Some(Instant::now()); | ||
} | ||
Poll::Pending => return Poll::Pending, | ||
} | ||
} | ||
|
||
match this.future.as_mut().poll(cx) { | ||
Poll::Ready(output) => Poll::Ready(output), | ||
Poll::Pending => { | ||
let should_yield = this | ||
.last_yield | ||
.map(|t| t.elapsed() >= this.min_duration) | ||
.unwrap_or(true); | ||
|
||
if should_yield && this.timeout.is_none() { | ||
this.timeout = Some(TimeoutFuture::new(0)); | ||
} | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
} | ||
|
||
WithTimeoutYield { | ||
future: Box::pin(future), | ||
timeout: None, | ||
last_yield: None, | ||
min_duration: min_time_between_yields, | ||
} | ||
} | ||
} | ||
|
||
pub fn spawn_future<T: WasmNotSend + 'static>( | ||
fut: impl Future<Output = T> + WasmNotSend + 'static, | ||
) { | ||
#[cfg(target_family = "wasm")] | ||
{ | ||
async_std::task::spawn_local(fut); | ||
} | ||
#[cfg(not(target_family = "wasm"))] | ||
{ | ||
async_std::task::spawn(fut); | ||
} | ||
} |
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 was deleted.
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
Oops, something went wrong.