mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-09-22 05:39:53 +03:00
feat(api/invoke): separate cmd arg (#1321)
This commit is contained in:
parent
c6b72781cb
commit
427d170930
5
.changes/separate-cmd-arg.md
Normal file
5
.changes/separate-cmd-arg.md
Normal file
@ -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.
|
@ -1,7 +1,7 @@
|
|||||||
declare global {
|
declare global {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
interface Window {
|
interface Window {
|
||||||
__TAURI_INVOKE_HANDLER__: (command: string) => void
|
__TAURI_INVOKE_HANDLER__: (command: { [key: string]: unknown }) => void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +56,10 @@ function transformCallback(
|
|||||||
*
|
*
|
||||||
* @return {Promise<T>} Promise resolving or rejecting to the backend response
|
* @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) => {
|
return new Promise((resolve, reject) => {
|
||||||
const callback = transformCallback((e) => {
|
const callback = transformCallback((e) => {
|
||||||
resolve(e)
|
resolve(e)
|
||||||
@ -67,6 +70,14 @@ async function invoke<T>(args: any): Promise<T> {
|
|||||||
Reflect.deleteProperty(window, callback)
|
Reflect.deleteProperty(window, callback)
|
||||||
}, true)
|
}, 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__({
|
window.__TAURI_INVOKE_HANDLER__({
|
||||||
callback,
|
callback,
|
||||||
error,
|
error,
|
||||||
|
@ -103,7 +103,7 @@ if (!String.prototype.startsWith) {
|
|||||||
return identifier;
|
return identifier;
|
||||||
};
|
};
|
||||||
|
|
||||||
window.__TAURI__.invoke = function invoke(args) {
|
window.__TAURI__.invoke = function invoke(cmd, args = {}) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
@ -116,6 +116,14 @@ if (!String.prototype.startsWith) {
|
|||||||
delete window[callback];
|
delete window[callback];
|
||||||
}, true);
|
}, 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__) {
|
if (window.__TAURI_INVOKE_HANDLER__) {
|
||||||
window.__TAURI_INVOKE_HANDLER__(
|
window.__TAURI_INVOKE_HANDLER__(
|
||||||
_objectSpread(
|
_objectSpread(
|
||||||
@ -191,18 +199,18 @@ if (!String.prototype.startsWith) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.__TAURI__.invoke({
|
window.__TAURI__.invoke({
|
||||||
__tauriModule: 'Event',
|
__tauriModule: "Event",
|
||||||
message: {
|
message: {
|
||||||
cmd: 'listen',
|
cmd: "listen",
|
||||||
event: 'tauri://window-created',
|
event: "tauri://window-created",
|
||||||
handler: window.__TAURI__.transformCallback(function (event) {
|
handler: window.__TAURI__.transformCallback(function (event) {
|
||||||
if (event.payload) {
|
if (event.payload) {
|
||||||
var windowLabel = event.payload.label
|
var windowLabel = event.payload.label;
|
||||||
window.__TAURI__.__windows.push({ label: windowLabel })
|
window.__TAURI__.__windows.push({ label: windowLabel });
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
let permissionSettable = false;
|
let permissionSettable = false;
|
||||||
let permissionValue = "default";
|
let permissionValue = "default";
|
||||||
|
@ -2,26 +2,24 @@
|
|||||||
import { listen, emit } from "@tauri-apps/api/event";
|
import { listen, emit } from "@tauri-apps/api/event";
|
||||||
import { invoke } from "@tauri-apps/api/tauri";
|
import { invoke } from "@tauri-apps/api/tauri";
|
||||||
|
|
||||||
export let onMessage
|
export let onMessage;
|
||||||
|
|
||||||
listen("rust-event", onMessage);
|
listen("rust-event", onMessage);
|
||||||
|
|
||||||
function log() {
|
function log() {
|
||||||
invoke({
|
invoke("log_operation", {
|
||||||
cmd: "log_operation",
|
|
||||||
event: "tauri-click",
|
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() {
|
function performRequest() {
|
||||||
invoke({
|
invoke("perform_request", {
|
||||||
cmd: "perform_request",
|
|
||||||
endpoint: "dummy endpoint arg",
|
endpoint: "dummy endpoint arg",
|
||||||
body: {
|
body: {
|
||||||
id: 5,
|
id: 5,
|
||||||
name: "test"
|
name: "test",
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
.then(onMessage)
|
.then(onMessage)
|
||||||
.catch(onMessage);
|
.catch(onMessage);
|
||||||
@ -40,4 +38,4 @@
|
|||||||
<button class="button" id="event" on:click={emitEvent}>
|
<button class="button" id="event" on:click={emitEvent}>
|
||||||
Send event to Rust
|
Send event to Rust
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user