-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerContext.mjs
70 lines (63 loc) · 2.09 KB
/
workerContext.mjs
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
// Add some randomnes to have different checkpoints per worker
const STOP_CHECK_INTERVAL = 200 + Math.floor(Math.random() * 100)
export class WorkerContext {
constructor() {
this.currentJob = null
this.lastStoppedJob = null
this.nextStopCheck = 0
this.timeSpendInStopCheck = 0
this.resetStats()
}
initTask(jobToken) {
this.timeSpendInStopCheck = 0
this.currentJob = jobToken
this.nextStopCheck = performance.now() + STOP_CHECK_INTERVAL
}
resetStats() {
this.stats = {
timeSpendInHighPrecision: 0,
timeSpendInLowPrecision: 0,
numberOfHighPrecisionPoints: 0,
numberOfLowPrecisionPoints: 0,
numberOfLowPrecisionMisses: 0,
timeLostOnLowPrecisionMisses: 0,
errorOffsetsPos: [],
errorOffsetsNeg: []
}
}
shouldStop() {
const currentJobToken = this.currentJob
const ts = performance.now()
let shouldStop = false
if (ts > this.nextStopCheck) {
shouldStop = this._shouldStop(currentJobToken)
this.nextStopCheck = performance.now() + STOP_CHECK_INTERVAL
}
this.timeSpendInStopCheck += performance.now() - ts
return shouldStop
}
_shouldStop(jobToken) {
if (!jobToken) {
return false
}
let xhr = new XMLHttpRequest();
xhr.open("GET", jobToken, /* async= */false);
try {
xhr.send(null);
} catch (e) {
return true // request failed, URL has been revoked
}
return false // URL is still valid, we can continue
}
}
// Inserts the smooth value in the smooth buffer if any and returns the (potentially updated) iter value
export function smoothen(smooth, offset, iter, zq) {
if (smooth && iter > 3) {
let log_zn = Math.log(zq) / 2
let nu = Math.log(log_zn / Math.log(2)) / Math.log(2)
iter = Math.floor(iter + 1 - nu)
nu = nu - Math.floor(nu)
smooth[offset] = Math.floor(255 - 255 * nu)
}
return iter
}