Synchronously starting a new Thread? #1019
-
|
As mentioned in the documentation and in a few discussions & issues: you shouldn't run a coroutine-method in a JavaScript callback. You would just get the In a few issues, like #941, the suggested workaround for this is like the following: button.onClick(e -> {
var thread = new Thread(() -> doSomething());
thread.start();
});Pretty easy to do. But let's say you would want the So could there be a way to start a new thread immediately/synchronously, so it could at least handle the event before it does anything "asynchronously"? The way I see it, in teavm_globals.setTimeout(() => {
$rt_threadStarter(teavm_javaMethod("org.teavm.platform.Platform",
"launchThread(Lorg/teavm/platform/PlatformRunnable;)V"))(runnable);
}, 0);could there be a way to start it synchronously, in some cases? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
|
Hi. I'm not sure what are you trying to do. You can't cancel event in JavaScript asynchronously. Consider you have something like: button.addEventListener("click", e => {
setTimeout(() => e.preventDefault()
}, 1.0);This also won't work. So the answer is: if you want to call |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for your response, I think my question may have been misunderstood. I'm not asking about calling preventDefault() from an asynchronous context. I understand that's impossible in JavaScript's event model. So that it could behave similar to JavaScript's async/await: button.addEventListener("click", async (e) => {
// This part runs synchronously
e.preventDefault();
// Only after the first await does it become asynchronous
await someAsyncOperation();
});In this pattern, the function runs synchronously until the first await, allowing event manipulation, and only then becomes asynchronous. The issue is that starting a new thread in TeaVM is always asynchronous from the start. My question is: Could TeaVM implement thread creation in a way that mimics this behavior? Instead of always using setTimeout to schedule thread execution (making it fully asynchronous), could there be an option for threads to start executing synchronously (allowing event.preventDefault() to work). |
Beta Was this translation helpful? Give feedback.
-
|
In you case the alternative would be: button.onClick(e -> {
e.preventDefault();
var thread = new Thread(() -> someAsyncOperation());
thread.start();
}); |
Beta Was this translation helpful? Give feedback.
-
|
Anyway, there's no easy way to do what you want. You can try to hack and generate bytecode that calls |
Beta Was this translation helpful? Give feedback.
Anyway, there's no easy way to do what you want. You can try to hack and generate bytecode that calls
java.lang.Thread.runThread()V.