From 322328ef4ad8e7a0ad62e29028113210f86b75b8 Mon Sep 17 00:00:00 2001 From: Davor Hrg Date: Tue, 12 Mar 2024 18:00:19 +0100 Subject: [PATCH] update engine test to new worker --- apps/engine-test/main.js | 64 +++++++++++++++++++---------------- apps/vue3-jscad/package.json | 2 +- package-lock.json | 6 ++-- packages/postmessage/index.js | 2 +- packages/worker/worker.js | 2 +- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/apps/engine-test/main.js b/apps/engine-test/main.js index f46ade6..8aacdb6 100644 --- a/apps/engine-test/main.js +++ b/apps/engine-test/main.js @@ -3,7 +3,7 @@ import { JscadToCommon } from '@jscadui/format-jscad' import { Gizmo } from '@jscadui/html-gizmo' import { OrbitControl, OrbitState, closerAngle, getCommonRotCombined } from '@jscadui/orbit' import { genParams } from '@jscadui/params' -import { initMessaging } from '@jscadui/postmessage' +import { initMessaging, messageProxy } from '@jscadui/postmessage' import { makeAxes, makeGrid } from '@jscadui/scene' import * as themes from '@jscadui/themes' @@ -16,6 +16,8 @@ import { availableEngines, availableEnginesList } from './src/availableEngines' import { CurrentUrl } from './src/currentUrl' import { EngineState } from './src/engineState' +/** @typedef {import('@jscadui/worker').JscadWorker} JscadWorker*/ + const theme = themes.light const { subtract } = booleans const { translate } = transforms @@ -122,7 +124,7 @@ document.body.ondrop = async ev => { if (!sw) await initFs() showDrop(false) - sendCmd('clearTempCache', [{}]) + workerApi.clearTempCache() const { alias, script } = await fileDropped(sw, files) projectName = sw.projectName if (alias.length) { @@ -167,14 +169,29 @@ function save(blob, filename) { } function exportModel(format) { - sendCmd('exportData', { format }).then(({ data }) => { + workerApi.exportData({ format }).then(({ data }) => { console.log('save', fileToRun + '.stl', data) save(new Blob([data], { type: 'text/plain' }), fileToRun + '.stl') }).catch(setError) } window.exportModel = exportModel +const paramChangeCallback = async params => { + console.log('params changed', params) + let result = await workerApi.runMain({ params }) + handlers.entities(result) +} + +const runScript = async ({script, url = './index.js', base, root}) => { + const result = await workerApi.runScript({ script, url, base, root }) + console.log('result', result) + genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) + handlers.entities(result) +} + +/** @type {JscadWorker} */ const worker = new Worker('./build/bundle.worker.js') +const workerApi = messageProxy(worker, handlers, { onJobCount: trackJobs }) const handlers = { entities: ({ entities }) => { if (!(entities instanceof Array)) entities = [entities] @@ -182,27 +199,30 @@ const handlers = { setError(undefined) }, } -const { sendCmd, sendNotify } = initMessaging(worker, handlers) const spinner = byId('spinner') -async function sendCmdAndSpin(method, params){ - spinner.style.display = 'block' - try{ - return await sendCmd(method, params) - }catch(error){ - setError(error) - throw error - }finally{ +let firstJobTimer + +function trackJobs(jobs) { + if (jobs === 1) { + // do not show spinner for fast renders + firstJobTimer = setTimeout(() => { + spinner.style.display = 'block' + }, 300) + } + if (jobs === 0) { + clearTimeout(firstJobTimer) spinner.style.display = 'none' } } -sendCmdAndSpin('init', [{ +await workerApi.init({ bundles: { '@jscad/modeling': toUrl('./build/bundle.jscad_modeling.js'), }, -}]).then(()=>{ - runScript({script:`const { sphere, geodesicSphere } = require('@jscad/modeling').primitives +}) + +runScript({script:`const { sphere, geodesicSphere } = require('@jscad/modeling').primitives const { translate, scale } = require('@jscad/modeling').transforms const main = () => [ @@ -220,20 +240,6 @@ sendCmdAndSpin('init', [{ module.exports = { main }` }) -}) - -const paramChangeCallback = async params => { - console.log('params changed', params) - let result = await sendCmdAndSpin('runMain', [{ params }]) - handlers.entities(result) -} - -const runScript = async ({script, url = './index.js', base, root}) => { - const result = await sendCmdAndSpin('runScript', [{ script, url, base, root }]) - console.log('result', result) - genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) - handlers.entities(result) -} let sw async function initFs() { diff --git a/apps/vue3-jscad/package.json b/apps/vue3-jscad/package.json index b881156..c0a83a1 100644 --- a/apps/vue3-jscad/package.json +++ b/apps/vue3-jscad/package.json @@ -13,7 +13,7 @@ "@jscadui/html-gizmo": "^0.1.0", "@jscadui/orbit": "^0.1.0", "@jscadui/params": "^0.1.0", - "@jscadui/postmessage": "^0.1.0", + "@jscadui/postmessage": "^0.2.0", "vue": "^3.3.11" }, "devDependencies": { diff --git a/package-lock.json b/package-lock.json index 107a243..f89684e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -847,7 +847,7 @@ "@jscadui/html-gizmo": "^0.1.0", "@jscadui/orbit": "^0.1.0", "@jscadui/params": "^0.1.0", - "@jscadui/postmessage": "^0.1.0", + "@jscadui/postmessage": "^0.2.0", "vue": "^3.3.11" }, "devDependencies": { @@ -15536,7 +15536,7 @@ }, "packages/postmessage": { "name": "@jscadui/postmessage", - "version": "0.1.0", + "version": "0.2.0", "license": "MIT", "devDependencies": { "@trivago/prettier-plugin-sort-imports": "~3.3.0", @@ -28165,7 +28165,7 @@ "@jscadui/html-gizmo": "^0.1.0", "@jscadui/orbit": "^0.1.0", "@jscadui/params": "^0.1.0", - "@jscadui/postmessage": "^0.1.0", + "@jscadui/postmessage": "^0.2.0", "@vitejs/plugin-vue": "^4.5.2", "sass": "^1.69.6", "vite": "^5.0.10", diff --git a/packages/postmessage/index.js b/packages/postmessage/index.js index 9fcf95a..6b042b7 100644 --- a/packages/postmessage/index.js +++ b/packages/postmessage/index.js @@ -137,7 +137,7 @@ export const initMessaging = (_self, handlers, {onJobCount}={}) => { * @param {*} handlers * @returns {object} */ -export const messageProxy = (_self, handlers, {sender, onJobCount}) => { +export const messageProxy = (_self, handlers, {sender, onJobCount}={}) => { const { sendCmd, sendNotify, getRpcJobCount} = sender || initMessaging(_self, handlers,{onJobCount}) return new Proxy({ diff --git a/packages/worker/worker.js b/packages/worker/worker.js index 7162f29..27a1c9a 100644 --- a/packages/worker/worker.js +++ b/packages/worker/worker.js @@ -120,7 +120,7 @@ export async function runMain({ params } = {}) { const importReg = /import(?:(?:(?:[ \n\t]+([^ *\n\t\{\},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*\{(?:[ \n\t]*[^ \n\t"'\{\}]+[ \n\t]*,?)+\})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t\{\}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/ const exportReg = /export.*from/ -const runScript = async ({ script, url, base=globalBase, root=base }) => { +const runScript = async ({ script, url='jscad.js', base=globalBase, root=base }) => { console.log('run script with base:', base) if(!script) script = readFileWeb(resolveUrl(url, base, root).url)