Hi there, I have a question regarding function session.customize(). So today we’ve experienced some very long time getting the result from session.customize() (it was pending, so our app freezes indefinitely). We want to add time limit if the answer wasn’t received after expected time and manually stop session.customize(), so it will not interrupt future customizations. So far I found only close() method.
But it will remove the whole session and a more work to reinitialize the previous state.
I know there is something similar done in shapediver model overview page when it takes more than 10 sec to render.
What options do we have in regards to manually canceling session.customize().
Thanks!
Today morning there was a problem on our shared Rhino 6 system which caused its bandwidth to become very limited. Therefore your computation requests took very long to complete. We are sorry for the inconvenience.
Currently, our viewer’s API does not offer a possibility to specify a timeout when calling session.customize(). However, you can quickly implement such a timeout on your end. ChatGPT does very well explaining how to do this
Q: Please provide sample code on how to implement a timeout for a promise based Javascript function.
A: Sure, here’s an example of how you can implement a timeout for a promise-based function in JavaScript:
function promiseTimeout(ms, promise) {
// Create a new promise that rejects after the specified time limit
const timeout = new Promise((resolve, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(`Timed out in ${ms} ms.`);
}, ms);
});
// Returns a race between our timeout and the passed-in promise
return Promise.race([promise, timeout]);
}
You can use this promiseTimeout function to wrap any promise-based function that you want to set a time limit on. Here’s an example usage:
// A function that resolves after a certain amount of time
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Wrap the wait function with a timeout of 1 second
promiseTimeout(1000, wait(2000))
.then(() => console.log("Promise resolved within time limit."))
.catch(err => console.error(err));
In the example above, the promiseTimeout function is used to wrap the wait function with a time limit of 1 second. The wait function itself takes 2 seconds to resolve, but since it’s wrapped with a timeout, it will be rejected after 1 second with a “Timed out in 1000 ms.” error message. The .catch() block will be executed with the error message as its argument.
Thanks for detailed explanation. It’s working now on our end, but I can see a problem when using automaticSceneUpdate, because once you call session.customize() the node and the scene will be updated automatically regardless of timeout wrapper. So you need to save the state internally in your app, and in case of timeout revert it to the previous one (if you need of course, as in our case).