1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 12:47:14 +01:00
metamask-extension/app/scripts/lib/network-store.js
Mark Stacey 3732c5f71e
Add JSDoc ESLint rules (#12112)
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.
2022-01-07 12:27:33 -03:30

67 lines
1.5 KiB
JavaScript

import log from 'loglevel';
import { SECOND } from '../../../shared/constants/time';
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
const FIXTURE_SERVER_HOST = 'localhost';
const FIXTURE_SERVER_PORT = 12345;
const FIXTURE_SERVER_URL = `http://${FIXTURE_SERVER_HOST}:${FIXTURE_SERVER_PORT}/state.json`;
/**
* A read-only network-based storage wrapper
*/
export default class ReadOnlyNetworkStore {
constructor() {
this._initialized = false;
this._initializing = this._init();
this._state = undefined;
}
/**
* Declares this store as compatible with the current browser
*/
isSupported = true;
/**
* Initializes by loading state from the network
*/
async _init() {
try {
const response = await fetchWithTimeout(FIXTURE_SERVER_URL);
if (response.ok) {
this._state = await response.json();
}
} catch (error) {
log.debug(`Error loading network state: '${error.message}'`);
} finally {
this._initialized = true;
}
}
/**
* Returns state
*
* @returns {Promise<object>}
*/
async get() {
if (!this._initialized) {
await this._initializing;
}
return this._state;
}
/**
* Set state
*
* @param {Object} state - The state to set
* @returns {Promise<void>}
*/
async set(state) {
if (!this._initialized) {
await this._initializing;
}
this._state = state;
}
}