|
| 1 | +import { Worker } from 'node:worker_threads'; |
| 2 | + |
| 3 | +import { PowerSyncDatabase, SyncStreamConnectionMethod } from '@powersync/node'; |
| 4 | +import { app, BrowserWindow, ipcMain, MessagePortMain } from 'electron'; |
| 5 | +import { AppSchema, BackendConnector } from './powersync'; |
| 6 | +import { default as Logger } from 'js-logger'; |
| 7 | + |
| 8 | +const logger = Logger.get('PowerSyncDemo'); |
| 9 | +Logger.useDefaults({ defaultLevel: logger.WARN }); |
| 10 | + |
| 11 | +// This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Webpack |
| 12 | +// plugin that tells the Electron app where to look for the Webpack-bundled app code (depending on |
| 13 | +// whether you're running in development or production). |
| 14 | +declare const MAIN_WINDOW_WEBPACK_ENTRY: string; |
| 15 | +declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string; |
| 16 | + |
| 17 | +// Handle creating/removing shortcuts on Windows when installing/uninstalling. |
| 18 | +if (require('electron-squirrel-startup')) { |
| 19 | + app.quit(); |
| 20 | +} |
| 21 | + |
| 22 | +const database = new PowerSyncDatabase({ |
| 23 | + schema: AppSchema, |
| 24 | + database: { |
| 25 | + dbFilename: 'test.db', |
| 26 | + openWorker(_, options) { |
| 27 | + return new Worker(new URL('./worker.ts', import.meta.url), options); |
| 28 | + } |
| 29 | + }, |
| 30 | + logger |
| 31 | +}); |
| 32 | + |
| 33 | +const createWindow = (): void => { |
| 34 | + // Create the browser window. |
| 35 | + const mainWindow = new BrowserWindow({ |
| 36 | + height: 600, |
| 37 | + width: 800, |
| 38 | + webPreferences: { |
| 39 | + preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + // and load the index.html of the app. |
| 44 | + mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY); |
| 45 | + |
| 46 | + // Open the DevTools. |
| 47 | + mainWindow.webContents.openDevTools(); |
| 48 | +}; |
| 49 | + |
| 50 | +// This method will be called when Electron has finished |
| 51 | +// initialization and is ready to create browser windows. |
| 52 | +// Some APIs can only be used after this event occurs. |
| 53 | +app.whenReady().then(() => { |
| 54 | + database.connect(new BackendConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP }); |
| 55 | + |
| 56 | + const forwardSyncStatus = (port: MessagePortMain) => { |
| 57 | + port.postMessage(database.currentStatus.toJSON()); |
| 58 | + const unregister = database.registerListener({ |
| 59 | + statusChanged(status) { |
| 60 | + port.postMessage(status.toJSON()); |
| 61 | + }, |
| 62 | + }); |
| 63 | + port.once('close', unregister); |
| 64 | + }; |
| 65 | + |
| 66 | + const forwardWatchResults = (sql: string, args: any[], port: MessagePortMain) => { |
| 67 | + const abort = new AbortController(); |
| 68 | + port.once('close', () => abort.abort()); |
| 69 | + |
| 70 | + database.watchWithCallback(sql, args, { |
| 71 | + onResult(results) { |
| 72 | + port.postMessage(results.rows._array); |
| 73 | + }, |
| 74 | + onError(error) { |
| 75 | + console.error(`Watch ${sql} with ${args} failed`, error); |
| 76 | + }, |
| 77 | + }, {signal: abort.signal}); |
| 78 | + }; |
| 79 | + |
| 80 | + ipcMain.on('port', (portEvent) => { |
| 81 | + const [port] = portEvent.ports; |
| 82 | + port.start(); |
| 83 | + |
| 84 | + port.on('message', (event) => { |
| 85 | + const {method, payload} = event.data; |
| 86 | + switch (method) { |
| 87 | + case 'syncStatus': |
| 88 | + forwardSyncStatus(port); |
| 89 | + break; |
| 90 | + case 'watch': |
| 91 | + const {sql, args} = payload; |
| 92 | + forwardWatchResults(sql, args, port); |
| 93 | + break; |
| 94 | + }; |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + ipcMain.handle('get', async (_, sql: string, args: any[]) => { |
| 99 | + return await database.get(sql, args); |
| 100 | + }); |
| 101 | + ipcMain.handle('getAll', async (_, sql: string, args: any[]) => { |
| 102 | + return await database.getAll(sql, args); |
| 103 | + }); |
| 104 | + createWindow(); |
| 105 | +}); |
| 106 | + |
| 107 | +// Quit when all windows are closed, except on macOS. There, it's common |
| 108 | +// for applications and their menu bar to stay active until the user quits |
| 109 | +// explicitly with Cmd + Q. |
| 110 | +app.on('window-all-closed', () => { |
| 111 | + if (process.platform !== 'darwin') { |
| 112 | + app.quit(); |
| 113 | + } |
| 114 | +}); |
| 115 | + |
| 116 | +app.on('activate', () => { |
| 117 | + // On OS X it's common to re-create a window in the app when the |
| 118 | + // dock icon is clicked and there are no other windows open. |
| 119 | + if (BrowserWindow.getAllWindows().length === 0) { |
| 120 | + createWindow(); |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +// In this file you can include the rest of your app's specific main process |
| 125 | +// code. You can also put them in separate files and import them here. |
0 commit comments