Skip to content

Commit

Permalink
update engine test to new worker
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Mar 12, 2024
1 parent 5079ec1 commit 322328e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
64 changes: 35 additions & 29 deletions apps/engine-test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -167,42 +169,60 @@ 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]
setViewerScene((model = entities))
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 = () => [
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion apps/vue3-jscad/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/postmessage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 322328e

Please sign in to comment.