webworker-polyfill
Web worker has a lot of limitations like you cannot access localStorage
, because you cannot access initiating window.
This library is to fill this gap, providing this missing objects. Only exception is that these APIs will become promised methods.
Usage
In your host context,
const worker = './worker.js'
In your worker context (worker.js
here):
// then do what you want to dolocalStoragelocalStorage // or use ES2017 async/awaitasync { const value = await localStorage console}
How it works
Just postMessage()
and request the host context to access these resources whenever you access these missing APIs. Simple, right?
Supported APIs
- localStorage
- indexDB