Skip to content

Commit 152d971

Browse files
fix(mock): expose callback functions (#13744)
* fix(mock): expose callback functions * Add change file * Bumped the wrong package * Fix end quote in comment
1 parent acd7574 commit 152d971

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": "patch:bug"
3+
---
4+
5+
Expose `unregisterCallback`, `runCallback`, `callbacks` in `mockIPC`

crates/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/src/global.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ declare global {
1212
__TAURI_INTERNALS__: {
1313
invoke: typeof invoke
1414
transformCallback: typeof transformCallback
15-
unregisterCallback: (number) => void
15+
unregisterCallback: (id: number) => void
16+
runCallback: (id: number, data: unknown) => void
17+
callbacks: Map<number, (data: unknown) => void>
1618
convertFileSrc: typeof convertFileSrc
1719
ipc: (message: {
1820
cmd: string

packages/api/src/mocks.ts

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
import type { invoke, InvokeArgs, InvokeOptions } from './core'
5+
import type { InvokeArgs, InvokeOptions } from './core'
66

77
function mockInternals() {
88
window.__TAURI_INTERNALS__ = window.__TAURI_INTERNALS__ ?? {}
@@ -66,18 +66,27 @@ export function mockIPC(
6666
): void {
6767
mockInternals()
6868

69-
const callbacks = new Map()
69+
// eslint-disable-next-line @typescript-eslint/require-await
70+
async function invoke<T>(
71+
cmd: string,
72+
args?: InvokeArgs,
73+
_options?: InvokeOptions
74+
): Promise<T> {
75+
return cb(cmd, args) as T
76+
}
77+
78+
const callbacks = new Map<number, (data: unknown) => void>()
7079

7180
function registerCallback<T = unknown>(
7281
callback?: (response: T) => void,
7382
once = false
7483
) {
7584
const identifier = window.crypto.getRandomValues(new Uint32Array(1))[0]
76-
callbacks.set(identifier, (data: T) => {
85+
callbacks.set(identifier, (data) => {
7786
if (once) {
7887
unregisterCallback(identifier)
7988
}
80-
return callback && callback(data)
89+
return callback && callback(data as T)
8190
})
8291
return identifier
8392
}
@@ -86,16 +95,23 @@ export function mockIPC(
8695
callbacks.delete(id)
8796
}
8897

89-
window.__TAURI_INTERNALS__.transformCallback = registerCallback
98+
function runCallback(id: number, data: unknown) {
99+
const callback = callbacks.get(id)
100+
if (callback) {
101+
callback(data)
102+
} else {
103+
// eslint-disable-next-line no-console
104+
console.warn(
105+
`[TAURI] Couldn't find callback id ${id}. This might happen when the app is reloaded while Rust is running an asynchronous operation.`
106+
)
107+
}
108+
}
90109

91-
// eslint-disable-next-line @typescript-eslint/require-await
92-
window.__TAURI_INTERNALS__.invoke = async function (
93-
cmd: string,
94-
args?: InvokeArgs,
95-
_options?: InvokeOptions
96-
): Promise<unknown> {
97-
return cb(cmd, args)
98-
} as typeof invoke
110+
window.__TAURI_INTERNALS__.invoke = invoke
111+
window.__TAURI_INTERNALS__.transformCallback = registerCallback
112+
window.__TAURI_INTERNALS__.unregisterCallback = unregisterCallback
113+
window.__TAURI_INTERNALS__.runCallback = runCallback
114+
window.__TAURI_INTERNALS__.callbacks = callbacks
99115
}
100116

101117
/**
@@ -210,13 +226,18 @@ export function clearMocks(): void {
210226
return
211227
}
212228

213-
if (window.__TAURI_INTERNALS__?.convertFileSrc)
214-
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
215-
delete window.__TAURI_INTERNALS__.convertFileSrc
216-
if (window.__TAURI_INTERNALS__?.invoke)
217-
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
218-
delete window.__TAURI_INTERNALS__.invoke
219-
if (window.__TAURI_INTERNALS__?.metadata)
220-
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
221-
delete window.__TAURI_INTERNALS__.metadata
229+
// @ts-expect-error "The operand of a 'delete' operator must be optional." does not matter in this case
230+
delete window.__TAURI_INTERNALS__.invoke
231+
// @ts-expect-error "The operand of a 'delete' operator must be optional." does not matter in this case
232+
delete window.__TAURI_INTERNALS__.transformCallback
233+
// @ts-expect-error "The operand of a 'delete' operator must be optional." does not matter in this case
234+
delete window.__TAURI_INTERNALS__.unregisterCallback
235+
// @ts-expect-error "The operand of a 'delete' operator must be optional." does not matter in this case
236+
delete window.__TAURI_INTERNALS__.runCallback
237+
// @ts-expect-error "The operand of a 'delete' operator must be optional." does not matter in this case
238+
delete window.__TAURI_INTERNALS__.callbacks
239+
// @ts-expect-error "The operand of a 'delete' operator must be optional." does not matter in this case
240+
delete window.__TAURI_INTERNALS__.convertFileSrc
241+
// @ts-expect-error "The operand of a 'delete' operator must be optional." does not matter in this case
242+
delete window.__TAURI_INTERNALS__.metadata
222243
}

0 commit comments

Comments
 (0)