mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 19:08:15 +00:00

Some checks are pending
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build wasm32 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
60 lines
1.5 KiB
Rust
60 lines
1.5 KiB
Rust
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
use deno_unsync::JoinHandle;
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub type JoinHandle<T> =
|
|
std::future::Ready<Result<T, std::convert::Infallible>>;
|
|
|
|
pub fn spawn_blocking<
|
|
F: (FnOnce() -> R) + Send + 'static,
|
|
R: Send + 'static,
|
|
>(
|
|
f: F,
|
|
) -> JoinHandle<R> {
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
let result = f();
|
|
std::future::ready(Ok(result))
|
|
}
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
deno_unsync::spawn_blocking(f)
|
|
}
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub use deno_unsync::sync::MultiRuntimeAsyncValueCreator;
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
mod wasm {
|
|
use futures::future::LocalBoxFuture;
|
|
|
|
type CreateFutureFn<TResult> =
|
|
Box<dyn Fn() -> LocalBoxFuture<'static, TResult> + Send + Sync>;
|
|
|
|
pub struct MultiRuntimeAsyncValueCreator<TResult: Send + Clone + 'static> {
|
|
create_future: CreateFutureFn<TResult>,
|
|
}
|
|
|
|
impl<TResult: Send + Clone + 'static> std::fmt::Debug
|
|
for MultiRuntimeAsyncValueCreator<TResult>
|
|
{
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_struct("MultiRuntimeAsyncValueCreator").finish()
|
|
}
|
|
}
|
|
|
|
impl<TResult: Send + Clone + 'static> MultiRuntimeAsyncValueCreator<TResult> {
|
|
pub fn new(create_future: CreateFutureFn<TResult>) -> Self {
|
|
Self { create_future }
|
|
}
|
|
|
|
pub async fn get(&self) -> TResult {
|
|
(self.create_future)().await
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub use wasm::MultiRuntimeAsyncValueCreator;
|