mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-26 20:39:08 +01:00
3732c5f71e
ESLint rules have been added to enforce our JSDoc conventions. These rules were introduced by updating `@metamask/eslint-config` to v9. Some of the rules have been disabled because the effort to fix all lint errors was too high. It might be easiest to enable these rules one directory at a time, or one rule at a time. Most of the changes in this PR were a result of running `yarn lint:fix`. There were a handful of manual changes that seemed obvious and simple to make. Anything beyond that and the rule was left disabled.
95 lines
2.0 KiB
JavaScript
95 lines
2.0 KiB
JavaScript
import extension from 'extensionizer';
|
|
import log from 'loglevel';
|
|
import { checkForError } from './util';
|
|
|
|
/**
|
|
* A wrapper around the extension's storage local API
|
|
*/
|
|
export default class ExtensionStore {
|
|
constructor() {
|
|
this.isSupported = Boolean(extension.storage.local);
|
|
if (!this.isSupported) {
|
|
log.error('Storage local API not available.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns all of the keys currently saved
|
|
*
|
|
* @returns {Promise<*>}
|
|
*/
|
|
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;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Sets the key in local state
|
|
*
|
|
* @param {Object} state - The state to set
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async set(state) {
|
|
return this._set(state);
|
|
}
|
|
|
|
/**
|
|
* Returns all of the keys currently saved
|
|
*
|
|
* @private
|
|
* @returns {Object} the key-value map from local storage
|
|
*/
|
|
_get() {
|
|
const { local } = extension.storage;
|
|
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
|
|
* @returns {Promise<void>}
|
|
* @private
|
|
*/
|
|
_set(obj) {
|
|
const { local } = extension.storage;
|
|
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}
|
|
*/
|
|
function isEmpty(obj) {
|
|
return Object.keys(obj).length === 0;
|
|
}
|