-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpow.js
77 lines (67 loc) · 1.74 KB
/
pow.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
window.finished = true;
window.threshold = '0xFFFFFE00';
window.stopWebGL = async () => {
window.finished = true;
}
window.startNewRace = async () => {
window.startWebGL();
}
window.calcuatePow = async () => {
if(!finished) {
return;
}
finished = false;
window.startWebGL();
}
window.startWebGL = async () => {
const startMs = Date.now();
const hash = document.getElementById('previous').innerText;
document.getElementById('workStatus').innerText = 'Started Get Work.';
window.nanoWebglPow(hash,
(workValue, n) => {
stopRace();
const ms = Date.now() - startMs;
callback(workValue, ms);
},
(n) => {
if (finished) {
const ms = Date.now() - startMs;
callback(null, ms);
return true;
} else {
document.getElementById('workStatus').innerText = 'Calculated ' + n + ' frames...';
return false;
}
},
threshold,
);
};
window.callback = (workValue, ms) => {
// console.log('callback', workValue, ms);
if (workValue !== null) {
console.log('callback', workValue, ms);
document.getElementById('work').innerText = workValue;
document.getElementById('workStatus').innerText = ' Get Work Complete.';
}
};
window.stopRace = () => {
finished = true;
};
window.delay = (time) => {
if (!isNaN(time)) {
if (isFinite(time)) {
return new Promise((resolve) => {
const fn = () => {
resolve();
};
setTimeout(fn, time);
});
}
}
};
window.bytesToHex = (bytes) => {
return Array.prototype.map
.call(bytes, (x) => ('00' + x.toString(16)).slice(-2))
.join('')
.toUpperCase();
};