web-worker

Spawning a Shared Worker in a Dedicated Worker

那年仲夏 提交于 2019-12-02 00:16:51
I'm playing around with WebWorkers. Somehow I had the idea to let the different instances of a page know when another one is closed. Therefore I wrote a Shared Worker and it works fine. But now I want a Dedicated Worker to act as an interface to the Shared Worker. So that expensive actions in the UI won't affect the continous communication with the Shared Worker. But I get the error, SharedWorker was not defined. An idea would be to use MessageChannel, but I want it to run at least in Firefox and Chrome and as far I know, Firefox still doesn't have a working implementation of MessageChannel.

How to allow Web Workers to receive new data while it still performing computation?

点点圈 提交于 2019-12-02 00:08:41
I want to sort an array, using Web Workers. But this array might receive new values over time, while the worker is still performing the sort function. So my question is, how can I "stop" the sorting computation on the worker after receiving the new item, so it can perform the sort on the array with that item, while still keeping the sorting that was already made? Example: let worker = new Worker('worker.js'); let list = [10,1,5,2,14,3]; worker.postMessage({ list }); setInterval(() => worker.postMessage({ num: SOME_RANDOM_NUM, list }), 100); worker.onmessage = event => { list = event.data.list;

Why is synchronous sleep function not made async by being inside promise?

蓝咒 提交于 2019-12-01 21:58:57
I'm trying to wrap my head around promises, and how JavaScript works with it's queue and event loop etc. I thought that if I put a slow synchronous function inside a promise, that slow sync function would be delegated to the background and I could use .then to deal with it when it was done. function syncSleep(ms){ var end = new Date().getTime() + ms; var start = new Date().getTime(); while (start < end) { start = new Date().getTime(); } } function p() { return new Promise(function(resolve) { syncSleep(5000); resolve("syncSleep done!"); }); } p().then( function(s) { var div = document

HTML 5 webworkers with multiple arguments

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 18:04:12
I just got into HTML5 webworkers and now I want to pass multiple arguments to my worker. I have this in my page: var username = document.getElementById("username").value; var server_url = 'localhost'; w.postMessage(username,server_url); and this in my worker: var username = ''; var server_url = ''; onmessage = function (e,f) { username = e.data; server_url = f.data; } console.log(username); console.log(server_url); and when I open it the page which calls the worker in the browser: Uncaught TypeError: Failed to execute 'postMessage' on 'Worker': The 2nd argument is neither an array, nor does it

Posting a message to a web worker while it is still running

倖福魔咒の 提交于 2019-12-01 17:55:33
Say we have a web worker referring to a file called "worker.js". We use the worker to execute a function in "worker.js" that does some lengthy operation. We call post the respective message to the worker and proceed in the main thread. However, before the worker has finished doing its initial work, the main thread posts another message to it. My question: Will the worker continue with our time-taking function and only process the newly posted message once finished or will it interrupt its current operation until the new one has been completed? I've tried out the following code in Google Chrome

Parallel programming / Synchronization using JavaScript Web Workers

橙三吉。 提交于 2019-12-01 17:48:12
问题 Are there any synchronization primitives like Barriers , Semaphors , Locks , Monitors , ... available in JavaScript / Web Workers or is there some library available empowering me to make use of such things (I'm thinking of something like java.util.concurrent in Java)? Do Workers have obscure properties which differentiate them from Threads (can they share memory with the main thread, for example)? Is there some kind of limit how many workers can be spawned (like, for security reasons or

Access localStorage from service worker

百般思念 提交于 2019-12-01 17:36:18
I want to periodically call an API from my service worker to send data stored in the localStorage. This data will be produced and saved in localStorage when a user browses my website. Consider it something like saving stats in localStorage and sending it periodically through the service worker. How should I do this? I understand that I can't access localStorage from service worker and will have to use the postMessage API. Any help would be highly appreciated. GibboK You cannot access localStorage (and also sessionStorage) from a webworker process, they result will be undefined , this is for

Sharing variables between web workers? [global variables?]

梦想的初衷 提交于 2019-12-01 16:00:59
Is there any way for me to share a variable between two web workers? (Web workers are basically threads in Javascript) In languages like c# you have: public static string message = ""; static void Main() { message = "asdf"; new Thread(mythread).Run(); } public static void mythread() { Console.WriteLine(message); //outputs "asdf" } I know thats a bad example, but in my Javascript application, I have a thread doing heavy computations that can be spread across multiple threads [since I have a big chunk of data in the form of an array. All the elements of the array are independent of each other.

Access localStorage from service worker

浪尽此生 提交于 2019-12-01 15:47:34
问题 I want to periodically call an API from my service worker to send data stored in the localStorage. This data will be produced and saved in localStorage when a user browses my website. Consider it something like saving stats in localStorage and sending it periodically through the service worker. How should I do this? I understand that I can't access localStorage from service worker and will have to use the postMessage API. Any help would be highly appreciated. 回答1: You cannot access

Converting Javascript 2d arrays to ArrayBuffer

放肆的年华 提交于 2019-12-01 12:12:20
I'm trying to use Web Workers to process large volumes of data, and when passing data back to the main thread for display, I would like to use a transferable object to reduce the impact on the UI thread. The procedure currently results in a multi dimensional array that can also contain objects. For instance: [{foo: [{bar: "Alice", car: 23, dab: [2, 3, 5]}], faa: [{moo: {a: [2,3], b: [4,5]} }, {moo: {a: [6,7], b: [8,9]} }]}, {foo: [{bar: "John", car: 33, dab: [6, 7, 1]}], faa: [{moo: {a: [5,5], b: [9,2]} }, {moo: {a: [7,7], b: [4,2]} }]}, ...] I have seen this string conversion post, but again,