Skip to content

Commit

Permalink
feat: add Builder::append_invoke_initialization_script (#10295)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
liesauer and lucasfernog authored Aug 19, 2024
1 parent ed04cc3 commit 30c7685
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/append_invoke_initialization_script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Added `Builder::append_invoke_initialization_script`.
47 changes: 47 additions & 0 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 30c7685

Please sign in to comment.