diff --git a/src/kiri-run/engine.js b/src/kiri-run/engine.js index a3cfca8e..ebd2b762 100644 --- a/src/kiri-run/engine.js +++ b/src/kiri-run/engine.js @@ -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; diff --git a/src/kiri-run/worker.js b/src/kiri-run/worker.js index 0944b08f..add39fb1 100644 --- a/src/kiri-run/worker.js +++ b/src/kiri-run/worker.js @@ -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({}); }, diff --git a/src/kiri/api.js b/src/kiri/api.js index 94339f70..704d9297 100644 --- a/src/kiri/api.js +++ b/src/kiri/api.js @@ -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, diff --git a/src/kiri/client.js b/src/kiri/client.js index 959eb1ae..363161fb 100644 --- a/src/kiri/client.js +++ b/src/kiri/client.js @@ -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; diff --git a/src/kiri/consts.js b/src/kiri/consts.js index 750f4296..e06956fa 100644 --- a/src/kiri/consts.js +++ b/src/kiri/consts.js @@ -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 }; diff --git a/src/kiri/init.js b/src/kiri/init.js index 63e407ac..efde270a 100644 --- a/src/kiri/init.js +++ b/src/kiri/init.js @@ -2166,5 +2166,6 @@ function init_lang() { export function run() { init_lang(); + client.start(); return api; } diff --git a/src/webpack/esbuild.config.mjs b/src/webpack/esbuild.config.mjs deleted file mode 100644 index 59061ce8..00000000 --- a/src/webpack/esbuild.config.mjs +++ /dev/null @@ -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(); \ No newline at end of file diff --git a/src/webpack/webpack-three-bundle.js b/src/webpack/webpack-three-bundle.js deleted file mode 100644 index e7832028..00000000 --- a/src/webpack/webpack-three-bundle.js +++ /dev/null @@ -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 -}; \ No newline at end of file diff --git a/src/webpack/webpack-three-esm.js b/src/webpack/webpack-three-esm.js deleted file mode 100644 index bbb6acee..00000000 --- a/src/webpack/webpack-three-esm.js +++ /dev/null @@ -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, - }, -}; \ No newline at end of file