Accessing indexedDB in ServiceWorker. Race condition

前端 未结 3 1591
猫巷女王i
猫巷女王i 2021-01-02 08:36

There aren\'t many examples demonstrating indexedDB in a ServiceWorker yet, but the ones I saw were all structured like this:

const request = indexedDB.open(         


        
相关标签:
3条回答
  • 2021-01-02 08:44

    I don't know of anything special about accessing IndexedDB from the context of a service worker via accessing IndexedDB via a controlled page.

    Promises obviously makes your life much easier within a service worker, so I've found using something like, e.g., https://gist.github.com/inexorabletash/c8069c042b734519680c to be useful instead of the raw IndexedDB API. But it's not mandatory as long as you create and manage your own promises to reflect the state of the asynchronous IndexedDB operations.

    The main thing to keep in mind when writing a fetch event handler (and this isn't specific to using IndexedDB), is that if you call event.respondWith(), you need to pass in either a Response object or a promise that resolves with a Response object. As long as you're doing that, it shouldn't matter whether your Response is constructed from IndexedDB entries or the Cache API or elsewhere.

    Are you running into any actual problems with the code you posted, or was this more of a theoretical question?

    0 讨论(0)
  • 2021-01-02 09:02

    Opening the IDB every time the ServiceWorker starts up is unlikely to be optimal, you'll end up opening it even when it isn't used. Instead, open the db when you need it. A singleton is really useful here (see https://github.com/jakearchibald/svgomg/blob/master/src/js/utils/storage.js#L5), so you don't need to open IDB twice if it's used twice in its lifetime.

    The "activate" event is a great place to open IDB and let any "onupdateneeded" events run, as the old version of ServiceWorker is out of the way.

    0 讨论(0)
  • 2021-01-02 09:02

    You can wrap a transaction in a promise like so:

    var tx = db.transaction(scope, mode);
    var p = new Promise(function(resolve, reject) {
      tx.onabort = function() { reject(tx.error); };
      tx.oncomplete = function() { resolve(); };
    });
    

    Now p will resolve/reject when the transaction completes/aborts. So you can do arbitrary logic in the tx transaction, and p.then(...) and/or pass a dependent promise into e.respondWith() or e.waitUntil() etc.

    As noted by other commenters, we really do need to promisify IndexedDB. But the composition of its post-task autocommit model and the microtask queues that Promises use make it... nontrivial to do so without basically completely replacing the API. But (as an implementer and one of the spec editors) I'm actively prototyping some ideas.

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