Last active
December 2, 2017 17:18
-
-
Save arackaf/98d6e6707d22fae1fd3666330747b430 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
toolbox.router.post(/graphql/, request => { | |
return fetch(request).then(response => { | |
let respClone = response.clone(); | |
setTimeout(() => { | |
respClone.json().then(resp => { | |
if (resp && resp.data && resp.data.updateBook && resp.data.updateBook.Book) { | |
syncBook(resp.data.updateBook.Book); | |
} | |
}, 5); | |
}); | |
return response; | |
}); | |
}); | |
function syncBook(book) { | |
let open = indexedDB.open("books", 1); | |
open.onsuccess = evt => { | |
let db = open.result; | |
if (db.objectStoreNames.contains("books")) { | |
let tran = db.transaction("books", "readwrite"); | |
let booksStore = tran.objectStore("books"); | |
booksStore.get(book._id).onsuccess = ({ target: { result: bookToUpdate } }) => { | |
["title", "authors", "isbn"].forEach(prop => (bookToUpdate[prop] = book[prop])); | |
booksStore.put(bookToUpdate); | |
}; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment