feat: add Builder::append_invoke_initialization_script (#10295)

* Allow to append a custom initialization script

* docs: add doc for `Builder::append_invoke_initialization_script`

* Update core/tauri/src/app.rs

* Update core/tauri/src/app.rs

* add change file

* fix signature

* fix doc test

* doc fmt

---------

Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app>
This commit is contained in:
LiesAuer 2024-08-20 03:41:28 +08:00 committed by GitHub
parent ed04cc3d36
commit 30c7685eb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---
Added `Builder::append_invoke_initialization_script`.

View File

@ -1328,6 +1328,53 @@ impl<R: Runtime> Builder<R> {
self
}
/// Append a custom initialization script.
///
/// Allow to append custom initialization script instend of replacing entire invoke system.
///
/// # Examples
///
/// ```
/// let custom_script = r#"
/// // A custom call system bridge build on top of tauri invoke system.
/// async function invoke(cmd, args = {}) {
/// if (!args) args = {};
///
/// let prefix = "";
///
/// if (args?.__module) {
/// prefix = `plugin:hybridcall.${args.__module}|`;
/// }
///
/// const command = `${prefix}tauri_${cmd}`;
///
/// const invoke = window.__TAURI_INTERNALS__.invoke;
///
/// return invoke(command, args).then(result => {
/// if (window.build.debug) {
/// console.log(`call: ${command}`);
/// console.log(`args: ${JSON.stringify(args)}`);
/// console.log(`return: ${JSON.stringify(result)}`);
/// }
///
/// return result;
/// });
/// }
/// "#;
///
/// tauri::Builder::default()
/// .append_invoke_initialization_script(custom_script);
/// ```
pub fn append_invoke_initialization_script(
mut self,
initialization_script: impl AsRef<str>,
) -> Self {
self
.invoke_initialization_script
.push_str(initialization_script.as_ref());
self
}
/// Defines the setup hook.
///
/// # Examples