2020-01-09 04:34:58 +01:00
|
|
|
import extension from 'extensionizer'
|
|
|
|
import log from 'loglevel'
|
|
|
|
import { checkForError } from './util'
|
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
|
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export default class ExtensionStore {
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor() {
|
2020-08-19 18:27:05 +02:00
|
|
|
this.isSupported = Boolean(extension.storage.local)
|
2018-01-24 01:26:50 +01:00
|
|
|
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
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {Promise<*>}
|
2018-04-16 17:29:43 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async get() {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!this.isSupported) {
|
|
|
|
return undefined
|
|
|
|
}
|
2018-03-08 23:10:28 +01:00
|
|
|
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
|
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
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
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} state - The state to set
|
|
|
|
* @returns {Promise<void>}
|
2018-04-16 17:29:43 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01: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
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Object} the key-value map from local storage
|
2018-04-16 17:29:43 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_get() {
|
2020-08-18 22:06:58 +02:00
|
|
|
const { local } = extension.storage
|
2018-03-14 18:49:54 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-16 17:29:43 +02:00
|
|
|
local.get(null, (/** @type {any} */ result) => {
|
2019-09-23 05:45:20 +02:00
|
|
|
const err = checkForError()
|
2018-03-14 18:49:54 +01:00
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Sets the key in local state
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} obj - The key to set
|
|
|
|
* @returns {Promise<void>}
|
2018-04-16 17:29:43 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_set(obj) {
|
2020-08-18 22:06:58 +02:00
|
|
|
const { local } = extension.storage
|
2018-03-14 18:49:54 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
local.set(obj, () => {
|
2019-09-23 05:45:20 +02:00
|
|
|
const err = checkForError()
|
2018-03-14 18:49:54 +01:00
|
|
|
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
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} obj - The object to check
|
2018-04-16 17:29:43 +02:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01: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
|
|
|
}
|