-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-worker.js
82 lines (63 loc) · 2.23 KB
/
gen-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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { MAX_LINE_BYTE_LENGTH, range } from './common.js'
const controller = new AbortController()
async function handleEvent(event) {
const { handle } = event.data
const writable = await handle.createWritable()
const writer = writable.getWriter()
const stations = [
'Bulawayo',
'Palembang',
'Hamburg',
'St. John\'s',
'Cracow',
'❤️',
'👩🏻❤️💋👩🏼❤❤️❤️❤️❤️️👩🏻❤️💋👩🏼', // 100 bytes
// '👩🏻❤️💋👩🏼👩🏻❤️💋👩🏼👩🏻❤️💋👩🏼', // over 100
]
const profiles = new Map()
for(const key of stations) {
const min = -999 + Math.floor(Math.random() * (999 - -999 + 1 ))
const max = Math.ceil(min + Math.floor(Math.random() * (999 - min + 1)))
profiles.set(key, {
min, max
})
}
const sensorReading = profile => ((profile.min + Math.floor(Math.random() * (profile.max - profile.min + 1))) / 10).toFixed(1)
const aStation = () => stations[Math.floor(Math.random() * stations.length)]
function sensorData(target) {
const key = aStation()
const profile = profiles.get(key)
const value = sensorReading(profile).toString()
const { read, written } = encoder.encodeInto(key + ';' + value + '\n', target)
return written
}
const encoder = new TextEncoder()
let lastReportedPercent = -1
const LINES_PER_CHUNK = 100
const buffer = new ArrayBuffer(MAX_LINE_BYTE_LENGTH * LINES_PER_CHUNK)
let targetOffset = 0
const max = 1_000_000_000
for await (const r of range(0, max, LINES_PER_CHUNK)) {
targetOffset = 0
for(const _u of range(0, LINES_PER_CHUNK)) {
const target = new Uint8Array(buffer, targetOffset)
const bytesWritten = sensorData(target)
targetOffset += bytesWritten
}
await writer.ready
writer.write(new Uint8Array(buffer, 0, targetOffset))
const percent = Math.trunc(r / max * 100)
if(percent !== lastReportedPercent) {
console.log('percent', percent)
lastReportedPercent = percent
postMessage({ type: 'percent', percent, done: false })
//await delayMs(1)
}
}
await writer.ready
await writer.close()
// self close
close()
}
onerror = event => controller.abort('onerror')
onmessage = event => handleEvent(event).catch(e => console.warn(e))