Skip to content

@statesync/electron


@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 ParameterDescription
TThe application-specific snapshot data type.

Parameters

ParameterTypeDescription
optionsElectronSnapshotHandlerOptions<T>Configuration for the snapshot handler.

Returns

ElectronSnapshotHandlerHandle

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();

Released under the MIT License.