@@ -7,7 +7,9 @@ pub(crate) mod tray;
77
88use crate :: {
99 command:: { CommandArg , CommandItem } ,
10- hooks:: { InvokeHandler , OnPageLoad , PageLoadPayload , SetupHook } ,
10+ hooks:: {
11+ window_invoke_responder, InvokeHandler , InvokeResponder , OnPageLoad , PageLoadPayload , SetupHook ,
12+ } ,
1113 manager:: { Asset , CustomProtocol , WindowManager } ,
1214 plugin:: { Plugin , PluginStore } ,
1315 runtime:: {
@@ -19,7 +21,7 @@ use crate::{
1921 sealed:: { ManagerBase , RuntimeOrDispatch } ,
2022 utils:: assets:: Assets ,
2123 utils:: config:: { Config , WindowUrl } ,
22- Context , Invoke , InvokeError , Manager , StateManager , Window ,
24+ Context , Invoke , InvokeError , InvokeResponse , Manager , StateManager , Window ,
2325} ;
2426
2527use tauri_macros:: default_runtime;
@@ -582,6 +584,12 @@ pub struct Builder<R: Runtime> {
582584 /// The JS message handler.
583585 invoke_handler : Box < InvokeHandler < R > > ,
584586
587+ /// The JS message responder.
588+ invoke_responder : Arc < InvokeResponder < R > > ,
589+
590+ /// The script that initializes the `window.__TAURI_POST_MESSAGE__` function.
591+ invoke_initialization_script : String ,
592+
585593 /// The setup hook.
586594 setup : SetupHook < R > ,
587595
@@ -624,6 +632,9 @@ impl<R: Runtime> Builder<R> {
624632 Self {
625633 setup : Box :: new ( |_| Ok ( ( ) ) ) ,
626634 invoke_handler : Box :: new ( |_| ( ) ) ,
635+ invoke_responder : Arc :: new ( window_invoke_responder) ,
636+ invoke_initialization_script :
637+ "Object.defineProperty(window, '__TAURI_POST_MESSAGE__', { value: (cmd, args) => window.rpc.notify(cmd, args) })" . into ( ) ,
627638 on_page_load : Box :: new ( |_, _| ( ) ) ,
628639 pending_windows : Default :: default ( ) ,
629640 plugins : PluginStore :: default ( ) ,
@@ -648,6 +659,21 @@ impl<R: Runtime> Builder<R> {
648659 self
649660 }
650661
662+ /// Defines a custom JS message system.
663+ ///
664+ /// The `responder` is a function that will be called when a command has been executed and must send a response to the JS layer.
665+ ///
666+ /// The `initialization_script` is a script that initializes `window.__TAURI_POST_MESSAGE__`.
667+ /// That function must take the `command: string` and `args: object` types and send a message to the backend.
668+ pub fn invoke_system < F > ( mut self , initialization_script : String , responder : F ) -> Self
669+ where
670+ F : Fn ( Window < R > , InvokeResponse , String , String ) + Send + Sync + ' static ,
671+ {
672+ self . invoke_initialization_script = initialization_script;
673+ self . invoke_responder = Arc :: new ( responder) ;
674+ self
675+ }
676+
651677 /// Defines the setup hook.
652678 pub fn setup < F > ( mut self , setup : F ) -> Self
653679 where
@@ -905,6 +931,7 @@ impl<R: Runtime> Builder<R> {
905931 self . state ,
906932 self . window_event_listeners ,
907933 ( self . menu , self . menu_event_listeners ) ,
934+ ( self . invoke_responder , self . invoke_initialization_script ) ,
908935 ) ;
909936
910937 // set up all the windows defined in the config
0 commit comments