Function tauri::api::ipc::format_callback

source ·
pub fn format_callback<T: Serialize>(
    function_name: CallbackFn,
    arg: &T
) -> Result<String>
Expand description

Formats a function name and argument to be evaluated as callback.

This will serialize primitive JSON types (e.g. booleans, strings, numbers, etc.) as JavaScript literals, but will serialize arrays and objects whose serialized JSON string is smaller than 1 GB and larger than 10 KiB with JSON.parse('...'). See json-parse-benchmark.

Examples

  • With string literals:
use tauri::api::ipc::{CallbackFn, format_callback};
// callback with a string argument
let cb = format_callback(CallbackFn(12345), &"the string response").unwrap();
assert!(cb.contains(r#"window["_12345"]("the string response")"#));
use tauri::api::ipc::{CallbackFn, format_callback};
use serde::Serialize;

// callback with large JSON argument
#[derive(Serialize)]
struct MyResponse {
  value: String
}

let cb = format_callback(
  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"#));