Using Webworkers in angular app (service worker cache access of data in angular-cli)

后端 未结 1 855
遇见更好的自我
遇见更好的自我 2021-01-04 20:00

I wish to run an function (in the background) using a worker. The data comes from a http request. I am using a mock calculation (e.data[0] * e.data[1] * xhrData.arr[3]

1条回答
  •  醉梦人生
    2021-01-04 20:29

    Yes this can be done by a service worker but :

    • The browser can terminate a service worker if it thinks the service worker has finished working. So be sure to wrap everything inside a promise and use event.waitUntil to make sure that the process isn't terminated before it has finished.

      self.addEventListener("message", function(event) { event.waitUntil(test(event)); });

    • The service worker runs on a single thread, so if you do long synchronous operation you'd be slowing every request of your page. Service workers are not meant to do long calculation.

    Yes shared workers can access the cache api. You can access it the same way you do from a service worker by using the global variable caches

    caches.open(cacheName).then(function(cache) {
          cache.match("...")
    });
    

    0 讨论(0)
提交回复
热议问题