Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function openDatabase() {
- // If the browser doesn't support service worker,
- // we don't care about having a database
- if (!navigator.serviceWorker) {
- return Promise.resolve();
- }
- // TODO: return a promise for a database called 'wittr'
- // that contains one objectStore: 'wittrs'
- // that uses 'id' as its key
- // and has an index called 'by-date', which is sorted
- // by the 'time' property
- return idb.open('wittr', 1, function(upgradeDb) {
- switch(upgradeDb.oldVersion) {
- case 0:
- var wittrStore = upgradeDb.createObjectStore('wittrs', { keyPath: 'id' });
- wittrStore.createIndex("by-date", 'time');
- }
- });
- }
- // called when the web socket sends message data
- IndexController.prototype._onSocketMessage = function(data) {
- var messages = JSON.parse(data);
- this._dbPromise.then(function(db) {
- if (!db) return;
- // TODO: put each message into the 'wittrs'
- // object store.
- var tx = db.transaction('wittr', 'readwrite');
- var wittrStore = tx.objectStore('wittrs');
- messages.forEach(function(msg) {
- wittrStore.put(msg);
- });
- return tx.complete;
- }).then(function() {
- console.log('New messages added!');
- });
- this._postsView.addPosts(messages);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement