-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwasm-worker.ts
53 lines (49 loc) · 1.76 KB
/
wasm-worker.ts
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
import * as Comlink from 'comlink';
import * as Stats from 'stats.js';
import { Simulation } from './pkg';
export type HandlersWrap = {
handlers: Handlers;
}
export type Handlers = {
sim: Simulation;
numThreads: number;
init: (offscreenCanvas: OffscreenCanvas, stats: Stats, simPanel: Stats.Panel, useDarkMode: boolean) => number;
addBlock: () => number;
reset: () => number;
};
const initHandlers = async (): Promise<Handlers> => {
const rust_wasm = await import('./pkg');
await rust_wasm.default();
const numThreads = navigator.hardwareConcurrency;
let maxSimMs = 0;
// must be included to init rayon thread pool with web workers
await rust_wasm.initThreadPool(numThreads);
return Comlink.proxy({
sim: null,
numThreads: numThreads,
init(offscreenCanvas: OffscreenCanvas, stats: Stats, simPanel: Stats.Panel, useDarkMode: boolean) {
this.sim = new rust_wasm.Simulation(offscreenCanvas, useDarkMode);
const step = () => {
stats.begin(); // collect perf data for stats.js
let simTimeMs = performance.now();
this.sim.step();
simTimeMs = performance.now() - simTimeMs;
this.sim.draw();
simPanel.update(simTimeMs, (maxSimMs = Math.max(maxSimMs, simTimeMs)));
stats.end();
requestAnimationFrame(step);
}
requestAnimationFrame(step);
return this.sim.num_particles;
},
addBlock() {
this.sim.add_block();
return this.sim.num_particles;
},
reset() {
this.sim.reset();
return this.sim.num_particles;
},
});
};
Comlink.expose({ handlers: initHandlers() });