55use crate :: {
66 image:: Image ,
77 ipc:: {
8- channel:: ChannelDataIpcQueue , CommandArg , CommandItem , Invoke , InvokeError , InvokeHandler ,
8+ channel:: ChannelDataIpcQueue , CallbackFn , CommandArg , CommandItem , Invoke , InvokeError ,
9+ InvokeHandler , InvokeResponseBody ,
910 } ,
1011 manager:: { webview:: UriSchemeProtocol , AppManager , Asset } ,
1112 plugin:: { Plugin , PluginStore } ,
@@ -15,8 +16,7 @@ use crate::{
1516 ExitRequestedEventAction , RunEvent as RuntimeRunEvent ,
1617 } ,
1718 sealed:: { ManagerBase , RuntimeOrDispatch } ,
18- utils:: config:: Config ,
19- utils:: Env ,
19+ utils:: { config:: Config , Env } ,
2020 webview:: PageLoadPayload ,
2121 Context , DeviceEventFilter , Emitter , EventLoopMessage , Listener , Manager , Monitor , Result ,
2222 Runtime , Scopes , StateManager , Theme , Webview , WebviewWindowBuilder , Window ,
@@ -66,6 +66,8 @@ pub type SetupHook<R> =
6666 Box < dyn FnOnce ( & mut App < R > ) -> std:: result:: Result < ( ) , Box < dyn std:: error:: Error > > + Send > ;
6767/// A closure that is run every time a page starts or finishes loading.
6868pub type OnPageLoad < R > = dyn Fn ( & Webview < R > , & PageLoadPayload < ' _ > ) + Send + Sync + ' static ;
69+ pub type ChannelInterceptor < R > =
70+ Box < dyn Fn ( & Webview < R > , CallbackFn , usize , & InvokeResponseBody ) -> bool + Send + Sync + ' static > ;
6971
7072/// The exit code on [`RunEvent::ExitRequested`] when [`AppHandle#method.restart`] is called.
7173pub const RESTART_EXIT_CODE : i32 = i32:: MAX ;
@@ -1205,6 +1207,8 @@ pub struct Builder<R: Runtime> {
12051207 /// The script that initializes the `window.__TAURI_INTERNALS__.postMessage` function.
12061208 pub ( crate ) invoke_initialization_script : String ,
12071209
1210+ channel_interceptor : Option < ChannelInterceptor < R > > ,
1211+
12081212 /// The setup hook.
12091213 setup : SetupHook < R > ,
12101214
@@ -1291,6 +1295,7 @@ impl<R: Runtime> Builder<R> {
12911295 . render_default ( & Default :: default ( ) )
12921296 . unwrap ( )
12931297 . into_string ( ) ,
1298+ channel_interceptor : None ,
12941299 on_page_load : None ,
12951300 plugins : PluginStore :: default ( ) ,
12961301 uri_scheme_protocols : Default :: default ( ) ,
@@ -1370,6 +1375,22 @@ impl<R: Runtime> Builder<R> {
13701375 self
13711376 }
13721377
1378+ /// Registers a channel interceptor that can overwrite the default channel implementation.
1379+ ///
1380+ /// If the event has been consumed, it must return `true`.
1381+ ///
1382+ /// The channel automatically orders the messages, so the third closure argument represents the message number.
1383+ /// The payload expected by the channel receiver is in the form of `{ id: usize, message: T }`.
1384+ pub fn channel_interceptor <
1385+ F : Fn ( & Webview < R > , CallbackFn , usize , & InvokeResponseBody ) -> bool + Send + Sync + ' static ,
1386+ > (
1387+ mut self ,
1388+ interceptor : F ,
1389+ ) -> Self {
1390+ self . channel_interceptor . replace ( Box :: new ( interceptor) ) ;
1391+ self
1392+ }
1393+
13731394 /// Append a custom initialization script.
13741395 ///
13751396 /// Allow to append custom initialization script instend of replacing entire invoke system.
@@ -1856,6 +1877,7 @@ tauri::Builder::default()
18561877 #[ cfg( desktop) ]
18571878 HashMap :: new ( ) ,
18581879 self . invoke_initialization_script ,
1880+ self . channel_interceptor ,
18591881 self . invoke_key ,
18601882 ) ) ;
18611883
0 commit comments