Skip to content

@signaldb/electron ​

Runs SignalDB's data layer in the Electron main process and connects every window to it through a MessagePort of its own. The package has an entry point for each of Electron's process contexts; the package root exports all of them.

Also check out our guide on how to use SignalDB with Electron.

createElectronHost ​

Import from @signaldb/electron/main

ts
import { ipcMain, MessageChannelMain } from 'electron'
import { Collection } from '@signaldb/core'
import { createElectronHost } from '@signaldb/electron/main'
import createFilesystemAdapter from '@signaldb/fs'

const host = createElectronHost({
  ipcMain,
  MessageChannelMain,
  storage: name => createFilesystemAdapter(`./data/${name}`),
})

const Posts = new Collection('posts', host.dataAdapter)

Starts a WorkerDataAdapterHost in the main process and answers every window that asks for a connection with a MessageChannelMain of its own.

Options ​

  • ipcMain — Electron's ipcMain. Required.
  • MessageChannelMain — Electron's MessageChannelMain. Required.
  • storage — (name: string) => StorageAdapter. Returns the storage adapter for a collection, by its name. Called once per collection, when the first connection uses it. Required.
  • id — Must match the id of createElectronDataAdapter. Defaults to 'signaldb-electron'; only needed when an app runs more than one host.
  • onError — Called with errors that cannot be answered to a window as a failed request. Defaults to console.error.
  • log — Receives a log line for every message the host handles.

Returns ​

{ dataAdapter: WorkerDataAdapter, dispose(): Promise<void> }

  • dataAdapter — The data adapter for collections in the main process. They work on the same data as the windows: a write on either side reaches the live queries of all others.
  • dispose() — Stops accepting windows, disconnects every connection, and closes their ports. Dispose the collections that use the host first: a collection whose connection is gone receives no further answers.

Behavior ​

  • A window that is closed or reloaded closes its port, and the host releases the collections and queries that window had registered.
  • A collection's storage is set up when the first connection registers the collection, and released once the last one is gone.
  • Every connection is a separate MessagePort. Messages are structurally cloned; after its first answer a query only sends what changed in its result.

setupElectronPreload ​

Import from @signaldb/electron/preload

ts
import { ipcRenderer } from 'electron'
import { setupElectronPreload } from '@signaldb/electron/preload'

setupElectronPreload(ipcRenderer)

Relays connection requests from the window to the main process and hands the port the main process answers with to the window. A MessagePort cannot cross contextBridge, so both directions use window.postMessage. Call it once in the preload script of every window that uses createElectronDataAdapter.

Parameters ​

  • ipcRenderer — Electron's ipcRenderer.

createElectronDataAdapter ​

Import from @signaldb/electron/renderer

ts
import { Collection } from '@signaldb/core'
import { createElectronDataAdapter } from '@signaldb/electron/renderer'

const dataAdapter = createElectronDataAdapter()
const Posts = new Collection('posts', dataAdapter)

Creates the data adapter for a window's collections. It is a WorkerDataAdapter whose worker is the main process, so everything said there applies: queries are answered asynchronously and Cursor#isLoading() tells a query that has not been answered yet from one that matched nothing, and writes are shown optimistically until the main process confirms them.

Options ​

  • id — Must match the id of createElectronHost. Defaults to 'signaldb-electron'.
  • log — Receives a log line for every response, query update and failed request.

Behavior ​

  • One adapter serves any number of collections; create one per window.
  • Without setupElectronPreload in the window's preload script no port arrives, and the adapter fails with WorkerDataAdapter initialization timed out after five seconds.
  • Disposing the last collection of the adapter closes its port, which releases the connection in the main process.

IPC channels ​

ChannelDirectionPurpose
signaldb:connectwindow → mainAsks for a connection ({ id, requestId })
signaldb:portmain → windowAnswers with the port ({ id, requestId }, port transferred)

Everything else goes through the port.

Released under the MIT License.