Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sw precaching #76

Merged
merged 20 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 123 additions & 107 deletions lib/idb-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,123 +19,139 @@ import idb from 'idb';
* A wrapper to store for an IDB connection to a particular ObjectStore.
*
* @private
* @class
*/
function IDBHelper(name, version, storeName) {
if (name == undefined || version == undefined || storeName == undefined) {
throw Error('name, version, storeName must be passed to the constructor.');
class IDBHelper {
constructor(name, version, storeName) {
if (name == undefined || version == undefined || storeName == undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strict === are being used elsewhere. Is there a reason the IDB helper is preferring ==?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my (somewhat old) code.

It's == to catch null as well as undefined, since they both would be treated the same way. Not sure how likely it is for someone to explicitly pass in null...

throw Error('name, version, storeName must be passed to the ' +
'constructor.');
}

this._name = name;
this._version = version;
this._storeName = storeName;
}

this._name = name;
this._version = version;
this._storeName = storeName;
}
/**
* Returns a promise that resolves with an open connection to IndexedDB,
* either existing or newly opened.
*
* @private
* @return {Promise<DB>}
*/
_getDb() {
if (this._dbPromise) {
return this._dbPromise;
}
console.log('-------------> Opening DB');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these console messages be limited to a dev mode?

this._dbPromise = idb.open(this._name, this._version, (upgradeDB) => {
upgradeDB.createObjectStore(this._storeName);
})
.then((db) => {
return db;
});

/**
* Returns a promise that resolves with an open connection to IndexedDB, either
* existing or newly opened.
*
* @private
* @return {Promise<DB>}
*/
IDBHelper.prototype._getDb = function() {
if (this._db) {
return Promise.resolve(this._db);
return this._dbPromise;
}

return idb.open(this._name, this._version, (upgradeDB) => {
upgradeDB.createObjectStore(this._storeName);
}).then((db) => {
this._db = db;
return db;
});
};
close() {
if (!this._dbPromise) {
return;
}
console.log('----------------> Closing DB');
return this._dbPromise
.then((db) => {
db.close();
this._dbPromise = null;
});
}

/**
* Wrapper on top of the idb wrapper, which simplifies saving the key/value
* pair to the object store.
* Returns a Promise that fulfills when the transaction completes.
*
* @private
* @param {String} key
* @param {Object} value
* @return {Promise<T>}
*/
IDBHelper.prototype.put = function(key, value) {
return this._getDb().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const objectStore = tx.objectStore(this._storeName);
objectStore.put(value, key);
return tx.complete;
});
};
/**
* Wrapper on top of the idb wrapper, which simplifies saving the key/value
* pair to the object store.
* Returns a Promise that fulfills when the transaction completes.
*
* @private
* @param {String} key
* @param {Object} value
* @return {Promise<T>}
*/
put(key, value) {
return this._getDb().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const objectStore = tx.objectStore(this._storeName);
objectStore.put(value, key);
return tx.complete;
});
}

/**
* Wrapper on top of the idb wrapper, which simplifies deleting an entry
* from the object store.
* Returns a Promise that fulfills when the transaction completes.
*
* @private
* @param {String} key
* @return {Promise<T>}
*/
IDBHelper.prototype.delete = function(key) {
return this._getDb().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const objectStore = tx.objectStore(this._storeName);
objectStore.delete(key);
return tx.complete;
});
};
/**
* Wrapper on top of the idb wrapper, which simplifies deleting an entry
* from the object store.
* Returns a Promise that fulfills when the transaction completes.
*
* @private
* @param {String} key
* @return {Promise<T>}
*/
delete(key) {
return this._getDb().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const objectStore = tx.objectStore(this._storeName);
objectStore.delete(key);
return tx.complete;
});
}

/**
* Wrapper on top of the idb wrapper, which simplifies getting a key's value
* from the object store.
* Returns a promise that fulfills with the value.
*
* @private
* @param {String} key
* @return {Promise<Object>}
*/
IDBHelper.prototype.get = function(key) {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.get(key);
});
};
/**
* Wrapper on top of the idb wrapper, which simplifies getting a key's value
* from the object store.
* Returns a promise that fulfills with the value.
*
* @private
* @param {String} key
* @return {Promise<Object>}
*/
get(key) {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.get(key);
});
}

/**
* Wrapper on top of the idb wrapper, which simplifies getting all the values
* in an object store.
* Returns a promise that fulfills with all the values.
*
* @private
* @return {Promise<Array<Object>>}
*/
IDBHelper.prototype.getAllValues = function() {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.getAll();
});
};
/**
* Wrapper on top of the idb wrapper, which simplifies getting all the values
* in an object store.
* Returns a promise that fulfills with all the values.
*
* @private
* @return {Promise<Array<Object>>}
*/
getAllValues() {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.getAll();
});
}

/**
* Wrapper on top of the idb wrapper, which simplifies getting all the keys
* in an object store.
* Returns a promise that fulfills with all the keys.
*
* @private
* @param {String} storeName
* @return {Promise<Array<Object>>}
*/
IDBHelper.prototype.getAllKeys = function() {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.getAllKeys();
});
};
/**
* Wrapper on top of the idb wrapper, which simplifies getting all the keys
* in an object store.
* Returns a promise that fulfills with all the keys.
*
* @private
* @param {String} storeName
* @return {Promise<Array<Object>>}
*/
getAllKeys() {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.getAllKeys();
});
}
}

export default IDBHelper;
80 changes: 0 additions & 80 deletions packages/sw-precaching/src/lib/install-handler.js

This file was deleted.

Loading