From 677568d9aff4359d115b825887e477a31efab28f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 22 Feb 2016 11:30:36 +0100 Subject: [PATCH] Use `window.requestIdleCallback` in `StateStore.prototype.save` This should alleviate some of the pressure of serializing changes on the main thread. We're assuming that `deadline.timeRemaining()` is high enough to compute the serialization because there's no simple path to serialize state across many `requestIdleCallback`s (e.g. because state might change between two callbacks). --- src/state-store.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/state-store.js b/src/state-store.js index 2175bbe4c..53998d802 100644 --- a/src/state-store.js +++ b/src/state-store.js @@ -24,16 +24,18 @@ class StateStore { } save (key, value) { - return this.dbPromise.then(db => { - if (!db) return + return new Promise((resolve, reject) => { + window.requestIdleCallback(deadline => { + this.dbPromise.then(db => { + if (db == null) resolve() - return new Promise((resolve, reject) => { - var request = db.transaction(['states'], 'readwrite') - .objectStore('states') - .put({value: value, storedAt: new Date().toString()}, key) + var request = db.transaction(['states'], 'readwrite') + .objectStore('states') + .put({value: value, storedAt: new Date().toString()}, key) - request.onsuccess = resolve - request.onerror = reject + request.onsuccess = resolve + request.onerror = reject + }) }) }) }