import { effect , stop , toRaw , unref } from '@vue/reactivity' ; import type { ReactiveEffectOptions , Ref } from '@vue/reactivity' ; import { ipcMain } from 'electron' ; import { broadcast } from '../lib/frame' ; export function useEffect < T > ( fn , options ? ) { let cleanup ; const onInvalidate = ( cleanupFn ) => { cleanup = cleanupFn ; } ; return effect ( ( ) => { if ( cleanup ) { cleanup ( ) ; } cleanup = undefined ; return fn ( onInvalidate ) ; } , { ... options , onStop ( ) { if ( cleanup ) { cleanup ( ) ; } const onStop = options ?. onStop ; if ( onStop ) { onStop ( ) ; } } , } ) ; } export function provideIPC < T > ( key , valueRef ) { ipcMain . handle ( `get-ref:${ key }` , ( ) => toRaw ( unref ( valueRef ) ) ) ; ipcMain . handle ( `set-ref:${ key }` , ( event , value ) => { valueRef . value = value ; } ) ; let latestValuePromise ; const reactiveEffect = effect ( async ( ) => { const valuePromise = Promise . resolve ( unref ( valueRef ) ) ; latestValuePromise = valuePromise ; const value = await valuePromise ; if ( valuePromise === latestValuePromise ) { broadcast ( `update-ref:${ key }` , value ) ; } } ) ; return ( ) => { ipcMain . removeHandler ( `get-ref:${ key }` ) ; ipcMain . removeHandler ( `set-ref:${ key }` ) ; stop ( reactiveEffect ) ; } ; }