allow engine to override work/pool urls and control threading

This commit is contained in:
Stewart Allen 2025-07-09 01:53:33 -04:00
commit 50e7e88d95
9 changed files with 44 additions and 235 deletions

View file

@ -22,6 +22,7 @@ class Engine {
widget: { [this.widget.id]: {} }
};
this.listener = () => { };
client.start();
}
load(url) {
@ -55,6 +56,14 @@ class Engine {
});
}
setThreading(bool) {
if (bool) {
client.pool.start();
} else {
client.pool.stop();
}
}
setListener(listener) {
this.listener = listener;
return this;

View file

@ -7,6 +7,7 @@ import '../ext/jszip.js';
import { base } from '../geo/base.js';
import { codec } from '../kiri/codec.js';
import { consts } from '../kiri/consts.js';
import { util } from '../geo/base.js';
import { newPoint } from '../geo/point.js';
import { polygons as POLY } from '../geo/polygons.js';
@ -84,12 +85,12 @@ const minwork = {
return minions.length;
},
start() {
start(poolpath) {
if (minions.length || !concurrent) {
return;
}
for (let i=0; i < concurrent; i++) {
let minion = new Worker(`/lib/kiri-run/minion.js`, { type: 'module' });
let minion = new Worker(poolpath || consts.PATHS.pool, { type: 'module' });
minion.onerror = (error) => {
debug({ MINION_ERROR: error });
error.preventDefault();
@ -102,7 +103,7 @@ const minwork = {
minion.postMessage({ cmd: "label", name: `#${i}` });
minions.push(minion);
}
console.log(`kiri | init pool | ${version || "rogue"} | ${concurrent + 1}`);
console.log(`kiri | init pool | ${version || "rogue"} | ${concurrent}`);
},
stop() {
@ -261,7 +262,7 @@ console.log(`kiri | init work | ${version || "rogue"}`);
// code is running in the worker / server context
const dispatch = {
pool_start(data, send) {
minwork.start();
minwork.start(data.url);
send.done({});
},

View file

@ -6,7 +6,7 @@ import STACKS from './stacks.js';
import { broker } from '../moto/broker.js';
import { client as work } from './client.js';
import { consts, COLOR as color, LISTS as lists, MODES, VIEWS } from './consts.js';
import { consts, COLOR as color, LISTS as lists, MODES, VIEWS, PATHS } from './consts.js';
import { device, devices } from './devices.js';
import { catalog, dialog, event, group, help, hide, image } from './main.js';
import { modal, mode, probe, process, show, space, util, view } from './main.js';
@ -130,7 +130,7 @@ export const api = {
clone,
color,
conf: settings.conf,
const: { LANG, LISTS: lists, LOCAL, MODES, SETUP, SECURE, STACKS, VIEWS },
const: { LANG, LOCAL, SETUP, SECURE, STACKS, ...consts },
devel,
device,
devices,

View file

@ -11,7 +11,9 @@ let debug = self.debug === true,
running = {},
worker = null,
minions = false,
restarting = false;
restarting = false,
workpath,
poolpath;
/**
* @param {Function} fn name of function in kiri.worker
@ -45,10 +47,20 @@ function send(fn, data, onreply, zerocopy) {
export const client = {
send: send,
setWorkPath(path) {
workpath = path;
return client;
},
setPoolPath(path) {
poolpath = path;
return client;
},
pool: {
start() {
minions = true;
send("pool_start", {}, noop);
send("pool_start", { url: poolpath }, noop);
},
stop() {
@ -65,7 +77,7 @@ export const client = {
const blob = new Blob([ work ], { type: 'application/javascript' });
return new Worker(URL.createObjectURL(blob));
} else {
let worker = new Worker(`/lib/kiri-run/worker.js`, { type: 'module' });
let worker = new Worker(workpath || api.const.PATHS.work, { type: 'module' });
worker.onerror = (error) => {
console.log({ WORKER_ERROR: error });
error.preventDefault();
@ -87,6 +99,10 @@ export const client = {
return current > 0;
},
start() {
client.restart();
},
restart() {
// prevent re-entry from cancel callback
if (restarting) {
@ -334,9 +350,6 @@ export const client = {
}
};
// start worker
client.restart();
self.kiri_client = client;
export default client;

View file

@ -141,16 +141,22 @@ const PMODES = {
TOOLS: 2
};
const PATHS = {
work: "/lib/kiri-run/worker.js",
pool: "/lib/kiri-run/minion.js"
};
const SEED = 'kiri-seed';
export let consts = {
PMODES,
COLOR,
LISTS,
MODES,
PATHS,
PMODES,
VIEWS,
SEED,
beltfact: Math.cos(Math.PI / 4)
};
export { COLOR, LISTS, MODES, VIEWS, PMODES, SEED };
export { COLOR, LISTS, MODES, VIEWS, PATHS, PMODES, SEED };

View file

@ -2166,5 +2166,6 @@ function init_lang() {
export function run() {
init_lang();
client.start();
return api;
}

View file

@ -1,160 +0,0 @@
import { build, transform } from 'esbuild';
import fs from 'fs/promises';
import glob from 'fast-glob';
// Get build mode from command line argument
const mode = process.argv[2] || 'dev';
const isProd = mode === 'prod';
console.log(`Building in ${mode} mode...`);
const MESH_OUTFILE = 'src/pack/mesh-main.js';
const MESH_EXTRAS = [ ];
const KIRI_OUTFILE = 'src/pack/kiri-main.js';
const KIRI_EXTRAS = [ ];
async function appendExtraModules(extras, outfile, minify = false) {
const files = await glob(extras);
if (files.length === 0) return;
const transformed = await Promise.all(
files.map(async (file) => {
const code = await fs.readFile(file, 'utf8');
if (!minify) return code;
const result = await transform(code, {
minify: true,
loader: 'js',
target: 'es2020',
});
return result.code;
})
);
await fs.appendFile(outfile, transformed.join(''));
console.log(`Appended ${files.length} module(s) to ${outfile}`);
}
const logOverride = {
'duplicate-class-member': 'silent',
'duplicate-object-key': 'silent',
'direct-eval': 'silent'
};
async function buildApp() {
try {
// Bundle mesh main app
await build({
entryPoints: [ 'src/main/mesh.js' ],
bundle: true,
outfile: MESH_OUTFILE,
format: 'esm',
external: ['module'],
sourcemap: !isProd, // true for dev, false for prod
minify: isProd, // false for dev, true for prod
target: 'es2020',
platform: 'browser',
define: {
'process.env.NODE_ENV': `"${mode}"`
},
logOverride
});
appendExtraModules(MESH_EXTRAS, MESH_OUTFILE, isProd);
// Bundle mesh worker
await build({
entryPoints: [ 'src/mesh/work.js' ],
bundle: true,
outfile: 'src/pack/mesh-work.js',
format: 'esm',
external: ['module'],
sourcemap: !isProd, // true for dev, false for prod
minify: isProd, // false for dev, true for prod
target: 'es2020',
platform: 'browser',
define: {
'process.env.NODE_ENV': `"${mode}"`
},
logOverride
});
// Bundle kiri main app
await build({
entryPoints: [ 'src/main/kiri.js' ],
bundle: true,
outfile: KIRI_OUTFILE,
format: 'esm',
external: ['module'],
sourcemap: !isProd, // true for dev, false for prod
minify: isProd, // false for dev, true for prod
target: 'es2020',
platform: 'browser',
define: {
'process.env.NODE_ENV': `"${mode}"`
},
logOverride
});
appendExtraModules(KIRI_EXTRAS, KIRI_OUTFILE, isProd);
// Bundle kiri worker
await build({
entryPoints: [ 'src/kiri-run/worker.js' ],
bundle: true,
outfile: 'src/pack/kiri-work.js',
format: 'esm',
external: ['module'],
sourcemap: !isProd, // true for dev, false for prod
minify: isProd, // false for dev, true for prod
target: 'es2020',
platform: 'browser',
define: {
'process.env.NODE_ENV': `"${mode}"`
},
logOverride
});
// Bundle kiri minion
await build({
entryPoints: [ 'src/kiri-run/minion.js' ],
bundle: true,
outfile: 'src/pack/kiri-pool.js',
format: 'esm',
external: ['module'],
sourcemap: !isProd, // true for dev, false for prod
minify: isProd, // false for dev, true for prod
target: 'es2020',
platform: 'browser',
define: {
'process.env.NODE_ENV': `"${mode}"`
},
logOverride
});
// Bundle kiri engine
await build({
entryPoints: [ 'src/kiri-run/engine.js' ],
bundle: true,
outfile: 'src/pack/kiri-eng.js',
format: 'esm',
external: ['module'],
sourcemap: !isProd, // true for dev, false for prod
minify: isProd, // false for dev, true for prod
target: 'es2020',
platform: 'browser',
define: {
'process.env.NODE_ENV': `"${mode}"`
},
logOverride
});
console.log(`Build completed successfully in ${mode} mode!`);
} catch (error) {
console.error('Build failed:', error);
process.exit(1);
}
}
buildApp();

View file

@ -1,23 +0,0 @@
import * as THREE from "../../node_modules/three/build/three.module.js";
import * as SVGLoader from "../../node_modules/three/examples/jsm/loaders/SVGLoader.js";
import * as BufferGeometryUtils from "../../node_modules/three/examples/jsm/utils/BufferGeometryUtils.js";
import { Line2 } from '../../node_modules/three/examples/jsm/lines/Line2.js';
import { LineMaterial } from '../../node_modules/three/examples/jsm/lines/LineMaterial.js';
import { LineGeometry } from '../../node_modules/three/examples/jsm/lines/LineGeometry.js';
import { LineSegments2 } from '../../node_modules/three/examples/jsm/lines/LineSegments2.js';
import { LineSegmentsGeometry } from '../../node_modules/three/examples/jsm/lines/LineSegmentsGeometry.js';
import * as MeshBVHLib from '../../node_modules/three-mesh-bvh/build/index.module.js';
export {
THREE,
SVGLoader,
BufferGeometryUtils,
LineMaterial,
Line2,
LineGeometry,
LineSegments2,
LineSegmentsGeometry,
MeshBVHLib
};

View file

@ -1,38 +0,0 @@
const path = require('path');
module.exports = {
mode: 'production',
entry: path.resolve(__dirname, './webpack-three-bundle.js'),
output: {
path: path.resolve(__dirname, '../ext'),
filename: 'three.js',
library: {
type: 'module'
},
module: true
},
experiments: {
outputModule: true
},
resolve: {
extensions: ['.mjs', '.js'],
},
module: {
rules: [
{
test: /\.m?js$/,
resolve: {
fullySpecified: false,
},
},
],
},
optimization: {
minimize: false
},
performance: {
hints: false,
maxAssetSize: 2 * 1024 * 1024,
maxEntrypointSize: 2 * 1024 * 1024,
},
};