1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 02:58:09 +01:00
metamask-extension/app/scripts/lib/local-store.js

105 lines
2.4 KiB
JavaScript
Raw Normal View History

const extension = require('extensionizer')
const log = require('loglevel')
/**
* A wrapper around the extension's storage local API
*/
module.exports = class ExtensionStore {
/**
* @constructor
*/
2018-07-03 00:49:33 +02:00
constructor () {
this.isSupported = !!(extension.storage.local)
if (!this.isSupported) {
log.error('Storage local API not available.')
}
}
/**
* Returns all of the keys currently saved
* @return {Promise<*>}
*/
2018-07-03 00:49:33 +02:00
async get () {
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
}
}
/**
* 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) {
return this._set(state)
}
/**
* 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 () {
const local = extension.storage.local
return new Promise((resolve, reject) => {
local.get(null, (/** @type {any} */ result) => {
const err = checkForError()
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
}
/**
* 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) {
const local = extension.storage.local
return new Promise((resolve, reject) => {
local.set(obj, () => {
const err = checkForError()
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
}
/**
* 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
}
/**
* Returns an Error if extension.runtime.lastError is present
* this is a workaround for the non-standard error object thats used
* @returns {Error}
*/
function checkForError () {
const lastError = extension.runtime.lastError
if (!lastError) return
// if it quacks like an Error, its an Error
if (lastError.stack && lastError.message) return lastError
// repair incomplete error object (eg chromium v77)
return new Error(lastError.message)
}