How to run an unblocking background task in a Meteor/JavaScript client? -
I will run a task on a meteor client which is a hungry resource in the background and is responsible for the interface, in the meantime user functions Makes some math (for example, to find the key numbers described here).
I have tried to follow the suggestions, but the interface is not yet complete until the "freeze" interface.
setTimeout, setInterval and those packages, like my current perspective also do not help:
var workQueue = new PowerQueue (); TaskQueue.add (function (done) {doSomeMath (); // It is still blocked / freezing even until () is not completed;}); DoSomeMath () is running or I'm doing something wrong to make the interface responsive (it also does not seem like you can do a lot in PowerQueue)?
Javascript libraries that solve the problem of asynchronous queuing, assume that the queued function is running concurrently But single-threaded environments such as node.js or your browser. However, in your case you only need more than concurrency - you need to have multi-threaded performance to move your CPU-intensive calculation with your UI thread, note that web workers can only be acquired Is supported, so if you do not care about IE9, keep reading it.
The above articles should be enough to get you started, though it is worth mentioning that the worker script will be required that your application should be kept out of the tree so that it does not bundle. An easy way to do this is to put it inside the public directory. This is a quick example, where my worker calculates the Fibonacci sequence (inefficiently):
public / Fib.JS var fib = Function (n) {if (n & lt; 2) {return 1; } And {return fiber (N-2) + Fib (N-1); }}; Self.addEventListener ('Message', (function (e) {var n = e.data; var results = prohibited (n); self.postMessage (result); self.close ();}), false); customer / app.js meteorite Start (Work (Work) (Var Worker = New Employee ('/ Phb. JS'); Worker. Post Message (40); worker.addEventListener ('Message', Function (e) {console.log (e.data); }, False);}); When the customer starts, it loads the worker and asks to compute the 40th number in the sequence. It takes a few seconds to complete, but your UI should be liable. After the value is returned, it should be printed on the 165580141 console.
Comments
Post a Comment