xxxxxxxxxx
48
function setup() {
// FAILS to create a shared buffer.
// Server needs COOP and COEP headers set
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements
const sharedBufferSize = 10;
const buffer = new SharedArrayBuffer(
Float32Array.BYTES_PER_ELEMENT * sharedBufferSize
);
// currently we can only send unshareable memory in p5js editor
// const buffer = new Float32Array(sharedBufferSize);
// create some workers
// resolve the p5js generated url for worker.js
const workerUrl = resolveEditorUrl("/worker.js");
const workers= []
for(let i = 0; i < 2; i++)
workers.push(createWorker(workerUrl, { num: i+1, buffer }))
// resolve all workers in parallel
Promise.all(workers)
.then((x) => console.log(x));
}
// need to resolve the p5js-editor url to create the worker
function resolveEditorUrl(filePath) {
for (let previewUrl in objectUrls) {
const editorFilePath = objectUrls[previewUrl];
if (filePath == editorFilePath) {
return previewUrl;
}
}
}
function createWorker(workerUrl, message) {
const worker = new Worker(workerUrl);
return new Promise((s, r) => {
worker.onmessage = (e) => s(e.data);
worker.onerror = r;
worker.postMessage(message);
})
// .finally(() => worker.terminate());
}