diff --git a/apps/jscad-web/main.js b/apps/jscad-web/main.js index 7b7740e..3d2cecd 100644 --- a/apps/jscad-web/main.js +++ b/apps/jscad-web/main.js @@ -11,7 +11,7 @@ import { import { Gizmo } from '@jscadui/html-gizmo' import { OrbitControl } from '@jscadui/orbit' import { genParams } from '@jscadui/params' -import { initMessaging, messageProxy } from '@jscadui/postmessage' +import { messageProxy } from '@jscadui/postmessage' import defaultCode from './examples/jscad.example.js' import * as editor from './src/editor.js' @@ -194,7 +194,7 @@ const handlers = { } /** @type {JscadWorker} */ -const workerApi = messageProxy(worker, handlers, { onJobCount: trackJobs }) +const workerApi = globalThis.workerApi = messageProxy(worker, handlers, { onJobCount: trackJobs }) const progress = byId('progress') let jobs = 0 diff --git a/apps/jscad-web/static/test.api.js b/apps/jscad-web/static/test.api.js new file mode 100644 index 0000000..ad2c738 --- /dev/null +++ b/apps/jscad-web/static/test.api.js @@ -0,0 +1,16 @@ +import * as jscad from '@jscad/modeling' + +export const main =({// @jscad-params + mesh,// {type:"file"} + check=false, + bla='test', + bla1='test', // {type:'choice', values:["a","b"]} +})=>{ + return mesh ? mesh : jscad.primitives.sphere() + +} + +export const cube = async ()=>{ + console.log('cube 5') + return jscad.primitives.cube() +} \ No newline at end of file diff --git a/packages/postmessage/index.js b/packages/postmessage/index.js index 045145c..c8c683f 100644 --- a/packages/postmessage/index.js +++ b/packages/postmessage/index.js @@ -148,7 +148,7 @@ export const messageProxy = (_self, handlers, {sender, onJobCount}={}) => { },{ get(target, prop, receiver) { if(prop in target) return target[prop] - if(prop.startsWith('on')){ + if(prop.startsWith('on') && (prop.length == 2 || prop[2] == prop[2].toUpperCase())){ return target[prop] = function(...params){ sendNotify(prop, params) } diff --git a/packages/worker/worker.js b/packages/worker/worker.js index 9063051..d3a431c 100644 --- a/packages/worker/worker.js +++ b/packages/worker/worker.js @@ -1,5 +1,5 @@ import { JscadToCommon } from '@jscadui/format-jscad' -import { initMessaging, withTransferable } from '@jscadui/postmessage' +import { messageProxy, withTransferable } from '@jscadui/postmessage' import { clearFileCache, jscadClearTempCache, readFileWeb, require, requireCache, resolveUrl } from '@jscadui/require' import { exportStlText } from './src/exportStlText.js' @@ -49,9 +49,9 @@ import { extractPathInfo, readAsArrayBuffer, readAsText } from '../fs-provider/f */ let main -self.JSCAD_WORKER_ENV = {} +let scriptModule = {} +globalThis.JSCAD_WORKER_ENV = {} let transformFunc = x => x -let client let globalBase = location.origin let userInstances let importData @@ -126,7 +126,6 @@ const jscadScript = async ({ script, url='jscad.js', base=globalBase, root=base const shouldTransform = url.endsWith('.ts') || script.includes('import') && (importReg.test(script) || exportReg.test(script)) let def = [] - let scriptModule try{ scriptModule = require({url,script}, shouldTransform ? transformFunc : undefined, readFileWeb, base, root, importData) }catch(e){ @@ -170,13 +169,18 @@ const jscadExportData = async (params) => { export const currentSolids = ()=>solids const handlers = { jscadScript, jscadInit, jscadMain, jscadClearTempCache, jscadClearFileCache:clearFileCache, jscadExportData } - +// allow main thread to call worker methods and any method from the loaded script +const handlersProxy = new Proxy(handlers, { + get(target, prop, receiver) { + return target[prop] || scriptModule[prop] + } +}) export const initWorker = (transform, jscadExportData, _importData) => { if (transform) transformFunc = transform if(jscadExportData) handlers.jscadExportData = jscadExportData importData = _importData - client = initMessaging(self, handlers) + JSCAD_WORKER_ENV.client = messageProxy(self, handlersProxy) }