-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): expose
async_runtime
module (#1576)
- Loading branch information
1 parent
508eddc
commit d3fdeb4
Showing
5 changed files
with
46 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"tauri": patch | ||
--- | ||
|
||
Expose `async_runtime` module. |
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,35 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
//! The singleton async runtime used by Tauri and exposed to consumers. | ||
//! Wraps a `tokio` Runtime and is meant to be used by initialization code, such as plugins `initialization` and app `setup` hooks. | ||
//! Fox more complex use cases, consider creating your own runtime. | ||
//! For command handlers, it's recommended to use a plain `async fn` command. | ||
|
||
use once_cell::sync::OnceCell; | ||
use tokio::runtime::Runtime; | ||
pub use tokio::sync::{ | ||
mpsc::{channel, Receiver, Sender}, | ||
Mutex, RwLock, | ||
}; | ||
|
||
use std::future::Future; | ||
|
||
static RUNTIME: OnceCell<Runtime> = OnceCell::new(); | ||
|
||
/// Run a future to completion on runtime. | ||
pub fn block_on<F: Future>(task: F) -> F::Output { | ||
let runtime = RUNTIME.get_or_init(|| Runtime::new().unwrap()); | ||
runtime.block_on(task) | ||
} | ||
|
||
/// Spawn a future onto the runtime. | ||
pub fn spawn<F>(task: F) | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
let runtime = RUNTIME.get_or_init(|| Runtime::new().unwrap()); | ||
runtime.spawn(task); | ||
} |
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