@statesync/electron / createElectronSnapshotHandler
Function: createElectronSnapshotHandler()
ts
function createElectronSnapshotHandler<T>(options): ElectronSnapshotHandlerHandle;Defined in: main.ts:242
Registers an ipcMain.handle() listener that serves SnapshotEnvelope responses to renderer processes requesting the current snapshot.
When a renderer calls ipcRenderer.invoke(channel), the registered ElectronSnapshotHandlerOptions.getSnapshot callback is invoked, and its result is returned as the IPC response.
Process context: main process only.
Errors thrown by getSnapshot are logged to console.error and re-thrown so they propagate as IPC rejection to the renderer.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The application-specific snapshot data type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | ElectronSnapshotHandlerOptions<T> | Configuration for the snapshot handler. |
Returns
A disposable handle. Call ElectronSnapshotHandlerHandle.dispose to unregister the IPC handler.
Example
ts
import { ipcMain } from 'electron';
import { createElectronSnapshotHandler } from '@statesync/electron';
const handler = createElectronSnapshotHandler({
topic: 'todos',
getSnapshot: async () => ({
revision: '42' as Revision,
data: await db.getAllTodos(),
}),
handle: ipcMain.handle.bind(ipcMain),
removeHandler: ipcMain.removeHandler.bind(ipcMain),
});
// On app quit:
handler.dispose();