/** * This file contains everything * that is supposed to run inside of the electron main process */ import type { RxStorage } from 'nxdb-old/src/types'; import { Subject } from 'rxjs'; import { IPC_RENDERER_KEY_PREFIX } from 'nxdb-old/src/plugins/electron/electron-helper'; import { exposeRxStorageRemote, RxStorageRemoteExposeSettings, MessageToRemote } from 'nxdb-old/src/plugins/storage-remote'; export function exposeIpcMainRxStorage( args: { key: string; storage: RxStorage; ipcMain: any; } ) { const channelId = [ IPC_RENDERER_KEY_PREFIX, args.key, ].join('|'); const messages$ = new Subject(); const openRenderers: Set = new Set(); args.ipcMain.on( channelId, (event: any, message: any) => { openRenderers.add(event.sender); if (message) { messages$.next(message); } } ); const send: RxStorageRemoteExposeSettings['send'] = (msg) => { /** * TODO we could improve performance * by only sending the message to the 'correct' sender * and removing senders whose browser window is closed. */ openRenderers.forEach(sender => { sender.send(channelId, msg); }); }; exposeRxStorageRemote({ storage: args.storage, messages$, send }); }