5
5
use crate :: {
6
6
image:: Image ,
7
7
ipc:: {
8
- channel:: ChannelDataIpcQueue , CommandArg , CommandItem , Invoke , InvokeError , InvokeHandler ,
8
+ channel:: ChannelDataIpcQueue , CallbackFn , CommandArg , CommandItem , Invoke , InvokeError ,
9
+ InvokeHandler , InvokeResponseBody ,
9
10
} ,
10
11
manager:: { webview:: UriSchemeProtocol , AppManager , Asset } ,
11
12
plugin:: { Plugin , PluginStore } ,
@@ -15,8 +16,7 @@ use crate::{
15
16
ExitRequestedEventAction , RunEvent as RuntimeRunEvent ,
16
17
} ,
17
18
sealed:: { ManagerBase , RuntimeOrDispatch } ,
18
- utils:: config:: Config ,
19
- utils:: Env ,
19
+ utils:: { config:: Config , Env } ,
20
20
webview:: PageLoadPayload ,
21
21
Context , DeviceEventFilter , Emitter , EventLoopMessage , Listener , Manager , Monitor , Result ,
22
22
Runtime , Scopes , StateManager , Theme , Webview , WebviewWindowBuilder , Window ,
@@ -66,6 +66,8 @@ pub type SetupHook<R> =
66
66
Box < dyn FnOnce ( & mut App < R > ) -> std:: result:: Result < ( ) , Box < dyn std:: error:: Error > > + Send > ;
67
67
/// A closure that is run every time a page starts or finishes loading.
68
68
pub 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 > ;
69
71
70
72
/// The exit code on [`RunEvent::ExitRequested`] when [`AppHandle#method.restart`] is called.
71
73
pub const RESTART_EXIT_CODE : i32 = i32:: MAX ;
@@ -1205,6 +1207,8 @@ pub struct Builder<R: Runtime> {
1205
1207
/// The script that initializes the `window.__TAURI_INTERNALS__.postMessage` function.
1206
1208
pub ( crate ) invoke_initialization_script : String ,
1207
1209
1210
+ channel_interceptor : Option < ChannelInterceptor < R > > ,
1211
+
1208
1212
/// The setup hook.
1209
1213
setup : SetupHook < R > ,
1210
1214
@@ -1291,6 +1295,7 @@ impl<R: Runtime> Builder<R> {
1291
1295
. render_default ( & Default :: default ( ) )
1292
1296
. unwrap ( )
1293
1297
. into_string ( ) ,
1298
+ channel_interceptor : None ,
1294
1299
on_page_load : None ,
1295
1300
plugins : PluginStore :: default ( ) ,
1296
1301
uri_scheme_protocols : Default :: default ( ) ,
@@ -1370,6 +1375,22 @@ impl<R: Runtime> Builder<R> {
1370
1375
self
1371
1376
}
1372
1377
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
+
1373
1394
/// Append a custom initialization script.
1374
1395
///
1375
1396
/// Allow to append custom initialization script instend of replacing entire invoke system.
@@ -1856,6 +1877,7 @@ tauri::Builder::default()
1856
1877
#[ cfg( desktop) ]
1857
1878
HashMap :: new ( ) ,
1858
1879
self . invoke_initialization_script ,
1880
+ self . channel_interceptor ,
1859
1881
self . invoke_key ,
1860
1882
) ) ;
1861
1883
0 commit comments