Created
June 28, 2015 11:01
-
-
Save nolanlawson/c2580896749cd475fb30 to your computer and use it in GitHub Desktop.
Idea for explicit commit control in IndexedDB
This file contains 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
// I can open a transaction in "manual commit" mode | |
var txn = db.transaction('mydb', 'readwrite', {commit: 'manual'}); // default: 'auto' | |
txn.objectStore('people').get('nolan').onsuccess = function (e) { | |
var photoBlob = e.target.result.photo; | |
var reader = new FileReader(); | |
// I can use FileReader inside of this transaction, and it's totally cool | |
reader.onloadend = function (e) { | |
var binaryString = e.target.result; | |
// I can also use fetch() to post the base64 image, and it's totally cool | |
fetch('/photos', { | |
method: 'post', | |
body: btoa(binaryString) | |
}).then(function (response) { | |
var receiptId = response.receiptId; | |
// I got a receipt from the server, now I put() and commit() | |
txn.objectStore('receipts').put('nolan', receiptId); | |
txn.commit(); | |
}).catch(function (err) { | |
txn.rollback(); | |
}); | |
}; | |
reader.onerror = function () { | |
txn.rollback(); | |
}; | |
reader.readAsBinaryString(photoBlob); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment