forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
65 lines (56 loc) · 1.75 KB
/
worker.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
58
59
60
61
62
63
64
65
'use strict'
import IPFS from 'ipfs'
import { Server, IPFSService } from 'ipfs-message-port-server'
const main = async () => {
// start listening to all the incoming connections (browsing contexts that
// which run new SharedWorker...)
// Note: It is important to start listening before we do any await to ensure
// that connections aren't missed while awaiting.
const connections = listen(self, 'connect')
// Start an IPFS node & create server that will expose it's API to all clients
// over message channel.
const ipfs = await IPFS.create()
const service = new IPFSService(ipfs)
const server = new Server(service)
// connect every queued and future connection to the server.
for await (const event of connections) {
const port = event.ports[0]
if (port) {
server.connect(port)
}
}
}
/**
* Creates an AsyncIterable<Event> for all the events on the given `target` for
* the given event `type`. It is like `target.addEventListener(type, listener, options)`
* but instead of passing listener you get `AsyncIterable<Event>` instead.
* @param {EventTarget} target
* @param {string} type
* @param {AddEventListenerOptions} options
*/
const listen = function (target, type, options) {
const events = []
let resume
let ready = new Promise(resolve => (resume = resolve))
const write = event => {
events.push(event)
resume()
}
const read = async () => {
await ready
ready = new Promise(resolve => (resume = resolve))
return events.splice(0)
}
const reader = async function * () {
try {
while (true) {
yield * await read()
}
} finally {
target.removeEventListener(type, write, options)
}
}
target.addEventListener(type, write, options)
return reader()
}
main()