I'm programming an Electron application, and a few pages are meant to update a table real-time by fetching Json strings from my web API. The part that matters from this is that my table first has to be loaded into the document (HTML), before or after (it doesn't matter) I start a new JavaScript thread. I already need to use the main process for handling button and table events.
The thread is used for continuously fetching the data, and if it detects differences from the currently loaded document, it'll put those changes back onto the server. Right now, I'm having trouble starting a new thread that has full access to preloaded JS functions that are used in Node, Electron, and JQuery.
Here's a list of what I tried and why it went wrong, so maybe you can help me find a solution to these or suggest a new direction (another NPM module maybe?):
Finally, in an attempt to fix the third option that I mentioned, I tried to focus on the ASAR path, mentioned here. That fixed this error:
Uncaught Error: ENOENT, worker\api\exports\electron.js not found
But now I'm at the point where the thread probably starts once I load the page but doesn't log even a simple "Hello" to the console, probably due to that wonderful thread isolation.
TLDR; I need a way to run a second thread in Electron that has full access to predefined objects. This option should hopefully run behind the main process, not stopping the page from loading once it goes into its infinite while(True) loop.
As answered by Darkrum in the comments:
setTimeOut or setInterval is your friend.
After some additional research (comment if I'm wrong here):
These two functions allow for code to be executed "simultaneously". Because Javascript is technically only a single-threaded language, true multithreading doesn't exist. Instead, the JS engine, when processing setTimeout
, will constantly check for updates in the main thread.