Skip to content

Commit 427d170

Browse files
authored
feat(api/invoke): separate cmd arg (#1321)
1 parent c6b7278 commit 427d170

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

.changes/separate-cmd-arg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": minor
3+
---
4+
5+
The invoke function can now be called with the cmd as the first parameter and the args as the second.

api/src/tauri.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
declare global {
22
// eslint-disable-next-line @typescript-eslint/no-unused-vars
33
interface Window {
4-
__TAURI_INVOKE_HANDLER__: (command: string) => void
4+
__TAURI_INVOKE_HANDLER__: (command: { [key: string]: unknown }) => void
55
}
66
}
77

@@ -56,7 +56,10 @@ function transformCallback(
5656
*
5757
* @return {Promise<T>} Promise resolving or rejecting to the backend response
5858
*/
59-
async function invoke<T>(args: any): Promise<T> {
59+
async function invoke<T>(
60+
cmd: string | { [key: string]: unknown },
61+
args: { [key: string]: unknown } = {}
62+
): Promise<T> {
6063
return new Promise((resolve, reject) => {
6164
const callback = transformCallback((e) => {
6265
resolve(e)
@@ -67,6 +70,14 @@ async function invoke<T>(args: any): Promise<T> {
6770
Reflect.deleteProperty(window, callback)
6871
}, true)
6972

73+
if (typeof cmd === 'string') {
74+
args.cmd = cmd
75+
} else if (typeof cmd === 'object') {
76+
args = cmd
77+
} else {
78+
return reject(new Error('Invalid argument type.'))
79+
}
80+
7081
window.__TAURI_INVOKE_HANDLER__({
7182
callback,
7283
error,

cli/core/src/templates/tauri.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ if (!String.prototype.startsWith) {
103103
return identifier;
104104
};
105105

106-
window.__TAURI__.invoke = function invoke(args) {
106+
window.__TAURI__.invoke = function invoke(cmd, args = {}) {
107107
var _this = this;
108108

109109
return new Promise(function (resolve, reject) {
@@ -116,6 +116,14 @@ if (!String.prototype.startsWith) {
116116
delete window[callback];
117117
}, true);
118118

119+
if (typeof cmd === "string") {
120+
args.cmd = cmd;
121+
} else if (typeof cmd === "object") {
122+
args = cmd;
123+
} else {
124+
return reject(new Error("Invalid argument type."));
125+
}
126+
119127
if (window.__TAURI_INVOKE_HANDLER__) {
120128
window.__TAURI_INVOKE_HANDLER__(
121129
_objectSpread(
@@ -191,18 +199,18 @@ if (!String.prototype.startsWith) {
191199
}
192200

193201
window.__TAURI__.invoke({
194-
__tauriModule: 'Event',
202+
__tauriModule: "Event",
195203
message: {
196-
cmd: 'listen',
197-
event: 'tauri://window-created',
204+
cmd: "listen",
205+
event: "tauri://window-created",
198206
handler: window.__TAURI__.transformCallback(function (event) {
199207
if (event.payload) {
200-
var windowLabel = event.payload.label
201-
window.__TAURI__.__windows.push({ label: windowLabel })
208+
var windowLabel = event.payload.label;
209+
window.__TAURI__.__windows.push({ label: windowLabel });
202210
}
203-
})
204-
}
205-
})
211+
}),
212+
},
213+
});
206214

207215
let permissionSettable = false;
208216
let permissionValue = "default";

tauri/examples/api/src/components/Communication.svelte

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@
22
import { listen, emit } from "@tauri-apps/api/event";
33
import { invoke } from "@tauri-apps/api/tauri";
44
5-
export let onMessage
5+
export let onMessage;
66
77
listen("rust-event", onMessage);
88
99
function log() {
10-
invoke({
11-
cmd: "log_operation",
10+
invoke("log_operation", {
1211
event: "tauri-click",
13-
payload: "this payload is optional because we used Option in Rust"
12+
payload: "this payload is optional because we used Option in Rust",
1413
});
1514
}
1615
1716
function performRequest() {
18-
invoke({
19-
cmd: "perform_request",
17+
invoke("perform_request", {
2018
endpoint: "dummy endpoint arg",
2119
body: {
2220
id: 5,
23-
name: "test"
24-
}
21+
name: "test",
22+
},
2523
})
2624
.then(onMessage)
2725
.catch(onMessage);
@@ -40,4 +38,4 @@
4038
<button class="button" id="event" on:click={emitEvent}>
4139
Send event to Rust
4240
</button>
43-
</div>
41+
</div>

0 commit comments

Comments
 (0)