Skip to content

Commit

Permalink
add prefix jscad to worker methods, to avoid future name collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Mar 17, 2024
1 parent bf70cb9 commit 24369ff
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 84 deletions.
4 changes: 2 additions & 2 deletions apps/cardboard-cutter/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div id="root" style="overflow: hidden"></div>
</div>
<script type="module">
import { initEngine, runScript } from './build/main.js'
import { initEngine, jscadScript } from './build/main.js'
// you can define bundles that are loaded by the worker and not bundled width a script
// not bundling @jscad/modeling makes sense as soon as you have more than one script you want to show
// esbuild howto: to bundle all imports except @jscad/modeling use: --external:@jscad/modeling
Expand All @@ -28,7 +28,7 @@
},
}
initEngine(THREE, 'root', workerOptions).then(() => {
runScript('./jscad/sqed/index.js')
jscadScript('./jscad/sqed/index.js')
})
</script>
</body>
Expand Down
10 changes: 5 additions & 5 deletions apps/cardboard-cutter/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand All @@ -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 })
})
Expand All @@ -141,5 +141,5 @@ export const initEngine = async (THREE, elem, workerOptions) => {
updateFromCtrl(ctrl)
setTheme(theme)

await sendCmd('init', [workerOptions])
await sendCmd('jscadInit', [workerOptions])
}
22 changes: 11 additions & 11 deletions apps/engine-test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 = () => [
Expand All @@ -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 })
}
}

Expand Down
34 changes: 17 additions & 17 deletions apps/jscad-web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) => {
Expand Down Expand Up @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions apps/model-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div id="root" style="overflow: hidden"></div>
</div>
<script type="module">
import { initEngine, runScript } from './build/main.js'
import { initEngine, jscadScript } from './build/main.js'
// you can define bundles that are loaded by the worker and not bundled width a script
// not bundling @jscad/modeling makes sense as soon as you have more than one script you want to show
// esbuild howto: to bundle all imports except @jscad/modeling use: --external:@jscad/modeling
Expand All @@ -27,7 +27,7 @@
},
}
initEngine(THREE, 'root', workerOptions).then(() => {
runScript('./jscad/planter.mesh.js')
jscadScript('./jscad/planter.mesh.js')
})
</script>
</body>
Expand Down
10 changes: 5 additions & 5 deletions apps/model-page/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand All @@ -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 })
})
Expand All @@ -141,5 +141,5 @@ export const initEngine = async (THREE, elem, workerOptions) => {
updateFromCtrl(ctrl)
setTheme(theme)

await sendCmd('init', [workerOptions])
await sendCmd('jscadInit', [workerOptions])
}
24 changes: 12 additions & 12 deletions apps/vue3-jscad/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,31 @@ 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 })
}
})
menu.init(loadExample);
remote.init((script) => {
// run remote script
editor.setSource(script)
runScript({ script })
jscadScript({ script })
}, (err) => {
// show remote script error
loadDefault = false
setError(err)
})
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) => {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 })
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/fs-provider/fs-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const checkFiles = sw => {
})

// TODO clear sw cache
// TODO sendCmd clearFileCache {files}
// TODO sendCmd jscadClearFileCache {files}
}
requestAnimationFrame(() => checkFiles(sw))
}
Expand Down
Loading

0 comments on commit 24369ff

Please sign in to comment.