Skip to content

Commit 3420aa5

Browse files
committed
refactor: IPC handler [TRI-019] (#9)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 6ac21b3 commit 3420aa5

9 files changed

Lines changed: 22 additions & 39 deletions

File tree

.changes/rpc-mod-refactor.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change:** Renamed the `rpc` module to `ipc`.

.changes/runtime-ipc.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": minor
3+
"tauri-runtime-wry": minor
4+
---
5+
6+
**Breaking change:** Renamed the `RPC` interface to `IPC`.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
//! Types and functions related to Remote Procedure Call(RPC).
5+
//! Types and functions related to Inter Procedure Call(IPC).
66
//!
77
//! This module includes utilities to send messages to the JS layer of the webview.
88
@@ -68,15 +68,15 @@ fn escape_json_parse(json: &RawValue) -> String {
6868
/// # Examples
6969
/// - With string literals:
7070
/// ```
71-
/// use tauri::api::rpc::format_callback;
71+
/// use tauri::api::ipc::format_callback;
7272
/// // callback with a string argument
7373
/// let cb = format_callback("callback-function-name", &"the string response").unwrap();
7474
/// assert!(cb.contains(r#"window["callback-function-name"]("the string response")"#));
7575
/// ```
7676
///
7777
/// - With types implement [`serde::Serialize`]:
7878
/// ```
79-
/// use tauri::api::rpc::format_callback;
79+
/// use tauri::api::ipc::format_callback;
8080
/// use serde::Serialize;
8181
///
8282
/// // callback with large JSON argument
@@ -157,7 +157,7 @@ pub fn format_callback<T: Serialize, S: AsRef<str>>(
157157
///
158158
/// # Examples
159159
/// ```
160-
/// use tauri::api::rpc::format_callback_result;
160+
/// use tauri::api::ipc::format_callback_result;
161161
/// let res: Result<u8, &str> = Ok(5);
162162
/// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
163163
/// assert!(cb.contains(r#"window["success_cb"](5)"#));
@@ -180,7 +180,7 @@ pub fn format_callback_result<T: Serialize, E: Serialize>(
180180

181181
#[cfg(test)]
182182
mod test {
183-
use crate::api::rpc::*;
183+
use crate::api::ipc::*;
184184
use quickcheck_macros::quickcheck;
185185

186186
#[test]

core/tauri/src/api/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ pub mod dialog;
1010
pub mod dir;
1111
pub mod file;
1212
pub mod http;
13+
pub mod ipc;
1314
pub mod path;
1415
pub mod process;
15-
pub mod rpc;
1616
#[cfg(shell_open)]
1717
pub mod shell;
1818
pub mod version;

core/tauri/src/endpoints/global_shortcut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn register_shortcut<R: Runtime>(
3737
) -> crate::Result<()> {
3838
let accelerator = shortcut.clone();
3939
manager.register(&shortcut, move || {
40-
let callback_string = crate::api::rpc::format_callback(&handler, &accelerator)
40+
let callback_string = crate::api::ipc::format_callback(&handler, &accelerator)
4141
.expect("unable to serialize shortcut string to json");
4242
let _ = window.eval(callback_string.as_str());
4343
})?;

core/tauri/src/endpoints/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Cmd {
107107
if matches!(event, crate::api::process::CommandEvent::Terminated(_)) {
108108
command_childs().lock().unwrap().remove(&pid);
109109
}
110-
let js = crate::api::rpc::format_callback(on_event_fn.clone(), &event)
110+
let js = crate::api::ipc::format_callback(on_event_fn.clone(), &event)
111111
.expect("unable to serialize CommandEvent");
112112

113113
let _ = window.eval(js.as_str());

core/tauri/src/hooks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
use crate::{
6-
api::rpc::{format_callback, format_callback_result},
6+
api::ipc::{format_callback, format_callback_result},
77
app::App,
88
runtime::Runtime,
99
StateManager, Window,
@@ -228,7 +228,7 @@ pub struct InvokeMessage<R: Runtime> {
228228
pub(crate) window: Window<R>,
229229
/// Application managed state.
230230
pub(crate) state: Arc<StateManager>,
231-
/// The RPC command.
231+
/// The IPC command.
232232
pub(crate) command: String,
233233
/// The JSON argument passed on the invoke message.
234234
pub(crate) payload: JsonValue,

core/tauri/src/manager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ impl<R: Runtime> WindowManager<R> {
467467
let manager = self.clone();
468468
Box::new(move |window, request| {
469469
let window = Window::new(manager.clone(), window, app_handle.clone());
470+
470471
match serde_json::from_str::<InvokePayload>(&request) {
471472
Ok(message) => {
472473
let _ = window.on_message(message);
Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +0,0 @@
1-
/*! *****************************************************************************
2-
Copyright (c) Microsoft Corporation.
3-
4-
Permission to use, copy, modify, and/or distribute this software for any
5-
purpose with or without fee is hereby granted.
6-
7-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13-
PERFORMANCE OF THIS SOFTWARE.
14-
***************************************************************************** */
15-
function n(n,e,t,r){return new(t||(t=Promise))((function(o,i){function a(n){try{c(r.next(n))}catch(n){i(n)}}function u(n){try{c(r.throw(n))}catch(n){i(n)}}function c(n){var e;n.done?o(n.value):(e=n.value,e instanceof t?e:new t((function(n){n(e)}))).then(a,u)}c((r=r.apply(n,e||[])).next())}))}function e(n,e){var t,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(t)throw new TypeError("Generator is already executing.");for(;a;)try{if(t=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(n,a)}catch(n){i=[6,n],r=0}finally{t=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}
16-
/*! *****************************************************************************
17-
Copyright (c) Microsoft Corporation.
18-
19-
Permission to use, copy, modify, and/or distribute this software for any
20-
purpose with or without fee is hereby granted.
21-
22-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
23-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
25-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
26-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
27-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28-
PERFORMANCE OF THIS SOFTWARE.
29-
***************************************************************************** */var t=function(){return(t=Object.assign||function(n){for(var e,t=1,r=arguments.length;t<r;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o]);return n}).apply(this,arguments)};function r(n,e){void 0===e&&(e=!1);var t=function(){var n=new Int8Array(1);window.crypto.getRandomValues(n);var e=new Uint8Array(Math.max(16,Math.abs(n[0])));return window.crypto.getRandomValues(e),e.join("")}();return Object.defineProperty(window,t,{value:function(r){return e&&Reflect.deleteProperty(window,t),null==n?void 0:n(r)},writable:!1,configurable:!0}),t}function o(n,e){return void 0===e&&(e={}),function(n,e,t,r){return new(t||(t=Promise))((function(o,i){function a(n){try{c(r.next(n))}catch(n){i(n)}}function u(n){try{c(r.throw(n))}catch(n){i(n)}}function c(n){var e;n.done?o(n.value):(e=n.value,e instanceof t?e:new t((function(n){n(e)}))).then(a,u)}c((r=r.apply(n,e||[])).next())}))}(this,void 0,void 0,(function(){return function(n,e){var t,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(t)throw new TypeError("Generator is already executing.");for(;a;)try{if(t=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(n,a)}catch(n){i=[6,n],r=0}finally{t=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}(this,(function(o){return[2,new Promise((function(o,i){var a=r((function(n){o(n),Reflect.deleteProperty(window,u)}),!0),u=r((function(n){i(n),Reflect.deleteProperty(window,a)}),!0);window.rpc.notify(n,t({__invokeKey:__TAURI_INVOKE_KEY__,callback:a,error:u},e))}))]}))}))}function i(){return n(this,void 0,void 0,(function(){return e(this,(function(n){switch(n.label){case 0:return[4,o("plugin:{{ plugin_name }}|execute")];case 1:return n.sent(),[2]}}))}))}Object.freeze({__proto__:null,transformCallback:r,invoke:o,convertFileSrc:function(n){return navigator.userAgent.includes("Windows")?"https://asset.localhost/"+n:"asset://"+n}});export{i as execute};

0 commit comments

Comments
 (0)