/ interactive_tutorials / hardening_softening
Sign in
interactive_tutorials / hardening_softening
Start the orbits worker on sandboxed sites
A page served with an opaque origin may not start a worker from its own URL, which stopped the whole deck from loading on the vault's site. Start one from an in-memory module that imports it instead, or compute on the main thread.
Jeremy Magland <jmagland@flatironinstitute.org> committed commit f598eb682a88 parent 7212d95 Browse files
1 changed file+31−2
app/js/phase-page.jsmodified+31−2View file
@@ -23,6 +23,7 @@
2323 import { color, DRAW_SCALE, prepare } from './canvas.js';
2424 import { bindSliders, createPresetSelect, physicsOf, sliderSpecs, slidersHTML, volumeHTML } from './controls.js';
2525 import { amplitude, omegaBound, step, X_MAX } from './duffing.js';
26+import { computeOrbitFrom, computeOrbits } from './orbits.js';
2627 import { createInstrumentChain } from './instrument-chain.js';
2728 import { DEFAULT_SCALE, impulseResponse, INSTRUMENTS, SCALES } from './instruments.js';
2829 import { createSound } from './sound.js';
@@ -41,6 +42,34 @@ function tickStep(range) {
4142 return [1, 2, 2.5, 5, 10].map((m) => m * mag).find((s) => s >= raw);
4243 }
4344
45+// A worker running orbits-worker.js. A sandboxed page (one served with an
46+// opaque origin, as on the vault's sites) may not start a worker from its own
47+// URL, so then start one from a small in-memory module that imports it; and if
48+// that fails too, compute on the main thread, through the same messages.
49+function createOrbitsWorker() {
50+ const url = new URL('./orbits-worker.js', import.meta.url);
51+ try {
52+ return new Worker(url, { type: 'module' });
53+ } catch {
54+ try {
55+ const blob = new Blob([`import ${JSON.stringify(url.href)};`], { type: 'text/javascript' });
56+ return new Worker(URL.createObjectURL(blob), { type: 'module' });
57+ } catch {
58+ const fallback = {
59+ onmessage: null,
60+ terminate() { this.onmessage = null; },
61+ postMessage({ id, p, from }) {
62+ setTimeout(() => {
63+ const data = from ? { id, orbit: computeOrbitFrom(p, from) } : { id, ...computeOrbits(p) };
64+ fallback.onmessage?.({ data });
65+ }, 0);
66+ },
67+ };
68+ return fallback;
69+ }
70+ }
71+}
72+
4473 export function createPhasePage({ controls, container, params, sliders = {}, presets, instruments = false }) {
4574 const { names, spec } = sliderSpecs(params, sliders);
4675 const instrumentOptions = Object.entries(INSTRUMENTS).map(([key, { label }]) => `<option value="${key}">${label}</option>`).join('');
@@ -249,7 +278,7 @@ export function createPhasePage({ controls, container, params, sliders = {}, pre
249278 worker = null;
250279 }
251280 if (!worker) {
252- worker = new Worker(new URL('./orbits-worker.js', import.meta.url), { type: 'module' });
281+ worker = createOrbitsWorker();
253282 worker.onmessage = (e) => {
254283 if (e.data.id !== requestId) return;
255284 busy = false;
@@ -286,7 +315,7 @@ export function createPhasePage({ controls, container, params, sliders = {}, pre
286315 stopPlaying();
287316 cancelAdded();
288317 if (!oneWorker) {
289- oneWorker = new Worker(new URL('./orbits-worker.js', import.meta.url), { type: 'module' });
318+ oneWorker = createOrbitsWorker();
290319 oneWorker.onmessage = (e) => {
291320 if (e.data.id !== oneId || !result) return;
292321 oneBusy = false;
↑↓ move↵openescclose