diff --git a/apps/cardboard-cutter/index.html b/apps/cardboard-cutter/index.html index 2dbc798..f2c990e 100644 --- a/apps/cardboard-cutter/index.html +++ b/apps/cardboard-cutter/index.html @@ -16,7 +16,7 @@
diff --git a/apps/cardboard-cutter/main.js b/apps/cardboard-cutter/main.js index 8706ff5..a825c8d 100644 --- a/apps/cardboard-cutter/main.js +++ b/apps/cardboard-cutter/main.js @@ -111,7 +111,7 @@ function save(blob, filename) { } function exportModel(format) { - sendCmd('exportData', [{ format }]).then(({ data }) => { + sendCmd('jscadExportData', [{ format }]).then(({ data }) => { console.log('save', fileToRun + '.stl', data) save(new Blob([data], { type: 'text/plain' }), fileToRun + '.stl') }) @@ -123,12 +123,12 @@ const { sendCmd, sendNotify } = initMessaging(worker, handlers) const paramChangeCallback = params => { console.log('params', params) - sendCmd('runMain', [{ params }]) + sendCmd('jscadMain', [{ params }]) } -export const runScript = file => { +export const jscadScript = file => { fileToRun = file.replace(/.*\//, '').replace(/\..*/, '') - sendCmd('runScript', [{ url: file }]).then(result => { + sendCmd('jscadScript', [{ url: file }]).then(result => { console.log('result', result) genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) }) @@ -141,5 +141,5 @@ export const initEngine = async (THREE, elem, workerOptions) => { updateFromCtrl(ctrl) setTheme(theme) - await sendCmd('init', [workerOptions]) + await sendCmd('jscadInit', [workerOptions]) } diff --git a/apps/engine-test/main.js b/apps/engine-test/main.js index 0edc827..992612f 100644 --- a/apps/engine-test/main.js +++ b/apps/engine-test/main.js @@ -124,13 +124,13 @@ document.body.ondrop = async ev => { if (!sw) await initFs() showDrop(false) - workerApi.clearTempCache() + workerApi.jscadClearTempCache() const { alias, script } = await fileDropped(sw, files) projectName = sw.projectName if (alias.length) { - workerApi.init({ alias }) + workerApi.jscadInit({ alias }) } - runScript({ url: sw.fileToRun, base: sw.base }) + jscadScript({ url: sw.fileToRun, base: sw.base }) } catch (error) { setError(error) console.error(error) @@ -169,7 +169,7 @@ function save(blob, filename) { } function exportModel(format) { - workerApi.exportData({ format }).then(({ data }) => { + workerApi.jscadExportData({ format }).then(({ data }) => { console.log('save', fileToRun + '.stl', data) save(new Blob([data], { type: 'text/plain' }), fileToRun + '.stl') }).catch(setError) @@ -178,12 +178,12 @@ window.exportModel = exportModel const paramChangeCallback = async params => { console.log('params changed', params) - let result = await workerApi.runMain({ params }) + let result = await workerApi.jscadMain({ params }) handlers.entities(result) } -const runScript = async ({script, url = './index.js', base, root}) => { - const result = await workerApi.runScript({ script, url, base, root }) +const jscadScript = async ({script, url = './index.js', base, root}) => { + const result = await workerApi.jscadScript({ script, url, base, root }) console.log('result', result) genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) handlers.entities(result) @@ -216,13 +216,13 @@ function trackJobs(jobs) { } } -await workerApi.init({ +await workerApi.jscadInit({ bundles: { '@jscad/modeling': toUrl('./build/bundle.jscad_modeling.js'), }, }) -runScript({script:`const { sphere, geodesicSphere } = require('@jscad/modeling').primitives +jscadScript({script:`const { sphere, geodesicSphere } = require('@jscad/modeling').primitives const { translate, scale } = require('@jscad/modeling').transforms const main = () => [ @@ -246,8 +246,8 @@ async function initFs() { sw = await registerServiceWorker('bundle.fs-serviceworker.js?prefix=/swfs/') sw.defProjectName = 'jscad' sw.onfileschange = files => { - workerApi.clearFileCache({ files }) - if (sw.fileToRun) runScript({ url: sw.fileToRun, base: sw.base }) + workerApi.jscadClearFileCache({ files }) + if (sw.fileToRun) jscadScript({ url: sw.fileToRun, base: sw.base }) } } diff --git a/apps/jscad-web/main.js b/apps/jscad-web/main.js index f478eaa..7b7740e 100644 --- a/apps/jscad-web/main.js +++ b/apps/jscad-web/main.js @@ -85,10 +85,10 @@ async function initFs() { if(files.includes('/package.json')){ reloadProject() }else{ - workerApi.clearFileCache({ files }) + workerApi.jscadClearFileCache({ files }) editor.filesChanged(files) } - if (sw.fileToRun) runScript({ url: sw.fileToRun, base: sw.base }) + if (sw.fileToRun) jscadScript({ url: sw.fileToRun, base: sw.base }) } sw.getFile = path => getFile(path, sw) } @@ -105,7 +105,7 @@ document.body.ondrop = async ev => { await resetFileRefs() if (!sw) await initFs() showDrop(false) - workerApi.clearTempCache() + workerApi.jscadClearTempCache() await fileDropped(sw, files) @@ -122,10 +122,10 @@ async function reloadProject(){ const { alias, script } = await analyzeProject(sw) projectName = sw.projectName if (alias.length) { - workerApi.init({ alias }) + workerApi.jscadInit({ alias }) } let url = sw.fileToRun - runScript({ url, base: sw.base }) + jscadScript({ url, base: sw.base }) editor.setSource(script, url) editor.setFiles(sw.filesToCheck) } @@ -165,7 +165,7 @@ function save(blob, filename) { } const exportModel = async (format, extension) => { - const { data } = (await workerApi.exportData({ format })) || {} + const { data } = (await workerApi.jscadExportData({ format })) || {} if (data) { save(new Blob([data], { type: 'text/plain' }), `${projectName}.${extension}`) console.log('save', `${projectName}.${extension}`, data) @@ -215,11 +215,11 @@ function trackJobs(jobs) { } } -const runScript = async ({ script, url = './jscad.model.js', base = currentBase, root }) => { +const jscadScript = async ({ script, url = './jscad.model.js', base = currentBase, root }) => { currentBase = base loadDefault = false // don't load default model if something else was loaded try{ - const result = await workerApi.runScript({ script, url, base, root, smooth: viewState.smoothRender }) + const result = await workerApi.jscadScript({ script, url, base, root, smooth: viewState.smoothRender }) genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) lastRunParams = result.params handlers.entities(result) @@ -234,9 +234,9 @@ const bundles = { '@jscad/io': toUrl('./build/bundle.jscad_io.js'), } -await workerApi.init({ bundles }) +await workerApi.jscadInit({ bundles }) if (loadDefault) { - runScript({ script: defaultCode, smooth: viewState.smoothRender }) + jscadScript({ script: defaultCode, smooth: viewState.smoothRender }) } let working @@ -252,7 +252,7 @@ const paramChangeCallback = async params => { working = true let result try { - result = await workerApi.runMain({ params, smooth: viewState.smoothRender }) + result = await workerApi.jscadMain({ params, smooth: viewState.smoothRender }) lastRunParams = params } finally { working = false @@ -264,7 +264,7 @@ const paramChangeCallback = async params => { const loadExample = async (source, base = appBase) => { await resetFileRefs() editor.setSource(source, base) - runScript({ script: source, base }) + jscadScript({ script: source, base }) } // Initialize three engine @@ -290,11 +290,11 @@ editor.init( // imported script will be also cached by require/import implementation // it is expected if multiple files require same file/module that first time it is loaded // but for others resolved module is returned - // if not cleared by calling clearFileCache, require will not try to reload the file - await workerApi.clearFileCache({ files: [path] }) - if (sw.fileToRun) runScript({ url: sw.fileToRun, base: sw.base }) + // if not cleared by calling jscadClearFileCache, require will not try to reload the file + await workerApi.jscadClearFileCache({ files: [path] }) + if (sw.fileToRun) jscadScript({ url: sw.fileToRun, base: sw.base }) } else { - runScript({ script }) + jscadScript({ script }) } }, async (script, path) => { @@ -331,7 +331,7 @@ remote.init( (script, url) => { // run remote script editor.setSource(script, url) - runScript({ script, base: url }) + jscadScript({ script, base: url }) welcome.dismiss() }, err => { diff --git a/apps/model-page/index.html b/apps/model-page/index.html index 8c9ea85..6cef2f2 100644 --- a/apps/model-page/index.html +++ b/apps/model-page/index.html @@ -16,7 +16,7 @@
diff --git a/apps/model-page/main.js b/apps/model-page/main.js index 8706ff5..a825c8d 100644 --- a/apps/model-page/main.js +++ b/apps/model-page/main.js @@ -111,7 +111,7 @@ function save(blob, filename) { } function exportModel(format) { - sendCmd('exportData', [{ format }]).then(({ data }) => { + sendCmd('jscadExportData', [{ format }]).then(({ data }) => { console.log('save', fileToRun + '.stl', data) save(new Blob([data], { type: 'text/plain' }), fileToRun + '.stl') }) @@ -123,12 +123,12 @@ const { sendCmd, sendNotify } = initMessaging(worker, handlers) const paramChangeCallback = params => { console.log('params', params) - sendCmd('runMain', [{ params }]) + sendCmd('jscadMain', [{ params }]) } -export const runScript = file => { +export const jscadScript = file => { fileToRun = file.replace(/.*\//, '').replace(/\..*/, '') - sendCmd('runScript', [{ url: file }]).then(result => { + sendCmd('jscadScript', [{ url: file }]).then(result => { console.log('result', result) genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) }) @@ -141,5 +141,5 @@ export const initEngine = async (THREE, elem, workerOptions) => { updateFromCtrl(ctrl) setTheme(theme) - await sendCmd('init', [workerOptions]) + await sendCmd('jscadInit', [workerOptions]) } diff --git a/apps/vue3-jscad/src/App.vue b/apps/vue3-jscad/src/App.vue index b50cb95..321fa66 100644 --- a/apps/vue3-jscad/src/App.vue +++ b/apps/vue3-jscad/src/App.vue @@ -157,10 +157,10 @@ onMounted(async () => { const sw = serviceWorker.value if (sw && sw.fileToRun) { await addToCache(sw.cache, path, script) - await sendCmd('clearFileCache', { files: [path] }) - if (sw.fileToRun) runScript({ url: sw.fileToRun, base: sw.base }) + await sendCmd('jscadClearFileCache', { files: [path] }) + if (sw.fileToRun) jscadScript({ url: sw.fileToRun, base: sw.base }) } else { - runScript({ script }) + jscadScript({ script }) } }) @@ -168,7 +168,7 @@ onMounted(async () => { remote.init((script) => { // run remote script editor.setSource(script) - runScript({ script }) + jscadScript({ script }) }, (err) => { // show remote script error loadDefault = false @@ -176,12 +176,12 @@ onMounted(async () => { }) exporter.init(exportModel) - runScript({ script: defaultCode }) + jscadScript({ script: defaultCode }) }); const loadExample = source => { editor.setSource(source) - runScript({ script: source }) + jscadScript({ script: source }) } const exportModel = async (format, extension) => { @@ -209,7 +209,7 @@ const paramChangeCallback = async (params) => { working.value = true; let result; try { - result = await sendCmdAndSpin('runMain', { params }); + result = await sendCmdAndSpin('jscadMain', { params }); } finally { working.value = false; } @@ -221,10 +221,10 @@ watch(lastParams, (newParams) => { paramChangeCallback(newParams); }); -const runScript = async ({ script, url = './index.js', base, root }) => { - const result = await sendCmdAndSpin('runScript', { script, url, base, root }) +const jscadScript = async ({ script, url = './index.js', base, root }) => { + const result = await sendCmdAndSpin('jscadScript', { script, url, base, root }) loadDefault = false // don't load default model if something else was loaded - console.log('runScript', result) + console.log('jscadScript', result) genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) handlers.entities(result) } @@ -262,14 +262,14 @@ async function sendCmdAndSpin(method, params) { } } -sendCmdAndSpin('init', { +sendCmdAndSpin('jscadInit', { bundles: {// local bundled alias for common libs. '@jscad/modeling': toUrl('./build/bundle.jscad_modeling.js'), '@jscad/io': toUrl('./build/bundle.jscad_io.js'), }, }).then(() => { if (loadDefault) { - runScript({ script: defaultCode }) + jscadScript({ script: defaultCode }) } }) diff --git a/packages/fs-provider/fs-provider.js b/packages/fs-provider/fs-provider.js index 0592e77..100fc7c 100644 --- a/packages/fs-provider/fs-provider.js +++ b/packages/fs-provider/fs-provider.js @@ -217,7 +217,7 @@ export const checkFiles = sw => { }) // TODO clear sw cache - // TODO sendCmd clearFileCache {files} + // TODO sendCmd jscadClearFileCache {files} } requestAnimationFrame(() => checkFiles(sw)) } diff --git a/packages/postmessage/README.md b/packages/postmessage/README.md index ddc701e..a88e1db 100644 --- a/packages/postmessage/README.md +++ b/packages/postmessage/README.md @@ -55,9 +55,8 @@ Here is partial sample of jsdoc definitions for jscad worker @prop {number} convertTime - tim converting script output to gl data @typedef JscadWorker -@prop {String} name -@prop {(options:InitOptions)=>Promise} init -@prop {(options:RunScriptOptions)=>Promise} runScript +@prop {(options:InitOptions)=>Promise} jscadInit +@prop {(options:RunScriptOptions)=>Promise} jscadScript */ ``` @@ -90,21 +89,21 @@ export interface ScriptResponse { } export interface JscadWorker { - async init(options:InitOptions):void, - async init(options:RunScriptOptions):ScriptResponse, + async jscadInit(options:InitOptions):void, + async jscadScript(options:RunScriptOptions):ScriptResponse, } ``` -Manuall calling `init` worker method documented above, requires sending -`worker.postMessage({method:'init', params:[{bundles}]})` just to trigger the method. +Manuall calling `jscadInit` worker method documented above, requires sending +`worker.postMessage({method:'jscadInit', params:[{bundles}]})` just to trigger the method. Then you would also need to handle response, and wrap it all into a promise If you do not want to be fancy with typed code you can just use `initMessaging` and call worker methods using `sendCmd`. ```js const {sendCmd } = initMessaging(worker, handlers, { onJobCount: trackJobs }) -await sendCmd('init',[{ bundles }]) -const result = await sendCmd('runScript', [{ script}]) +await sendCmd('jscadInit',[{ bundles }]) +const result = await sendCmd('jscadScript', [{ script}]) // IDE does not know type of the result ``` @@ -115,8 +114,8 @@ You can then import and use the definition /** @type {JscadWorker} */ const workerApi = messageProxy(worker, handlers, { onJobCount: trackJobs }) -await workerApi.init({ bundles }) -const result = await workerApi.runScript({ script}) +await workerApi.jscadInit({ bundles }) +const result = await workerApi.jscadScript({ script}) // result is now known to be ScriptResponse and you get autocomplete ``` diff --git a/packages/require/src/require.js b/packages/require/src/require.js index 2ea587d..3729509 100644 --- a/packages/require/src/require.js +++ b/packages/require/src/require.js @@ -144,7 +144,7 @@ export const clearFileCache = async ({files}) => { /** * Clear project-specific cache */ -export const clearTempCache = () => { +export const jscadClearTempCache = () => { requireCache.local = {} requireCache.alias = {} } diff --git a/packages/worker/worker.js b/packages/worker/worker.js index 7304b32..9063051 100644 --- a/packages/worker/worker.js +++ b/packages/worker/worker.js @@ -1,6 +1,6 @@ import { JscadToCommon } from '@jscadui/format-jscad' import { initMessaging, withTransferable } from '@jscadui/postmessage' -import { clearFileCache, clearTempCache, readFileWeb, require, requireCache, resolveUrl } from '@jscadui/require' +import { clearFileCache, jscadClearTempCache, readFileWeb, require, requireCache, resolveUrl } from '@jscadui/require' import { exportStlText } from './src/exportStlText.js' import { combineParameterDefinitions, getParameterDefinitionsFromSource } from './src/getParameterDefinitionsFromSource.js' @@ -39,13 +39,12 @@ import { extractPathInfo, readAsArrayBuffer, readAsText } from '../fs-provider/f @typedef JscadWorker -@prop {String} name -@prop {(options:InitOptions)=>Promise} init -@prop {(options:RunMainOptions)=>Promise} runMain -@prop {(options:RunScriptOptions)=>Promise} runScript -@prop {(options:ExportDataOptions)=>Promise} exportData -@prop {(options:ClearFileCacheOptions)=>Promise} clearFileCache -@prop {()=>Promise} clearTempCache +@prop {(options:InitOptions)=>Promise} jscadInit +@prop {(options:RunMainOptions)=>Promise} jscadMain - run the main method of the loaded script +@prop {(options:RunScriptOptions)=>Promise} jscadScript - run a jscad script +@prop {(options:ExportDataOptions)=>Promise} jscadExportData +@prop {(options:ClearFileCacheOptions)=>Promise} jscadClearFileCache +@prop {()=>Promise} jscadClearTempCache */ @@ -71,7 +70,7 @@ export const flatten = arr=>{ return out } -export const init = options => { +export const jscadInit = options => { let { baseURI, alias = [], bundles = {} } = options if (baseURI) globalBase = baseURI @@ -90,7 +89,7 @@ async function readFileFile(file, {bin=false}={}){ } solids = [] -export async function runMain({ params } = {}) { +export async function jscadMain({ params } = {}) { params = {...params} for(let p in params){ if(params[p] instanceof File && importData){ @@ -99,7 +98,7 @@ export async function runMain({ params } = {}) { params[p] = importData.deserialize(info, content) } } - console.log('runMain with params', params) + console.log('jscadMain with params', params) let entities = [] const transferable = [] @@ -120,7 +119,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='jscad.js', base=globalBase, root=base }) => { +const jscadScript = 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) @@ -143,7 +142,7 @@ const runScript = async ({ script, url='jscad.js', base=globalBase, root=base }) // if the main function is the default export if(!main && typeof scriptModule == 'function') main = scriptModule let params = extractDefaults(def) - let out = await runMain({ params }) + let out = await jscadMain({ params }) out.def = def out.params = params return out @@ -153,7 +152,7 @@ const runScript = async ({ script, url='jscad.js', base=globalBase, root=base }) // this is interesting in regards to exporting to stl, and 3mf which actually need vertex data, // and not jcad geometry polygons. So it will be interesting to can give back transferable buffers // instead of re-running conversion. or move export to main thread where the data already is, as it is needed for rendering -const exportData = async (params) => { +const jscadExportData = async (params) => { if(self.exportData) return self.exportData(params) const { format } = params @@ -170,11 +169,11 @@ const exportData = async (params) => { export const currentSolids = ()=>solids -const handlers = { runScript, init, runMain, clearTempCache, clearFileCache, exportData } +const handlers = { jscadScript, jscadInit, jscadMain, jscadClearTempCache, jscadClearFileCache:clearFileCache, jscadExportData } -export const initWorker = (transform, exportData, _importData) => { +export const initWorker = (transform, jscadExportData, _importData) => { if (transform) transformFunc = transform - if(exportData) handlers.exportData = exportData + if(jscadExportData) handlers.jscadExportData = jscadExportData importData = _importData client = initMessaging(self, handlers)