Last active
December 2, 2017 04:13
-
-
Save arackaf/0ff0fbcbd200df555a69b4f0759f555f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function syncImages(db) { | |
let tran = db.transaction("books"); | |
let booksStore = tran.objectStore("books"); | |
let idx = booksStore.index("imgSync"); | |
let booksCursor = idx.openCursor(0); | |
let booksToUpdate = []; | |
booksCursor.onsuccess = evt => { | |
let cursor = evt.target.result; | |
if (!cursor) return runIt(); | |
let book = cursor.value; | |
booksToUpdate.push({ _id: book._id, smallImage: book.smallImage }); | |
cursor.continue(); | |
}; | |
async function runIt() { | |
if (!booksToUpdate.length) return; | |
for (let book of booksToUpdate) { | |
try { | |
await preCacheBookImage(book); | |
let tran = db.transaction("books", "readwrite"); | |
let booksStore = tran.objectStore("books"); | |
await new Promise(res => { | |
let req = booksStore.get(book._id); | |
req.onsuccess = ({ target: { result: bookToUpdate } }) => { | |
bookToUpdate.imgSync = 1; | |
booksStore.put(bookToUpdate); | |
res(); | |
}; | |
req.onerror = () => res(); | |
}); | |
} catch (er) { | |
console.log("ERROR", er); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment