mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-11-24 12:14:05 +03:00
refactor: IPC handler [TRI-019] (#9)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
parent
6ac21b3cef
commit
3420aa5031
5
.changes/rpc-mod-refactor.md
Normal file
5
.changes/rpc-mod-refactor.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
**Breaking change:** Renamed the `rpc` module to `ipc`.
|
6
.changes/runtime-ipc.md
Normal file
6
.changes/runtime-ipc.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"tauri-runtime": minor
|
||||
"tauri-runtime-wry": minor
|
||||
---
|
||||
|
||||
**Breaking change:** Renamed the `RPC` interface to `IPC`.
|
@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//! Types and functions related to Remote Procedure Call(RPC).
|
||||
//! Types and functions related to Inter Procedure Call(IPC).
|
||||
//!
|
||||
//! This module includes utilities to send messages to the JS layer of the webview.
|
||||
|
||||
@ -68,7 +68,7 @@ fn escape_json_parse(json: &RawValue) -> String {
|
||||
/// # Examples
|
||||
/// - With string literals:
|
||||
/// ```
|
||||
/// use tauri::api::rpc::format_callback;
|
||||
/// use tauri::api::ipc::format_callback;
|
||||
/// // callback with a string argument
|
||||
/// let cb = format_callback("callback-function-name", &"the string response").unwrap();
|
||||
/// assert!(cb.contains(r#"window["callback-function-name"]("the string response")"#));
|
||||
@ -76,7 +76,7 @@ fn escape_json_parse(json: &RawValue) -> String {
|
||||
///
|
||||
/// - With types implement [`serde::Serialize`]:
|
||||
/// ```
|
||||
/// use tauri::api::rpc::format_callback;
|
||||
/// use tauri::api::ipc::format_callback;
|
||||
/// use serde::Serialize;
|
||||
///
|
||||
/// // callback with large JSON argument
|
||||
@ -157,7 +157,7 @@ pub fn format_callback<T: Serialize, S: AsRef<str>>(
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use tauri::api::rpc::format_callback_result;
|
||||
/// use tauri::api::ipc::format_callback_result;
|
||||
/// let res: Result<u8, &str> = Ok(5);
|
||||
/// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
|
||||
/// assert!(cb.contains(r#"window["success_cb"](5)"#));
|
||||
@ -180,7 +180,7 @@ pub fn format_callback_result<T: Serialize, E: Serialize>(
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::api::rpc::*;
|
||||
use crate::api::ipc::*;
|
||||
use quickcheck_macros::quickcheck;
|
||||
|
||||
#[test]
|
@ -10,9 +10,9 @@ pub mod dialog;
|
||||
pub mod dir;
|
||||
pub mod file;
|
||||
pub mod http;
|
||||
pub mod ipc;
|
||||
pub mod path;
|
||||
pub mod process;
|
||||
pub mod rpc;
|
||||
#[cfg(shell_open)]
|
||||
pub mod shell;
|
||||
pub mod version;
|
||||
|
@ -37,7 +37,7 @@ fn register_shortcut<R: Runtime>(
|
||||
) -> crate::Result<()> {
|
||||
let accelerator = shortcut.clone();
|
||||
manager.register(&shortcut, move || {
|
||||
let callback_string = crate::api::rpc::format_callback(&handler, &accelerator)
|
||||
let callback_string = crate::api::ipc::format_callback(&handler, &accelerator)
|
||||
.expect("unable to serialize shortcut string to json");
|
||||
let _ = window.eval(callback_string.as_str());
|
||||
})?;
|
||||
|
@ -107,7 +107,7 @@ impl Cmd {
|
||||
if matches!(event, crate::api::process::CommandEvent::Terminated(_)) {
|
||||
command_childs().lock().unwrap().remove(&pid);
|
||||
}
|
||||
let js = crate::api::rpc::format_callback(on_event_fn.clone(), &event)
|
||||
let js = crate::api::ipc::format_callback(on_event_fn.clone(), &event)
|
||||
.expect("unable to serialize CommandEvent");
|
||||
|
||||
let _ = window.eval(js.as_str());
|
||||
|
@ -3,7 +3,7 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use crate::{
|
||||
api::rpc::{format_callback, format_callback_result},
|
||||
api::ipc::{format_callback, format_callback_result},
|
||||
app::App,
|
||||
runtime::Runtime,
|
||||
StateManager, Window,
|
||||
@ -228,7 +228,7 @@ pub struct InvokeMessage<R: Runtime> {
|
||||
pub(crate) window: Window<R>,
|
||||
/// Application managed state.
|
||||
pub(crate) state: Arc<StateManager>,
|
||||
/// The RPC command.
|
||||
/// The IPC command.
|
||||
pub(crate) command: String,
|
||||
/// The JSON argument passed on the invoke message.
|
||||
pub(crate) payload: JsonValue,
|
||||
|
@ -467,6 +467,7 @@ impl<R: Runtime> WindowManager<R> {
|
||||
let manager = self.clone();
|
||||
Box::new(move |window, request| {
|
||||
let window = Window::new(manager.clone(), window, app_handle.clone());
|
||||
|
||||
match serde_json::from_str::<InvokePayload>(&request) {
|
||||
Ok(message) => {
|
||||
let _ = window.on_message(message);
|
||||
|
@ -1,29 +0,0 @@
|
||||
/*! *****************************************************************************
|
||||
Copyright (c) Microsoft Corporation.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
***************************************************************************** */
|
||||
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])}}}
|
||||
/*! *****************************************************************************
|
||||
Copyright (c) Microsoft Corporation.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
***************************************************************************** */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};
|
Loading…
Reference in New Issue
Block a user