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
77function 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