Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Rust executor's async runtime for customization #2151

Merged
merged 10 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::api::media_element::MyMediaElement;
use crate::frb_generated::FLUTTER_RUST_BRIDGE_HANDLER;
use extend::ext;
use flutter_rust_bridge::for_generated::anyhow;
use flutter_rust_bridge::{frb, DartFnFuture};
use flutter_rust_bridge::{frb, BaseAsyncRuntime, DartFnFuture};
use std::sync::Arc;
use web_audio_api::context::{AudioContext, BaseAudioContext, OfflineAudioContext};
use web_audio_api::media_streams::{MediaStream, MediaStreamTrack};
Expand Down Expand Up @@ -38,7 +39,9 @@ pub impl AudioContext {
let callback = Arc::new(callback);
self.set_onstatechange(move |event| {
let callback_cloned = callback.clone();
flutter_rust_bridge::spawn(async move { callback_cloned(event).await });
FLUTTER_RUST_BRIDGE_HANDLER
.async_runtime()
.spawn(async move { callback_cloned(event).await });
})
}
}
Expand All @@ -50,7 +53,9 @@ pub impl OfflineAudioContext {
callback: impl Fn(OfflineAudioCompletionEvent) -> DartFnFuture<()> + Send + Sync + 'static,
) {
self.set_oncomplete(move |event| {
flutter_rust_bridge::spawn(async move { callback(event).await });
FLUTTER_RUST_BRIDGE_HANDLER
.async_runtime()
.spawn(async move { callback(event).await });
})
}
}
Expand All @@ -70,7 +75,9 @@ macro_rules! handle_audio_node_trait_impls_override {
callback: impl Fn(String) -> DartFnFuture<()> + Send + 'static,
) {
self.set_onprocessorerror(Box::new(|event| {
flutter_rust_bridge::spawn(async move { callback(event.message).await });
FLUTTER_RUST_BRIDGE_HANDLER
.async_runtime()
.spawn(async move { callback(event.message).await });
}))
}
}
Expand Down Expand Up @@ -111,7 +118,7 @@ macro_rules! handle_audio_scheduled_source_node_trait_impls_override {
callback: impl Fn(Event) -> DartFnFuture<()> + Send + 'static,
) {
self.set_onended(Box::new(|event| {
flutter_rust_bridge::spawn(async move { callback(event).await });
FLUTTER_RUST_BRIDGE_HANDLER.async_runtime().spawn(async move { callback(event).await });
}))
}
}
Expand Down
4 changes: 4 additions & 0 deletions frb_rust/src/handler/implementation/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl<EL: ErrorListener, TP: BaseThreadPool, AR: BaseAsyncRuntime> SimpleExecutor
pub fn thread_pool(&self) -> &TP {
&self.thread_pool
}

pub fn async_runtime(&self) -> &AR {
&self.async_runtime
}
}

impl<EL: ErrorListener + Sync, TP: BaseThreadPool, AR: BaseAsyncRuntime> Executor
Expand Down
4 changes: 4 additions & 0 deletions frb_rust/src/handler/implementation/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl<TP: BaseThreadPool> DefaultHandler<TP> {
pub fn thread_pool(&self) -> &TP {
self.executor.thread_pool()
}

pub fn async_runtime(&self) -> &SimpleAsyncRuntime {
self.executor.async_runtime()
}
}

/// The simple handler uses a simple thread pool to execute tasks.
Expand Down
2 changes: 1 addition & 1 deletion frb_rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub use crate::rust2dart::sender::Rust2DartSendError;
#[cfg(all(feature = "rust-async", feature = "thread-pool"))]
pub use crate::rust_async::spawn_blocking_with;
#[cfg(feature = "rust-async")]
pub use crate::rust_async::{spawn, spawn_local, JoinHandle};
pub use crate::rust_async::{spawn, spawn_local, BaseAsyncRuntime, JoinHandle};
#[cfg(feature = "rust-async")]
pub use crate::rust_auto_opaque::RustAutoOpaqueNom;
#[allow(deprecated)]
Expand Down
Loading