Promises in jscad file
Can we have promises in jscad code?
I've added it to my version of @jscad/core. I'm working on a opentype.js wrapper to make 3D text. I would be nice to fetch a font async.
Screenshot:
Screenshot, now in my hacky version, of csg-viewer:

@profOnno Wow! This is awesome! Thanks! The support for fonts/text is one of the last missing pieces of JSCAD. This will be very welcomed.
FYI, @lautr3k added support for single-line fonts in the CSG library. See PR #107 I think he may have a few comments and suggestions for this work.
Promises versus Webworkers... the short answer is that data structures cannot be shared between threads (workers) in browsers. @kaosat-dev has already hit this everal times in the new UI, and there’s not much hope of an answer.
The desktop application does not have restrictions on the use of threads, promises, libraries, etc.
This is just the start of a long thread. 😃
I'm not really sharing data structures. MDN has a nice page on that here, search for transferable objects. This 'transferable objects' looks like future technology. I'm keeping my data contained in the worker, and post the resulting CSS/CAG (same way as it works now).
Is it planned to have some kind of ttf text in the core? Or do you want to keep the core smaller, and use something like:
include('ttffont') //exposes libobject
const cags = TtfFont.cagFromString('hello world')
To be honest, I don't think it is a 'Promises versus Webworkers', it is only a way to make cag/csg async. The code changed for Promises works in both 'rebuildSolids' and 'rebuildSolidsInWorker' .
function main () {
return new Promise ( (resolve, reject) => {
setTimeout(() => resolve(cube({size: 20})), 2000);
} )
}
I did a quick scan over the 'Hershey Vector Font' of @lautr3k and it's probably a lot lighter. There is no problem of having this living side by side, That is if your work flow uses the include statements to use which ever library you want.
So for the web version, I guess it would be great to have a library directory next to examples directory with an option to include common libraries (Text, connectors, nuts & bolds). But that is more an other topic.
Already done, but rejected by the team, reason? wait for v2... https://github.com/jscad/csg.js/pull/106
Ai, re-inventing, nice that its already done..
If you use Promises you can however include the font you want, skipping the rest (using the params like params.myFont). At the moment you can't do (or am I missing something):
myFont = include(params.myFont);
The rebuildSolids flow is, to parse the script, replaceIncludes, then hand it over to the Worker. Meaning there is no way to use the font as parameter...? Maybe replaceIncludes can handle the params (when not using Promises)?
The transferable objects looks promising. We will have to look at the possibility of using those more.
For more advanced designs, especially those that need additional libraries, we feel that V2 is the only solution. Adding bandaids to V1 isn’t going to allow enough flexibility, and maintaining both isn’t possible.
Having said that, we want as many ideas as possible for V2. I think @kaosat-dev has a short (ha ha) list of ideas how how to extend V2, and make the framework more extensible for other uses. Would you like to help?
Hi @profOnno , nice idea / prototype! a few things to note :
- please take a look at the basis for V2 , the handling of code will change A LOT,
rebuildSolidsis not working the same way, in order to have actual modules , and no more string based hacks- include() will not be supported any more
- replaceIncludes() will be removed
- a ton of other things will change
- you can see an overview of the workflow in the central file here : https://github.com/jscad/OpenJSCAD.org/blob/a2130d9d3ffde6b71fa2d7cacfc691a097dbeac8/packages/web/src/core/code-evaluation/rebuildGeometry.js (check the imported files in that one to get a better idea of what is done)
- I will create a markdown file/ blog post to clarify what is currently done, what will be done , and what are the limitations / future hopes as far as web workers etc go, things are sadly a lot less trivial as it would seem
@profOnno about returning promises from main() I have nothing against it and I see the benefits, however it means your main could return:
- a single CSG
- an array of CSGs
- a single CAG
- an array of CAGs
- a mix of CSGs & CAGs
- virtual geometry (ok that one will likely replace all the above soonish)
- some of the other new data structures that will be introduced in V2
- promises containing any / all of the above
quite a bit of possibilities : so would your case return either a single or an array of promises ? (our standard way of dealing with outputs is always to convert data coming from main() to arrays) I think we could add support and detect, resolve the promise. Even better would be to just support 'then-ables' , that would pave the way to supporting anything which can be async-awaited.
@lautr3k hey , sorry if we came off as too blunt on this ! We did not mean to diminish your insanely good work ! we are just undermanned, and we cannot realistically upgrade V1 and create V2 at the same time ! you csg.js changes have already been injected into V2 so nothing is lost !
Already done, but rejected by the team, reason? wait for v2... jscad/csg.js#106
Actually, changes were requested, a new pull request was merged, adding support for simplex fonts. These changes are part of V1 and V2.
@profOnno the ability to use promises within V2 designs is not supported yet. However, there is some hope. See #671
FYI, V2 JSCAD exposes the file system, just like running from the command line. Therefore, fonts and images can be loaded and used as part of V2 projects.
I posted an example at the User Group.
https://openjscad.nodebb.com/topic/188/access-to-the-fake-filesystem-in-web-applications