Skip to content

Commit

Permalink
move some srcs, separate extractDefaults func
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Oct 16, 2023
1 parent 83a3a28 commit 9438874
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
File renamed without changes.
19 changes: 19 additions & 0 deletions packages/worker/src/extractDefaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function extractDefaults(def){
const params = {}
def.forEach(({ name, initial, default: def, type, values, captions }) =>{
let val = def === undefined ? initial : def
if(type === 'choice' && values.indexOf(v=>v === val) === -1){
// it is supported for choice to use default value from captions also
// but script will need the matching value
for(let i=0; i<captions.length; i++){
if(captions[i] === val){
val = values[i]
break;
}
}
if(val === undefined) val = values[0]
}
params[name] = val
})
return params
}
23 changes: 4 additions & 19 deletions packages/worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { JscadToCommon } from '@jscadui/format-jscad'
import { initMessaging, withTransferable } from '@jscadui/postmessage'
import { clearFileCache, clearTempCache, readFileWeb, require, requireCache, resolveUrl } from '@jscadui/require'

import { exportStlText } from './exportStlText.js'
import { combineParameterDefinitions, getParameterDefinitionsFromSource } from './getParameterDefinitionsFromSource.js'
import { exportStlText } from './src/exportStlText.js'
import { combineParameterDefinitions, getParameterDefinitionsFromSource } from './src/getParameterDefinitionsFromSource.js'
import { extractDefaults } from './src/extractDefaults.js'

let main
self.JSCAD_WORKER_ENV = {}
Expand Down Expand Up @@ -78,23 +79,7 @@ const runScript = async ({ script, url, base=globalBase, root=base }) => {
const fromSource = getParameterDefinitionsFromSource(script)
def = combineParameterDefinitions(fromSource, await scriptModule.getParameterDefinitions?.())
main = scriptModule.main
const params = {}
def.forEach(({ name, initial, default: def, type, values, captions }) =>{
let val = def === undefined ? initial : def
if(type === 'choice' && values.indexOf(v=>v === val) === -1){
// it is supported for choice to use default value from captions also
// but script will need the matching value
for(let i=0; i<captions.length; i++){
if(captions[i] === val){
val = values[i]
break;
}
}
if(val === undefined) val = values[0]
}
params[name] = val
})
await runMain({ params })
await runMain({ params: extractDefaults(def) })
return {def}
}

Expand Down

0 comments on commit 9438874

Please sign in to comment.