50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
// Disable no-unused-vars, broken for spread args
|
|
/* eslint no-unused-vars: off */
|
|
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
|
|
import { LoadAudioBufferArgs } from '../ipc/audio/types';
|
|
import AudioChannels from '../ipc/audio/channels';
|
|
// import '../ipc/file/preload'; // Import file API preload to ensure it runs and exposes the API
|
|
|
|
export type Channels = 'ipc-example';
|
|
|
|
const electronHandler = {
|
|
ipcRenderer: {
|
|
sendMessage(channel: Channels, ...args: unknown[]) {
|
|
ipcRenderer.send(channel, ...args);
|
|
},
|
|
on(channel: Channels, func: (...args: unknown[]) => void) {
|
|
const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
|
|
func(...args);
|
|
ipcRenderer.on(channel, subscription);
|
|
|
|
return () => {
|
|
ipcRenderer.removeListener(channel, subscription);
|
|
};
|
|
},
|
|
once(channel: Channels, func: (...args: unknown[]) => void) {
|
|
ipcRenderer.once(channel, (_event, ...args) => func(...args));
|
|
},
|
|
|
|
invoke: (event: string, ...args: unknown[]) =>
|
|
ipcRenderer.invoke(event, ...args),
|
|
},
|
|
};
|
|
|
|
contextBridge.exposeInMainWorld('electron', electronHandler);
|
|
|
|
export type ElectronHandler = typeof electronHandler;
|
|
|
|
const audioHandler = {
|
|
loadAudioBuffer: (filePath: string) =>
|
|
ipcRenderer.invoke(AudioChannels.LOAD_AUDIO_BUFFER, {
|
|
filePath,
|
|
} satisfies LoadAudioBufferArgs),
|
|
|
|
getPort: () => ipcRenderer.invoke(AudioChannels.GET_PORT),
|
|
restartService: () => ipcRenderer.invoke(AudioChannels.RESTART_SERVICE),
|
|
};
|
|
|
|
contextBridge.exposeInMainWorld('audio', audioHandler);
|
|
|
|
export type AudioHandler = typeof audioHandler;
|