(method: string, args?: unknown)
| 245 | } |
| 246 | |
| 247 | export async function invokeInPluginWindow(method: string, args?: unknown): Promise<unknown> { |
| 248 | if (!pluginWindow || pluginWindow.isDestroyed()) { |
| 249 | createPluginWindow(); |
| 250 | } |
| 251 | |
| 252 | await waitForReady(); |
| 253 | |
| 254 | return new Promise((resolve, reject) => { |
| 255 | const id = randomUUID(); |
| 256 | const startedAt = Date.now(); |
| 257 | |
| 258 | const timeout = setTimeout(() => { |
| 259 | if (pendingRequests.has(id)) { |
| 260 | pendingRequests.delete(id); |
| 261 | recordInvocation(method, 'timeout', Date.now() - startedAt); |
| 262 | reject(new Error(`[plugin-window] timeout invoking ${method}`)); |
| 263 | } |
| 264 | }, 30_000); |
| 265 | |
| 266 | pendingRequests.set(id, { |
| 267 | method, |
| 268 | startedAt, |
| 269 | resolve: v => { |
| 270 | clearTimeout(timeout); |
| 271 | resolve(v); |
| 272 | }, |
| 273 | reject: e => { |
| 274 | clearTimeout(timeout); |
| 275 | reject(e); |
| 276 | }, |
| 277 | }); |
| 278 | |
| 279 | pluginWindow!.webContents.send('plugins.invoke', { id, method, args }); |
| 280 | }); |
| 281 | } |
| 282 | |
| 283 | export function destroyPluginWindow() { |
| 284 | pluginWindow?.destroy(); |
no test coverage detected