Skip to content

Commit

Permalink
form main, allow calling module methods, not just worker methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Apr 22, 2024
1 parent bce57f6 commit 8c84282
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions apps/jscad-web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions apps/jscad-web/static/test.api.js
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion packages/postmessage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 10 additions & 6 deletions packages/worker/worker.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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)
}


0 comments on commit 8c84282

Please sign in to comment.