@@ -9,7 +9,7 @@ use std::fmt::Display;
99/// use tauri_api::rpc::format_callback;
1010/// // callback with a string argument
1111/// let cb = format_callback("callback-function-name", "the string response");
12- /// assert_eq !(cb, r#"window["callback-function-name"]("the string response")"#);
12+ /// assert !(cb.contains( r#"window["callback-function-name"]("the string response")"#) );
1313/// ```
1414///
1515/// ```
@@ -23,13 +23,23 @@ use std::fmt::Display;
2323/// let cb = format_callback("callback-function-name", serde_json::to_value(&MyResponse {
2424/// value: "some value".to_string()
2525/// }).expect("failed to serialize"));
26- /// assert_eq !(cb, r#"window["callback-function-name"]({"value":"some value"})"#);
26+ /// assert !(cb.contains( r#"window["callback-function-name"]({"value":"some value"})"#) );
2727/// ```
2828pub fn format_callback < T : Into < JsonValue > , S : AsRef < str > + Display > (
2929 function_name : S ,
3030 arg : T ,
3131) -> String {
32- format ! ( r#"window["{}"]({})"# , function_name, arg. into( ) . to_string( ) )
32+ format ! (
33+ r#"
34+ if (window["{fn}"]) {{
35+ window["{fn}"]({arg})
36+ }} else {{
37+ 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.")
38+ }}
39+ "# ,
40+ fn = function_name,
41+ arg = arg. into( ) . to_string( )
42+ )
3343}
3444
3545/// Formats a Result type to its Promise response.
@@ -48,11 +58,11 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
4858/// use tauri_api::rpc::format_callback_result;
4959/// let res: Result<u8, &str> = Ok(5);
5060/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
51- /// assert_eq !(cb, r#"window["success_cb"](5)"#);
61+ /// assert !(cb.contains( r#"window["success_cb"](5)"#) );
5262///
5363/// let res: Result<&str, &str> = Err("error message here");
5464/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
55- /// assert_eq !(cb, r#"window["error_cb"]("error message here")"#);
65+ /// assert !(cb.contains( r#"window["error_cb"]("error message here")"#) );
5666/// ```
5767pub fn format_callback_result < T : Serialize , E : Serialize > (
5868 result : Result < T , E > ,
@@ -78,7 +88,11 @@ mod test {
7888 if f != "" && a != "" {
7989 // call format callback
8090 let fc = format_callback ( f. clone ( ) , a. clone ( ) ) ;
81- fc == format ! ( r#"window["{}"]({})"# , f, serde_json:: Value :: String ( a) )
91+ fc. contains ( & format ! (
92+ r#"window["{}"]({})"# ,
93+ f,
94+ serde_json:: Value :: String ( a) ,
95+ ) )
8296 } else {
8397 true
8498 }
@@ -94,11 +108,10 @@ mod test {
94108 Err ( e) => ( ec, e) ,
95109 } ;
96110
97- resp
98- == format ! (
99- r#"window["{}"]({})"# ,
100- function,
101- serde_json:: Value :: String ( value) ,
102- )
111+ resp. contains ( & format ! (
112+ r#"window["{}"]({})"# ,
113+ function,
114+ serde_json:: Value :: String ( value) ,
115+ ) )
103116 }
104117}
0 commit comments