Skip to content

Commit

Permalink
Merge pull request #2151 from fzyzcjy/feat/12405
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy authored Jun 24, 2024
2 parents df698b2 + 519104a commit e7ffe16
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
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

0 comments on commit e7ffe16

Please sign in to comment.