2018-01-24 01:26:50 +01:00
|
|
|
const extension = require('extensionizer')
|
2018-04-12 23:06:59 +02:00
|
|
|
const log = require('loglevel')
|
2018-01-24 01:26:50 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* A wrapper around the extension's storage local API
|
|
|
|
*/
|
2018-01-24 01:26:50 +01:00
|
|
|
module.exports = class ExtensionStore {
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
constructor () {
|
2018-01-24 01:26:50 +01:00
|
|
|
this.isSupported = !!(extension.storage.local)
|
|
|
|
if (!this.isSupported) {
|
|
|
|
log.error('Storage local API not available.')
|
|
|
|
}
|
|
|
|
}
|
2018-03-08 23:10:28 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Returns all of the keys currently saved
|
|
|
|
* @return {Promise<*>}
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
async get () {
|
2018-03-08 23:10:28 +01:00
|
|
|
if (!this.isSupported) return undefined
|
|
|
|
const result = await this._get()
|
|
|
|
// extension.storage.local always returns an obj
|
|
|
|
// if the object is empty, treat it as undefined
|
|
|
|
if (isEmpty(result)) {
|
|
|
|
return undefined
|
|
|
|
} else {
|
|
|
|
return result
|
|
|
|
}
|
2018-01-24 01:26:50 +01:00
|
|
|
}
|
2018-03-08 23:10:28 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Sets the key in local state
|
|
|
|
* @param {object} state - The state to set
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
async set (state) {
|
2018-03-08 23:10:28 +01:00
|
|
|
return this._set(state)
|
2018-01-24 01:26:50 +01:00
|
|
|
}
|
2018-03-14 18:49:54 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Returns all of the keys currently saved
|
|
|
|
* @private
|
|
|
|
* @return {object} the key-value map from local storage
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
_get () {
|
2018-03-14 18:49:54 +01:00
|
|
|
const local = extension.storage.local
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-16 17:29:43 +02:00
|
|
|
local.get(null, (/** @type {any} */ result) => {
|
2018-03-14 18:49:54 +01:00
|
|
|
const err = extension.runtime.lastError
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Sets the key in local state
|
|
|
|
* @param {object} obj - The key to set
|
|
|
|
* @return {Promise<void>}
|
|
|
|
* @private
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
_set (obj) {
|
2018-03-14 18:49:54 +01:00
|
|
|
const local = extension.storage.local
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
local.set(obj, () => {
|
|
|
|
const err = extension.runtime.lastError
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-01-24 01:26:50 +01:00
|
|
|
}
|
2018-03-08 23:10:28 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Returns whether or not the given object contains no keys
|
|
|
|
* @param {object} obj - The object to check
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
function isEmpty (obj) {
|
2018-03-08 23:55:35 +01:00
|
|
|
return Object.keys(obj).length === 0
|
2018-03-08 23:10:28 +01:00
|
|
|
}
|