Test and fix the new features from the Thread API

This commit is contained in:
Peaceultime 2021-11-22 11:19:56 +01:00
parent 5ae0479e02
commit f1a3ca1f2e
4 changed files with 35 additions and 38 deletions

View File

@ -8,10 +8,10 @@ import Renderer from "./modules/renderer/renderer.mjs";
globalThis.TIMINGS = DEBUG && localStorage.getItem("timings");
globalThis.RELEASE = !DEBUG;
const renderer = new Renderer();
renderer.render();
/*const renderer = new Renderer();
renderer.render();*/
const thread = new Thread("./workers/base.mjs", "backend");
await thread.setup().complete();
const grid = await thread.test_fillArr({x: 20, y: 20, depth: 2, seed: 0, jitter: 3 });
})();
const thread = new Thread("./workers/base.mjs", "backend", true);
await thread.setup();
const grid = await thread.test.fillArr([{x: 20, y: 20, seed: 0, jitter: 3 }]);
})();

View File

@ -1,13 +1,14 @@
import * as Three from "../../libs/three.mjs";
import Thread from "../../utils/workerUtils.mjs";
import { Thread } from "../../utils/workerUtils.mjs";
class Renderer
{
static async init()
{
const renderer_thread = new Thread("../../workers/renderer.mjs", "renderer");
await renderer_thread.setup().complete();
renderer_thread.init(document.createElement("canvas").transferControlToOffscreen(), window.innerWidth, window.innerHeight);
const renderer_thread = new Thread("../../workers/renderer.mjs", "renderer", true);
await renderer_thread.setup();
const offcanvas = document.createElement("canvas").transferControlToOffscreen();
await renderer_thread.render.init([offcanvas, window.innerWidth, window.innerHeight], [offcanvas]);
}
}

View File

@ -55,9 +55,12 @@ function wrapper(thread, mod, fn)
{
return function(args, shared)
{
globalThis.TIMINGS && console.time("Measuring " + mod + "." + fn);
globalThis.TIMINGS && thread._calledMod = mod;
globalThis.TIMINGS && thread._calledFn = fn;
if(globalThis.TIMINGS)
{
console.time("Measuring " + mod + "." + fn);
thread._calledMod = mod;
thread._calledFn = fn;
}
return request(thread, [Constants.REQUEST.CALL, mod, fn, args], shared);
}
}
@ -66,12 +69,12 @@ function onmessage(thread)
{
return function(e) {
const ret = parseReturns(e.data);
globalThis.VERBOSE && console.debug(`Received message from ${thread._name} containing ${ret}`);
globalThis.VERBOSE && console.debug(`Received message from ${thread._name} containing `, ret);
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
if(thread._waiting && ret)
{
globalThis.DEBUG && console.log(`Resolving ${thread._name}`);
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
thread._ret = ret;
thread._resolver(ret);
cleanUp(thread);
@ -81,7 +84,8 @@ function onmessage(thread)
function onerror(thread)
{
return function(e) {
globalThis.DEBUG && console.error(`Received error from ${thread._name} containing ${e}`);
globalThis.DEBUG && console.error(`Received error from ${thread._name} containing `, e);
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
if(thread._waiting)
{
@ -97,9 +101,9 @@ const Thread = Object.freeze(class
* Creates a new Thread loading the linked file. The linked file must follow the Thread expectations.
* You can give it a optionnal name to make the debug easier.
*/
constructor(url, name)
constructor(url, name, mod)
{
this._worker = new Worker(url, {type: "module"});
this._worker = new Worker(url, mod ? {type: "module"} : undefined);
this._name = name || url;
this._worker.onmessage = onmessage(this);
@ -120,23 +124,13 @@ const Thread = Object.freeze(class
this[mod] = {};
for(let i = 0; i < result[mod].length; ++i)
{
const fn = result[mod][i];
this[mod][fn] = wrapper(this, mod, fn);
}
}
}
}
complete()
{
if(this._waiting)
{
return this._promise;
}
else
{
return Promise.resolve(this._ret);
}
}
terminate()
async terminate()
{
await request(this, [Constants.REQUEST.TERMINATE]);
this._worker.terminate();
@ -147,7 +141,7 @@ const Thread = Object.freeze(class
function send(ret)
{
globalThis.VERBOSE && console.debug(`Sending ${ret} to the main thread`);
globalThis.VERBOSE && console.debug(`Sending `, ret, ` back to the main thread`);
postMessage([Constants.STATE.OK, ret]);
}
@ -163,7 +157,7 @@ class Process
}
static onmessage(e)
{
globalThis.VERBOSE && console.debug(`Received ${e.data}`);
globalThis.VERBOSE && console.debug(`Received `, e.data);
if(e && e.data)
{
switch(e.data[0])
@ -207,21 +201,24 @@ class Process
if(!Array.isArray(arr))
arr = [arr];
if(mod in Process._customFn)
throw new Error("This module has already been registered in the process.");
const modObj = {};
for(let i = 0; i < arr.length; ++i)
{
if(typeof arr[i] != 'function')
throw new Error("You can only register functions");
if(mod in Process._customFn)
throw new Error("This module has already been registered in the process.");
const modObj = {};
const name = arr[i].name;
if(name in modObj)
throw new Error("This function name is already registered in the process. Please use a different one.");
else
modObj[name] = arr[i];
}
Process._customFn[mod] = modObj;
}
static cleanUp()
{

View File

@ -5,12 +5,11 @@ import Noise from '../libs/alea.mjs'
function fillArr(settings)
{
const noise = Noise(settings.seed);
const arr = new Float32Array(settings.x * settings.y * settings.depth);
const arr = new Float32Array(settings.x * settings.y * 2);
for(let i = arr.length - 1; i >= 0; --i)
{
const _x = i % settings.x;
const _y = (i - _x) / settings.x;
arr[i] =
}
}