Skip to content

Commit

Permalink
Update DBEngine to Use Some ES6
Browse files Browse the repository at this point in the history
Updated all the 'var' to 'let' and 'const'. Replaced uses of bind with
ES6 inline functions.

Change-Id: I0427b972c56ac304417d3129cc9d137610cb5b6b
  • Loading branch information
vaage committed Jan 26, 2018
1 parent 4147e77 commit bd2b163
Showing 1 changed file with 49 additions and 50 deletions.
99 changes: 49 additions & 50 deletions lib/offline/db_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ goog.provide('shaka.offline.DBEngine');
goog.require('goog.asserts');
goog.require('shaka.offline.IStorageEngine');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.PublicPromise');


Expand Down Expand Up @@ -108,9 +107,9 @@ shaka.offline.DBEngine.isSupported = function() {
shaka.offline.DBEngine.deleteDatabase = function(name) {
if (!window.indexedDB)
return Promise.resolve();
var request = window.indexedDB.deleteDatabase(name);
let request = window.indexedDB.deleteDatabase(name);

var p = new shaka.util.PublicPromise();
let p = new shaka.util.PublicPromise();
request.onsuccess = function(event) {
goog.asserts.assert(event.newVersion == null, 'Unexpected database update');
p.resolve();
Expand All @@ -127,8 +126,8 @@ shaka.offline.DBEngine.deleteDatabase = function(name) {
* @return {!Promise}
*/
shaka.offline.DBEngine.prototype.init = function(opt_updateRetries) {
var name = this.name_;
var version = shaka.offline.DBEngine.DB_VERSION_;
let name = this.name_;
let version = shaka.offline.DBEngine.DB_VERSION_;

return shaka.offline.DBEngine.open_(name, version, opt_updateRetries)
.then((db) => { this.db_ = db; });
Expand All @@ -137,24 +136,24 @@ shaka.offline.DBEngine.prototype.init = function(opt_updateRetries) {

/** @override */
shaka.offline.DBEngine.prototype.destroy = function() {
return Promise.all(this.operations_.map(function(op) {
return Promise.all(this.operations_.map((op) => {
try {
// If the transaction is considered finished but has not called the
// callbacks yet, it will still be in the list and this call will fail.
// Simply ignore errors.
op.transaction.abort();
} catch (e) {}

var Functional = shaka.util.Functional;
return op.promise.catch(Functional.noop);
})).then(function() {
// Silence any rejected promises.
return op.promise.catch(() => {});
})).then(() => {
goog.asserts.assert(this.operations_.length == 0,
'All operations should have been closed');
if (this.db_) {
this.db_.close();
this.db_ = null;
}
}.bind(this));
});
};


Expand Down Expand Up @@ -244,10 +243,10 @@ shaka.offline.DBEngine.prototype.removeSegments =
*/
shaka.offline.DBEngine.prototype.get_ = function(store, key) {
/** @const */
var READ_ONLY = shaka.offline.DBEngine.Mode.READ_ONLY;
const READ_ONLY = shaka.offline.DBEngine.Mode.READ_ONLY;

/** @type {IDBRequest} */
var request;
let request;
return this.createTransaction_(store, READ_ONLY, function(store) {
request = store.get(key);
}).then(function() { return request.result; });
Expand All @@ -263,12 +262,12 @@ shaka.offline.DBEngine.prototype.get_ = function(store, key) {
*/
shaka.offline.DBEngine.prototype.forEach_ = function(storeName, each) {
/** @const */
var READ_ONLY = shaka.offline.DBEngine.Mode.READ_ONLY;
const READ_ONLY = shaka.offline.DBEngine.Mode.READ_ONLY;

return this.createTransaction_(storeName, READ_ONLY, (store) => {
store.openCursor().onsuccess = (event) => {
/** @type {IDBCursor} */
var cursor = event.target.result;
let cursor = event.target.result;
// When we reach the end of the data that the cursor is iterating
// over, |event.target.result| will be null to signal the end of the
// iteration.
Expand All @@ -278,9 +277,9 @@ shaka.offline.DBEngine.prototype.forEach_ = function(storeName, each) {
}

/** @type {number} */
var key = /** @type {number} */ (cursor.key);
let key = /** @type {number} */ (cursor.key);
/** @type {Object} */
var value = /** @type {Object} */ (cursor.value);
let value = /** @type {Object} */ (cursor.value);

each(key, value);

Expand All @@ -300,7 +299,7 @@ shaka.offline.DBEngine.prototype.forEach_ = function(storeName, each) {
*/
shaka.offline.DBEngine.prototype.update_ = function(store, key, value) {
/** @const */
var READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;
const READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;

return this.createTransaction_(store, READ_WRITE, function(store) {
store.put(value, key);
Expand All @@ -317,13 +316,13 @@ shaka.offline.DBEngine.prototype.update_ = function(store, key, value) {
*/
shaka.offline.DBEngine.prototype.add_ = function(store, value) {
/** @const */
var READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;
const READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;

/** @type {number} */
var key;
let key;

return this.createTransaction_(store, READ_WRITE, function(store) {
var request = store.add(value);
let request = store.add(value);
request.onsuccess = function(event) {
key = event.target.result;
};
Expand All @@ -341,12 +340,12 @@ shaka.offline.DBEngine.prototype.add_ = function(store, value) {
*/
shaka.offline.DBEngine.prototype.remove_ = function(store, keys, onKeyRemoved) {
/** @const */
var READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;
const READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;

return this.createTransaction_(store, READ_WRITE, function(store) {
keys.forEach(function(key) {
/** @type {IDBRequest} */
var request = store.delete(key);
let request = store.delete(key);
request.onsuccess = function() {
if (onKeyRemoved) {
onKeyRemoved(key);
Expand All @@ -373,18 +372,18 @@ shaka.offline.DBEngine.prototype.createTransaction_ = function(storeName,
type,
action) {
/** @const */
var READ_ONLY = shaka.offline.DBEngine.Mode.READ_ONLY;
const READ_ONLY = shaka.offline.DBEngine.Mode.READ_ONLY;
/** @const */
var READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;
const READ_WRITE = shaka.offline.DBEngine.Mode.READ_WRITE;

/** @type {!shaka.offline.DBEngine} */
var self = this;
let self = this;

goog.asserts.assert(self.db_, 'DBEngine must not be destroyed');
goog.asserts.assert(type == READ_ONLY || type == READ_WRITE,
'Unexpected transaction type.');

var op = {
let op = {
transaction: self.db_.transaction([storeName], type),
promise: new shaka.util.PublicPromise()
};
Expand All @@ -406,7 +405,7 @@ shaka.offline.DBEngine.prototype.createTransaction_ = function(storeName,

op.transaction.onerror = shaka.offline.DBEngine.quietEventHandler_();

var store = op.transaction.objectStore(storeName);
let store = op.transaction.objectStore(storeName);
action(store);

self.operations_.push(op);
Expand All @@ -422,7 +421,7 @@ shaka.offline.DBEngine.prototype.createTransaction_ = function(storeName,
* @private
*/
shaka.offline.DBEngine.prototype.closeOperation_ = function(op) {
var i = this.operations_.indexOf(op);
let i = this.operations_.indexOf(op);
goog.asserts.assert(i >= 0, 'Operation must be in the list.');
this.operations_.splice(i, 1);
};
Expand All @@ -440,9 +439,9 @@ shaka.offline.DBEngine.onUpgrade_ = function(promise,
db,
transaction) {
/** @const {!IDBObjectStoreParameters} */
var storeSettings = {autoIncrement: true};
const storeSettings = {autoIncrement: true};

var Store = shaka.offline.DBEngine.Store;
let Store = shaka.offline.DBEngine.Store;

// We are upgrading from an empty database, this is good, we can handle this.
if (oldVersion == 0) {
Expand All @@ -463,18 +462,18 @@ shaka.offline.DBEngine.onUpgrade_ = function(promise,
*/
shaka.offline.DBEngine.rejectUpgrade_ = function(promise, db, transaction) {
// Make a list of all the stores that we need to check the uris from.
var stores = ['manifest', 'manifest-v2'].filter((name) => {
let stores = ['manifest', 'manifest-v2'].filter((name) => {
return db.objectStoreNames.contains(name);
});

/** @type {!Array.<string>} */
var uris = [];
let uris = [];

var addUrisFromStore = (name, next) => {
transaction.objectStore(name).openCursor().onsuccess = (event) => {
var cursor = event.target.result;
let cursor = event.target.result;
if (cursor) {
var content = cursor.value;
let content = cursor.value;
uris.push(content.originalManifestUri);

cursor.continue();
Expand All @@ -486,8 +485,8 @@ shaka.offline.DBEngine.rejectUpgrade_ = function(promise, db, transaction) {

// Collect all the uris from the next store. If there are no more stores
// this will terminate the series and call |end|.
var next = () => {
var name = stores.pop();
let next = () => {
let name = stores.pop();
if (name) {
addUrisFromStore(name, next);
} else {
Expand Down Expand Up @@ -520,7 +519,7 @@ shaka.offline.DBEngine.rejectUpgrade_ = function(promise, db, transaction) {
*/
shaka.offline.DBEngine.onError_ = function(errorSource, promise, event) {

var error;
let error;

if (errorSource.error) {
error = new shaka.util.Error(
Expand Down Expand Up @@ -557,18 +556,18 @@ shaka.offline.DBEngine.onError_ = function(errorSource, promise, event) {
* @private
*/
shaka.offline.DBEngine.open_ = function(name, version, opt_updateRetries) {
var tryOpen = shaka.offline.DBEngine.tryOpen_;
let tryOpen = shaka.offline.DBEngine.tryOpen_;

/** @type {number} */
var retries = opt_updateRetries || 0;
let retries = opt_updateRetries || 0;

var wait = () => {
var wait = 1000; // 1 second
let wait = () => {
let wait = 1000; // 1 second
return new Promise((resolve) => setTimeout(resolve, wait));
};

var forceUpgrade = retries > 0;
var chain = shaka.offline.DBEngine.tryOpen_(name, version, forceUpgrade);
let forceUpgrade = retries > 0;
let chain = shaka.offline.DBEngine.tryOpen_(name, version, forceUpgrade);

for (let i = 0; i < retries; i++) {
chain = chain.then((db) => {
Expand Down Expand Up @@ -603,23 +602,23 @@ shaka.offline.DBEngine.open_ = function(name, version, opt_updateRetries) {
*/
shaka.offline.DBEngine.tryOpen_ = function(name, version, needUpgrade) {
/** @type {!shaka.util.PublicPromise} */
var promise = new shaka.util.PublicPromise();
let promise = new shaka.util.PublicPromise();

var upgraded = false;
let upgraded = false;

var request = window.indexedDB.open(name, version);
let request = window.indexedDB.open(name, version);
request.onupgradeneeded = (event) => {
var oldVersion = event.oldVersion;
var transaction = event.target.transaction;
var db = transaction.db;
let oldVersion = event.oldVersion;
let transaction = event.target.transaction;
let db = transaction.db;

shaka.offline.DBEngine.onUpgrade_(promise, oldVersion, db, transaction);
upgraded = true;
};

request.onsuccess = (event) => {
/** @type {IDBDatabase} */
var db = event.target.result;
let db = event.target.result;

if (needUpgrade && !upgraded) {
db.close();
Expand Down

0 comments on commit bd2b163

Please sign in to comment.