-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
renderer.js
57 lines (45 loc) · 1.5 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict'
const { ManyLevelGuest } = require('many-level')
const { pipeline, Duplex, PassThrough, Writable } = require('readable-stream')
async function main () {
// Use database of main process as if it was a regular local database
const db = new ManyLevelGuest({ valueEncoding: 'json' })
// Wrap Electron IPC in a duplex Node.js stream
const ipcStream = wrapIPC()
pipeline(ipcStream, db.createRpcStream(), ipcStream, (err) => {
// Called when streams close, with an optional error
console.log('Disconnected', err)
})
await db.put('example', 'hello world')
console.log(await db.get('example'))
// Expose as global for easy access in devtools console
window.db = db
}
main()
function wrapIPC () {
const handleMessage = (event) => {
if (event.source !== window) return
if (event.origin !== 'file://') return
if (event.data?.src !== 'level') return
readable.write(event.data.message)
}
// Data sent from main process to renderer (via preload script)
const readable = new PassThrough({
construct (callback) {
window.addEventListener('message', handleMessage)
callback()
},
destroy (err, callback) {
window.removeEventListener('message', handleMessage)
callback(err)
}
})
// Data sent from renderer to main process (via preload script)
const writable = new Writable({
write (chunk, _, next) {
window.postMessage({ dst: 'level', message: chunk })
next()
}
})
return Duplex.from({ readable, writable })
}