Skip to content

Commit d3fdeb4

Browse files
authored
feat(core): expose async_runtime module (#1576)
1 parent 508eddc commit d3fdeb4

File tree

5 files changed

+46
-31
lines changed

5 files changed

+46
-31
lines changed

.changes/expose-async-runtime.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Expose `async_runtime` module.

core/tauri/src/api/command.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::os::windows::process::CommandExt;
1616
#[cfg(windows)]
1717
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
1818

19-
use crate::api::private::async_runtime::{channel, spawn, Receiver, RwLock};
19+
use crate::async_runtime::{channel, spawn, Receiver, RwLock};
2020
use os_pipe::{pipe, PipeWriter};
2121
use serde::Serialize;
2222
use shared_child::SharedChild;
@@ -203,7 +203,7 @@ mod test {
203203
let cmd = Command::new("cat").args(&["test/api/test.txt"]);
204204
let (mut rx, _) = cmd.spawn().unwrap();
205205

206-
crate::api::private::async_runtime::block_on(async move {
206+
crate::async_runtime::block_on(async move {
207207
while let Some(event) = rx.recv().await {
208208
match event {
209209
CommandEvent::Terminated(payload) => {
@@ -225,7 +225,7 @@ mod test {
225225
let cmd = Command::new("cat").args(&["test/api/"]);
226226
let (mut rx, _) = cmd.spawn().unwrap();
227227

228-
crate::api::private::async_runtime::block_on(async move {
228+
crate::async_runtime::block_on(async move {
229229
while let Some(event) = rx.recv().await {
230230
match event {
231231
CommandEvent::Terminated(payload) => {

core/tauri/src/api/mod.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,6 @@ pub struct PackageInfo {
6868
// Not public API
6969
#[doc(hidden)]
7070
pub mod private {
71-
// Core API only.
72-
pub mod async_runtime {
73-
use once_cell::sync::OnceCell;
74-
use tokio::runtime::Runtime;
75-
pub use tokio::sync::{
76-
mpsc::{channel, Receiver, Sender},
77-
Mutex, RwLock,
78-
};
79-
80-
use std::future::Future;
81-
82-
static RUNTIME: OnceCell<Runtime> = OnceCell::new();
83-
84-
pub fn block_on<F: Future>(task: F) -> F::Output {
85-
let runtime = RUNTIME.get_or_init(|| Runtime::new().unwrap());
86-
runtime.block_on(task)
87-
}
88-
89-
pub fn spawn<F>(task: F)
90-
where
91-
F: Future + Send + 'static,
92-
F::Output: Send + 'static,
93-
{
94-
let runtime = RUNTIME.get_or_init(|| Runtime::new().unwrap());
95-
runtime.spawn(task);
96-
}
97-
}
9871
pub use once_cell::sync::OnceCell;
9972

10073
pub trait AsTauriContext {

core/tauri/src/async_runtime.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
//! The singleton async runtime used by Tauri and exposed to consumers.
6+
//! Wraps a `tokio` Runtime and is meant to be used by initialization code, such as plugins `initialization` and app `setup` hooks.
7+
//! Fox more complex use cases, consider creating your own runtime.
8+
//! For command handlers, it's recommended to use a plain `async fn` command.
9+
10+
use once_cell::sync::OnceCell;
11+
use tokio::runtime::Runtime;
12+
pub use tokio::sync::{
13+
mpsc::{channel, Receiver, Sender},
14+
Mutex, RwLock,
15+
};
16+
17+
use std::future::Future;
18+
19+
static RUNTIME: OnceCell<Runtime> = OnceCell::new();
20+
21+
/// Run a future to completion on runtime.
22+
pub fn block_on<F: Future>(task: F) -> F::Output {
23+
let runtime = RUNTIME.get_or_init(|| Runtime::new().unwrap());
24+
runtime.block_on(task)
25+
}
26+
27+
/// Spawn a future onto the runtime.
28+
pub fn spawn<F>(task: F)
29+
where
30+
F: Future + Send + 'static,
31+
F::Output: Send + 'static,
32+
{
33+
let runtime = RUNTIME.get_or_init(|| Runtime::new().unwrap());
34+
runtime.spawn(task);
35+
}

core/tauri/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
//! Tauri uses (and contributes to) the MIT licensed project that you can find at [webview](https://github.com/webview/webview).
1111
#![warn(missing_docs, rust_2018_idioms)]
1212

13-
pub(crate) use crate::api::private::async_runtime;
1413
/// The Tauri error enum.
1514
pub use error::Error;
1615
pub use tauri_macros::{command, generate_handler};
1716

17+
/// Core API.
1818
pub mod api;
19+
/// Async runtime.
20+
pub mod async_runtime;
1921
/// The Tauri API endpoints.
2022
mod endpoints;
2123
mod error;

0 commit comments

Comments
 (0)