Skip to content

Commit

Permalink
#55 Remove ManagedWorker class, make OffscreenSkyboxWorker use Change…
Browse files Browse the repository at this point in the history
…Tracker instead.

#55 Change how large unit numbers are represented.
  • Loading branch information
aggregate1166877 committed Dec 4, 2021
1 parent c354f0e commit 8153912
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
6 changes: 4 additions & 2 deletions app/local/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Unit.parsec = {};
// Note that, while this exact value is correct, it's larger than
// Number.MAX_SAFE_INTEGER and will produce bad results in contexts where
// accuracy is needed.
Unit.parsec.inMeters = BigInt('30856775814913673');
Unit.parsec.inMetersBigInt = BigInt('30856775814913673');
Unit.parsec.inMeters = Number(Unit.parsec.inMetersBigInt);
// ^^ factors:
// 1801n, 12269n, 22096469n, 1396457317n

Expand All @@ -30,6 +31,7 @@ Unit.lightYear = {};
// Note that, while this exact value is correct, it's larger than
// Number.MAX_SAFE_INTEGER and will produce bad results in contexts where
// accuracy is needed (we're accurate up to just over 0.9ly).
Unit.lightYear.inMeters = BigInt('9460730472580800');
Unit.lightYear.inMetersBigInt = BigInt('9460730472580800');
Unit.lightYear.inMeters = Number(Unit.lightYear.inMetersBigInt);

export default Unit;
1 change: 1 addition & 0 deletions app/local/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ window.$game = {
// TODO: determine if this has become redundant (we're implementing new positioning methods).
hyperMovement: false,
event: {
offscreenSkyboxReady: new ChangeTracker(),
skyboxLoaded: new ChangeTracker(),
}
};
Expand Down
32 changes: 0 additions & 32 deletions app/managedWorkers/ManagedWorker.js

This file was deleted.

45 changes: 37 additions & 8 deletions app/managedWorkers/OffscreenSkyboxWorker.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import ManagedWorker from './ManagedWorker';
import * as THREE from 'three';
import Unit from '../local/Unit';
import { logBootInfo } from '../local/windowLoadListener';
import ChangeTracker from '../emitters/ChangeTracker';

export default class OffscreenSkyboxWorker extends ManagedWorker {
// TODO: maybe rename to astrometrics worker? It's become more than just a
// skybox machine.
export default class OffscreenSkyboxWorker extends Worker {
constructor() {
// Note: offscreenRenderer.js is made available via Webpack bundling.
super('./build/offscreenSkybox.js');
super('./build/offscreenSkybox.js', { type: 'module' });
this.skyboxCube = null;
this.initComplete = false;
// Just a tad bigger than the Milky Way. Yes, we can do that. In meters.
this.skyboxSize = Number(Unit.parsec.inMeters * BigInt(32768));
this.skyboxSize = Number(Unit.parsec.inMetersBigInt * BigInt(32768));
// Used to uniquely identify each web worker request.
this.ticketCount = 0;

this._collectedImages = [
null, null, null, null, null, null,
Expand All @@ -19,17 +24,41 @@ export default class OffscreenSkyboxWorker extends ManagedWorker {
}

prepListeners() {
this.addWorkerListener('init', ({ error, value }) => {
// Create worker listeners.
this.workerListener = {
init: new ChangeTracker(),
renderFace: new ChangeTracker(),
testHeavyPayload: new ChangeTracker(),
};

// Hook listeners into onmessage.
this.onmessage = (message) => {
const payload = message.data;
const listener = this.workerListener[payload.key];
if (!listener) {
return console.error(
'[OffscreenSkyboxWorker] Nothing to receive', payload.key
);
}
listener.setValue(payload);
};

// Define listener responses.
this.workerListener.init.getEveryChange(({ error, value }) => {
if (this.initComplete) {
return console.error('[OffscreenSkyboxWorker] Init received twice.');
}
this.initComplete = true;
if (error || !value) {
console.error(error, '- got:', value);
}
else {
this.ready = true;
this.generateSkybox();
$game.event.offscreenSkyboxReady.setValue({ when: new Date() });
}
});

this.addWorkerListener('renderFace', ({ value }) => {
this.workerListener.renderFace.getEveryChange(({ value }) => {
const { x, y, z, sideNumber, tag } = value;
if (tag === 'internal cascade') {
// Caution: this is request**Post**AnimationFrame, not
Expand Down Expand Up @@ -57,7 +86,7 @@ export default class OffscreenSkyboxWorker extends ManagedWorker {
}
});

this.addWorkerListener('testHeavyPayload', ({ error, value }) => {
this.workerListener.testHeavyPayload.getEveryChange(({ error, value }) => {
console.log('testHeavyPayload:', value.length, 'KB transferred. Error:', error);
});
}
Expand Down

0 comments on commit 8153912

Please sign in to comment.