Plugin execution context (#119)

This commit is contained in:
Gregory Schier
2024-09-30 17:45:51 -07:00
committed by GitHub
parent 917adcfb2e
commit 9915c57817
29 changed files with 637 additions and 373 deletions

View File

@@ -1,5 +1,6 @@
import type { EventCallback, EventName, Options } from '@tauri-apps/api/event';
import type { EventCallback, EventName } from '@tauri-apps/api/event';
import { listen } from '@tauri-apps/api/event';
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
import type { DependencyList } from 'react';
import { useEffect } from 'react';
@@ -9,14 +10,18 @@ import { useEffect } from 'react';
export function useListenToTauriEvent<T>(
event: EventName,
fn: EventCallback<T>,
options: Options | undefined = undefined,
deps: DependencyList = [],
) {
useEffect(() => {
let unMounted = false;
let unsubFn: (() => void) | undefined = undefined;
listen(event, fn, options).then((unsub) => {
listen(
event,
fn,
// Listen to `emit_all()` events or events specific to the current window
{ target: { label: getCurrentWebviewWindow().label, kind: 'Window' } },
).then((unsub) => {
if (unMounted) unsub();
else unsubFn = unsub;
});