Skip to content

Commit

Permalink
feat(api/invoke): separate cmd arg (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
nklayman committed Mar 5, 2021
1 parent c6b7278 commit 427d170
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changes/separate-cmd-arg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": minor
---

The invoke function can now be called with the cmd as the first parameter and the args as the second.
15 changes: 13 additions & 2 deletions api/src/tauri.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare global {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Window {
__TAURI_INVOKE_HANDLER__: (command: string) => void
__TAURI_INVOKE_HANDLER__: (command: { [key: string]: unknown }) => void
}
}

Expand Down Expand Up @@ -56,7 +56,10 @@ function transformCallback(
*
* @return {Promise<T>} Promise resolving or rejecting to the backend response
*/
async function invoke<T>(args: any): Promise<T> {
async function invoke<T>(
cmd: string | { [key: string]: unknown },
args: { [key: string]: unknown } = {}
): Promise<T> {
return new Promise((resolve, reject) => {
const callback = transformCallback((e) => {
resolve(e)
Expand All @@ -67,6 +70,14 @@ async function invoke<T>(args: any): Promise<T> {
Reflect.deleteProperty(window, callback)
}, true)

if (typeof cmd === 'string') {
args.cmd = cmd
} else if (typeof cmd === 'object') {
args = cmd
} else {
return reject(new Error('Invalid argument type.'))
}

window.__TAURI_INVOKE_HANDLER__({
callback,
error,
Expand Down
26 changes: 17 additions & 9 deletions cli/core/src/templates/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ if (!String.prototype.startsWith) {
return identifier;
};

window.__TAURI__.invoke = function invoke(args) {
window.__TAURI__.invoke = function invoke(cmd, args = {}) {
var _this = this;

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

if (typeof cmd === "string") {
args.cmd = cmd;
} else if (typeof cmd === "object") {
args = cmd;
} else {
return reject(new Error("Invalid argument type."));
}

if (window.__TAURI_INVOKE_HANDLER__) {
window.__TAURI_INVOKE_HANDLER__(
_objectSpread(
Expand Down Expand Up @@ -191,18 +199,18 @@ if (!String.prototype.startsWith) {
}

window.__TAURI__.invoke({
__tauriModule: 'Event',
__tauriModule: "Event",
message: {
cmd: 'listen',
event: 'tauri://window-created',
cmd: "listen",
event: "tauri://window-created",
handler: window.__TAURI__.transformCallback(function (event) {
if (event.payload) {
var windowLabel = event.payload.label
window.__TAURI__.__windows.push({ label: windowLabel })
var windowLabel = event.payload.label;
window.__TAURI__.__windows.push({ label: windowLabel });
}
})
}
})
}),
},
});

let permissionSettable = false;
let permissionValue = "default";
Expand Down
16 changes: 7 additions & 9 deletions tauri/examples/api/src/components/Communication.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
import { listen, emit } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/tauri";
export let onMessage
export let onMessage;
listen("rust-event", onMessage);
function log() {
invoke({
cmd: "log_operation",
invoke("log_operation", {
event: "tauri-click",
payload: "this payload is optional because we used Option in Rust"
payload: "this payload is optional because we used Option in Rust",
});
}
function performRequest() {
invoke({
cmd: "perform_request",
invoke("perform_request", {
endpoint: "dummy endpoint arg",
body: {
id: 5,
name: "test"
}
name: "test",
},
})
.then(onMessage)
.catch(onMessage);
Expand All @@ -40,4 +38,4 @@
<button class="button" id="event" on:click={emitEvent}>
Send event to Rust
</button>
</div>
</div>

0 comments on commit 427d170

Please sign in to comment.