electric-sql / electric-sql/pglite
pgliteworker with electron
- Dominant language
- TypeScript
- Stars
- 16k
- Forks
- 442
- Avg merge
- 20h 19m
- Merged PRs (30d)
- 7
Description
currently trying to create a worker so i can access the "proxy" via different workers.
appearently the worker class mismatches with the source?
because postMessage, addEventListener is not available

more main process logs:
```
(node:34988) UnhandledPromiseRejectionWarning: TypeError: a(...).addEventListener is not a function
at (redacted)\node_modules\@electric-sql\pglite\dist\worker\index.cjs:8:3552
at new Promise ()
at new Ee ((redacted)\node_modules\@electric-sql\pglite\dist\worker\index.cjs:8:3526)
at createDatabaseWorker ((redacted)\out\main\json-IHKDEz9o.js:916:14)
at Object. ((redacted)\out\main\json-IHKDEz9o.js:923:16)
at Module._compile (node:internal/modules/cjs/loader:1484:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1564:10)
at Module.load (node:internal/modules/cjs/loader:1295:32)
at Module._load (node:internal/modules/cjs/loader:1111:12)
at c._load (node:electron/js2c/node_init:2:16955)
```
Source:
```ts
// my-pglite-worker.ts
import { PGlite } from '@electric-sql/pglite'
import { worker } from '@electric-sql/pglite/worker'
import { parentPort } from 'worker_threads'
const port = parentPort
if (!port) throw new Error('IllegalState')
worker({
async init(options) {
const meta = options.meta
// Do something with additional metadata.
// or even run your own code in the leader along side the PGlite
return new PGlite({
dataDir: options.dataDir
})
}
})
```
```ts
// createDatabaseWorker.ts
import { PGliteWorker } from '@electric-sql/pglite/worker'
import { isProduction } from '@shared/config'
import { app } from 'electron'
import path from 'path'
import { Worker } from 'worker_threads'
import PGWorkerPath from './worker?modulePath'
let client: PGliteWorker
const databasePath = isProduction
? path.join(app.getPath('userData'), 'data_app')
: path.resolve(__dirname, '../database')
export default function createDatabaseWorker() {
if (client) return client
const worker = new Worker(PGWorkerPath, { type: 'module' })
client = new PGliteWorker(worker as any, {
dataDir: databasePath
})
return client
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.