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).
This commit is contained in:
Antonio Scandurra 2016-02-22 11:30:36 +01:00
parent e39d200a77
commit 677568d9af

View File

@ -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
})
})
})
}