-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
35 lines (26 loc) · 1.04 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
import { handleStream as handleStreamStreamStyle } from './stream-style.js'
import { humanByteLength } from './common.js'
import { handleStream as handleStreamByteStyle } from './byte-style.js'
const controller = new AbortController()
async function handleEvent(event) {
const { handle } = event.data
const file = await handle.getFile()
const totalByteLength = file.size
console.log('File Size', humanByteLength(totalByteLength))
const stream = await file.stream()
const signal = controller.signal
const handleStream = (true) ? handleStreamByteStyle : handleStreamStreamStyle
const results = await handleStream(stream, { totalByteLength, signal: controller.signal })
const end = Date.now()
// postMessage({ type: 'percent', done: true })
let totalCount = 0
for(const [ key, value ] of results.entries()) {
totalCount += value.count
postMessage({ type: 'update', key, ...value })
}
console.log(totalCount)
//
close()
}
onerror = event => controller.abort('onerror')
onmessage = event => handleEvent(event).catch(e => console.warn(e))