mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-29 14:04:19 +03:00
fix(core): ipc and tests
This commit is contained in:
parent
766c4f2c57
commit
eae311e6e2
@ -1558,7 +1558,7 @@ impl PackageConfig {
|
||||
|
||||
/// The config type mapped to `tauri.conf.json`.
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
||||
#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
|
||||
#[cfg_attr(feature = "schema", derive(JsonSchema))]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
|
File diff suppressed because one or more lines are too long
@ -16,14 +16,19 @@
|
||||
once
|
||||
) {
|
||||
var identifier = uid()
|
||||
var prop = `_${identifier}`
|
||||
|
||||
window[identifier] = function (result) {
|
||||
if (once) {
|
||||
delete window[identifier]
|
||||
}
|
||||
Object.defineProperty(window, prop, {
|
||||
value: (result) => {
|
||||
if (once) {
|
||||
Reflect.deleteProperty(window, prop)
|
||||
}
|
||||
|
||||
return callback && callback(result)
|
||||
}
|
||||
return callback && callback(result)
|
||||
},
|
||||
writable: false,
|
||||
configurable: true
|
||||
})
|
||||
|
||||
return identifier
|
||||
}
|
||||
|
@ -72,15 +72,15 @@ fn escape_json_parse(json: &RawValue) -> String {
|
||||
/// # Examples
|
||||
/// - With string literals:
|
||||
/// ```
|
||||
/// use tauri::api::ipc::format_callback;
|
||||
/// use tauri::api::ipc::{CallbackFn, format_callback};
|
||||
/// // callback with a string argument
|
||||
/// let cb = format_callback(12345, &"the string response").unwrap();
|
||||
/// assert!(cb.contains(r#"window["12345"]("the string response")"#));
|
||||
/// let cb = format_callback(CallbackFn(12345), &"the string response").unwrap();
|
||||
/// assert!(cb.contains(r#"window["_12345"]("the string response")"#));
|
||||
/// ```
|
||||
///
|
||||
/// - With types implement [`serde::Serialize`]:
|
||||
/// ```
|
||||
/// use tauri::api::ipc::format_callback;
|
||||
/// use tauri::api::ipc::{CallbackFn, format_callback};
|
||||
/// use serde::Serialize;
|
||||
///
|
||||
/// // callback with large JSON argument
|
||||
@ -90,11 +90,11 @@ fn escape_json_parse(json: &RawValue) -> String {
|
||||
/// }
|
||||
///
|
||||
/// let cb = format_callback(
|
||||
/// 6789,
|
||||
/// CallbackFn(6789),
|
||||
/// &MyResponse { value: String::from_utf8(vec![b'X'; 10_240]).unwrap()
|
||||
/// }).expect("failed to serialize");
|
||||
///
|
||||
/// assert!(cb.contains(r#"window["6789"](JSON.parse('{"value":"XXXXXXXXX"#));
|
||||
/// assert!(cb.contains(r#"window["_6789"](JSON.parse('{"value":"XXXXXXXXX"#));
|
||||
/// ```
|
||||
pub fn format_callback<T: Serialize>(
|
||||
function_name: CallbackFn,
|
||||
@ -104,8 +104,8 @@ pub fn format_callback<T: Serialize>(
|
||||
( $arg:expr ) => {
|
||||
format!(
|
||||
r#"
|
||||
if (window["{fn}"]) {{
|
||||
window["{fn}"]({arg})
|
||||
if (window["_{fn}"]) {{
|
||||
window["_{fn}"]({arg})
|
||||
}} else {{
|
||||
console.warn("[TAURI] Couldn't find callback id {fn} in window. This happens when the app is reloaded while Rust is running an asynchronous operation.")
|
||||
}}
|
||||
@ -161,14 +161,14 @@ pub fn format_callback<T: Serialize>(
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use tauri::api::ipc::format_callback_result;
|
||||
/// use tauri::api::ipc::{CallbackFn, 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)"#));
|
||||
/// let cb = format_callback_result(res, CallbackFn(145), CallbackFn(0)).expect("failed to format");
|
||||
/// assert!(cb.contains(r#"window["_145"](5)"#));
|
||||
///
|
||||
/// let res: Result<&str, &str> = Err("error message here");
|
||||
/// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
|
||||
/// assert!(cb.contains(r#"window["error_cb"]("error message here")"#));
|
||||
/// let cb = format_callback_result(res, CallbackFn(2), CallbackFn(1)).expect("failed to format");
|
||||
/// assert!(cb.contains(r#"window["_1"]("error message here")"#));
|
||||
/// ```
|
||||
// TODO: better example to explain
|
||||
pub fn format_callback_result<T: Serialize, E: Serialize>(
|
||||
@ -220,11 +220,11 @@ mod test {
|
||||
// call format callback
|
||||
let fc = format_callback(f, &a).unwrap();
|
||||
fc.contains(&format!(
|
||||
r#"window["{}"](JSON.parse('{}'))"#,
|
||||
r#"window["_{}"](JSON.parse('{}'))"#,
|
||||
f.0,
|
||||
serde_json::Value::String(a.clone()),
|
||||
)) || fc.contains(&format!(
|
||||
r#"window["{}"]({})"#,
|
||||
r#"window["_{}"]({})"#,
|
||||
f.0,
|
||||
serde_json::Value::String(a),
|
||||
))
|
||||
@ -241,7 +241,7 @@ mod test {
|
||||
};
|
||||
|
||||
resp.contains(&format!(
|
||||
r#"window["{}"]({})"#,
|
||||
r#"window["_{}"]({})"#,
|
||||
function.0,
|
||||
serde_json::Value::String(value),
|
||||
))
|
||||
|
@ -119,7 +119,7 @@ pub fn listen_js<R: Runtime>(
|
||||
}}
|
||||
window['{listeners}']['{event}'].push({{
|
||||
id: {event_id},
|
||||
handler: window['{handler}']
|
||||
handler: window['_{handler}']
|
||||
}});
|
||||
",
|
||||
listeners = window.manager().event_listeners_object_name(),
|
||||
|
@ -12,8 +12,8 @@ use tauri_runtime::{
|
||||
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
|
||||
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
|
||||
},
|
||||
ClipboardManager, Dispatch, GlobalShortcutManager, Icon, Result, RunEvent, Runtime,
|
||||
RuntimeHandle, UserAttentionType,
|
||||
ActivationPolicy, ClipboardManager, Dispatch, GlobalShortcutManager, Icon, Result, RunEvent,
|
||||
RunIteration, Runtime, RuntimeHandle, UserAttentionType,
|
||||
};
|
||||
#[cfg(feature = "system-tray")]
|
||||
use tauri_runtime::{SystemTray, SystemTrayEvent};
|
||||
@ -554,7 +554,9 @@ impl Runtime for MockRuntime {
|
||||
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {}
|
||||
|
||||
#[cfg(any(target_os = "windows", target_os = "macos"))]
|
||||
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) {}
|
||||
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn run<F: FnMut(RunEvent) + 'static>(self, callback: F) {
|
||||
loop {
|
||||
|
2
core/tauri/tests/restart/Cargo.lock
generated
2
core/tauri/tests/restart/Cargo.lock
generated
@ -3107,7 +3107,7 @@ checksum = "2c8d5cf83fb08083438c5c46723e6206b2970da57ce314f80b57724439aaacab"
|
||||
[[package]]
|
||||
name = "wry"
|
||||
version = "0.12.2"
|
||||
source = "git+ssh://git@github.com/tauri-sec/wry?branch=next#f4005af1e30894bae955eb28b1eec68b6e933ef7"
|
||||
source = "git+https://github.com/tauri-sec/wry?branch=next#0f9eb9b1b9288b5be443bced0e8fa58a2479c491"
|
||||
dependencies = [
|
||||
"cocoa",
|
||||
"core-graphics 0.22.3",
|
||||
|
@ -40,11 +40,12 @@ function transformCallback(
|
||||
once = false
|
||||
): number {
|
||||
const identifier = uid()
|
||||
const prop = `_${identifier}`
|
||||
|
||||
Object.defineProperty(window, identifier, {
|
||||
Object.defineProperty(window, prop, {
|
||||
value: (result: any) => {
|
||||
if (once) {
|
||||
Reflect.deleteProperty(window, identifier)
|
||||
Reflect.deleteProperty(window, prop)
|
||||
}
|
||||
|
||||
return callback?.(result)
|
||||
|
Loading…
Reference in New Issue
Block a user