Skip to content

Commit ea73325

Browse files
authored
refactor(core): all API are now promise based (#1239)
1 parent a7bc472 commit ea73325

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1621
-2086
lines changed

.changes/api-refactor.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor
3+
---
4+
5+
Now Tauri commands always returns Promise<T>.

api/src/cli.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { promisified } from './tauri'
1+
import { invoke } from './tauri'
22

33
export interface ArgMatch {
44
/**
@@ -27,8 +27,8 @@ export interface CliMatches {
2727
* gets the CLI matches
2828
*/
2929
async function getMatches(): Promise<CliMatches> {
30-
return await promisified<CliMatches>({
31-
module: 'Cli',
30+
return await invoke<CliMatches>({
31+
__tauriModule: 'Cli',
3232
message: {
3333
cmd: 'cliMatches'
3434
}

api/src/dialog.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { promisified } from './tauri'
1+
import { invoke } from './tauri'
22

33
export interface OpenDialogOptions {
44
filter?: string
@@ -29,8 +29,9 @@ async function open(
2929
Object.freeze(options)
3030
}
3131

32-
return await promisified({
33-
module: 'Dialog',
32+
return await invoke<string | string[]>({
33+
__tauriModule: 'Dialog',
34+
mainThread: true,
3435
message: {
3536
cmd: 'openDialog',
3637
options
@@ -51,8 +52,9 @@ async function save(options: SaveDialogOptions = {}): Promise<string> {
5152
Object.freeze(options)
5253
}
5354

54-
return await promisified({
55-
module: 'Dialog',
55+
return await invoke<string>({
56+
__tauriModule: 'Dialog',
57+
mainThread: true,
5658
message: {
5759
cmd: 'saveDialog',
5860
options

api/src/event.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export type EventCallback<T> = (event: Event<T>) => void
1313
* @param event the event name
1414
* @param handler the event handler callback
1515
*/
16-
function listen<T>(
16+
async function listen<T>(
1717
event: string,
1818
handler: EventCallback<T>,
1919
once = false
20-
): void {
21-
invoke({
22-
module: 'Event',
20+
): Promise<void> {
21+
await await invoke({
22+
__tauriModule: 'Event',
2323
message: {
2424
cmd: 'listen',
2525
event,
@@ -35,9 +35,9 @@ function listen<T>(
3535
* @param event the event name
3636
* @param [payload] the event payload
3737
*/
38-
function emit(event: string, payload?: string): void {
39-
invoke({
40-
module: 'Event',
38+
async function emit(event: string, payload?: string): Promise<void> {
39+
await invoke({
40+
__tauriModule: 'Event',
4141
message: {
4242
cmd: 'emit',
4343
event,

api/src/fs.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { promisified } from './tauri'
1+
import { invoke } from './tauri'
22

33
export enum BaseDirectory {
44
Audio = 1,
@@ -61,8 +61,8 @@ async function readTextFile(
6161
filePath: string,
6262
options: FsOptions = {}
6363
): Promise<string> {
64-
return await promisified<string>({
65-
module: 'Fs',
64+
return await invoke<string>({
65+
__tauriModule: 'Fs',
6666
message: {
6767
cmd: 'readTextFile',
6868
path: filePath,
@@ -83,8 +83,8 @@ async function readBinaryFile(
8383
filePath: string,
8484
options: FsOptions = {}
8585
): Promise<number[]> {
86-
return await promisified<number[]>({
87-
module: 'Fs',
86+
return await invoke<number[]>({
87+
__tauriModule: 'Fs',
8888
message: {
8989
cmd: 'readBinaryFile',
9090
path: filePath,
@@ -114,8 +114,8 @@ async function writeFile(
114114
Object.freeze(file)
115115
}
116116

117-
return await promisified({
118-
module: 'Fs',
117+
return await invoke<void>({
118+
__tauriModule: 'Fs',
119119
message: {
120120
cmd: 'writeFile',
121121
path: file.path,
@@ -179,8 +179,8 @@ async function writeBinaryFile(
179179
Object.freeze(file)
180180
}
181181

182-
return await promisified({
183-
module: 'Fs',
182+
return await invoke<void>({
183+
__tauriModule: 'Fs',
184184
message: {
185185
cmd: 'writeBinaryFile',
186186
path: file.path,
@@ -203,8 +203,8 @@ async function readDir(
203203
dir: string,
204204
options: FsDirOptions = {}
205205
): Promise<FileEntry[]> {
206-
return await promisified({
207-
module: 'Fs',
206+
return await invoke<void>({
207+
__tauriModule: 'Fs',
208208
message: {
209209
cmd: 'readDir',
210210
path: dir,
@@ -228,8 +228,8 @@ async function createDir(
228228
dir: string,
229229
options: FsDirOptions = {}
230230
): Promise<void> {
231-
return await promisified({
232-
module: 'Fs',
231+
return await invoke<void>({
232+
__tauriModule: 'Fs',
233233
message: {
234234
cmd: 'createDir',
235235
path: dir,
@@ -252,8 +252,8 @@ async function removeDir(
252252
dir: string,
253253
options: FsDirOptions = {}
254254
): Promise<void> {
255-
return await promisified({
256-
module: 'Fs',
255+
return await invoke<void>({
256+
__tauriModule: 'Fs',
257257
message: {
258258
cmd: 'removeDir',
259259
path: dir,
@@ -276,8 +276,8 @@ async function copyFile(
276276
destination: string,
277277
options: FsOptions = {}
278278
): Promise<void> {
279-
return await promisified({
280-
module: 'Fs',
279+
return await invoke<void>({
280+
__tauriModule: 'Fs',
281281
message: {
282282
cmd: 'copyFile',
283283
source,
@@ -299,8 +299,8 @@ async function removeFile(
299299
file: string,
300300
options: FsOptions = {}
301301
): Promise<void> {
302-
return await promisified({
303-
module: 'Fs',
302+
return await invoke<void>({
303+
__tauriModule: 'Fs',
304304
message: {
305305
cmd: 'removeFile',
306306
path: file,
@@ -323,8 +323,8 @@ async function renameFile(
323323
newPath: string,
324324
options: FsOptions = {}
325325
): Promise<void> {
326-
return await promisified({
327-
module: 'Fs',
326+
return await invoke<void>({
327+
__tauriModule: 'Fs',
328328
message: {
329329
cmd: 'renameFile',
330330
oldPath,

api/src/globalShortcut.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { promisified, transformCallback } from './tauri'
1+
import { invoke, transformCallback } from './tauri'
22

33
/**
44
* register a global shortcut
@@ -9,8 +9,8 @@ async function registerShortcut(
99
shortcut: string,
1010
handler: () => void
1111
): Promise<void> {
12-
return await promisified({
13-
module: 'GlobalShortcut',
12+
return await invoke<void>({
13+
__tauriModule: 'GlobalShortcut',
1414
message: {
1515
cmd: 'register',
1616
shortcut,
@@ -24,16 +24,13 @@ async function registerShortcut(
2424
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. Alt+Q
2525
*/
2626
async function unregisterShortcut(shortcut: string): Promise<void> {
27-
return await promisified({
28-
module: 'GlobalShortcut',
27+
return await invoke<void>({
28+
__tauriModule: 'GlobalShortcut',
2929
message: {
3030
cmd: 'unregister',
3131
shortcut
3232
}
3333
})
3434
}
3535

36-
export {
37-
registerShortcut,
38-
unregisterShortcut
39-
}
36+
export { registerShortcut, unregisterShortcut }

api/src/http.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { invoke, promisified } from './tauri'
1+
import { invoke } from './tauri'
22

33
export interface ClientOptions {
44
maxRedirections: boolean
@@ -79,8 +79,8 @@ export class Client {
7979
/**
8080
* drops the client instance
8181
*/
82-
drop(): void {
83-
invoke({
82+
async drop(): Promise<void> {
83+
await invoke({
8484
module: 'Http',
8585
message: {
8686
cmd: 'dropClient',
@@ -97,7 +97,7 @@ export class Client {
9797
* @return promise resolving to the response
9898
*/
9999
async request<T>(options: HttpOptions): Promise<Response<T>> {
100-
return await promisified({
100+
return await invoke({
101101
module: 'Http',
102102
message: {
103103
cmd: 'httpRequest',
@@ -204,7 +204,7 @@ export class Client {
204204
}
205205

206206
async function getClient(options?: ClientOptions): Promise<Client> {
207-
return await promisified<number>({
207+
return await invoke<number>({
208208
module: 'Http',
209209
message: {
210210
cmd: 'createClient',

api/src/notification.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { promisified } from './tauri'
1+
import { invoke } from './tauri'
22

33
export interface Options {
44
title: string
@@ -13,8 +13,8 @@ async function isPermissionGranted(): Promise<boolean | null> {
1313
if (window.Notification.permission !== 'default') {
1414
return await Promise.resolve(window.Notification.permission === 'granted')
1515
}
16-
return await promisified({
17-
module: 'Notification',
16+
return await invoke<void>({
17+
__tauriModule: 'Notification',
1818
message: {
1919
cmd: 'isNotificationPermissionGranted'
2020
}

0 commit comments

Comments
 (0)