2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
|
|
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
2021-01-19 17:41:57 +01:00
|
|
|
|
2022-07-19 18:13:45 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout();
|
2019-12-11 18:26:20 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const FIXTURE_SERVER_HOST = 'localhost';
|
|
|
|
const FIXTURE_SERVER_PORT = 12345;
|
|
|
|
const FIXTURE_SERVER_URL = `http://${FIXTURE_SERVER_HOST}:${FIXTURE_SERVER_PORT}/state.json`;
|
2019-12-11 18:26:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A read-only network-based storage wrapper
|
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class ReadOnlyNetworkStore {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._initialized = false;
|
|
|
|
this._initializing = this._init();
|
|
|
|
this._state = undefined;
|
2019-12-11 18:26:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-03 00:41:28 +01:00
|
|
|
* Declares this store as compatible with the current browser
|
|
|
|
*/
|
2021-02-04 19:15:23 +01:00
|
|
|
isSupported = true;
|
2019-12-11 18:26:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes by loading state from the network
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async _init() {
|
2019-12-11 18:26:20 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const response = await fetchWithTimeout(FIXTURE_SERVER_URL);
|
2019-12-11 18:26:20 +01:00
|
|
|
if (response.ok) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._state = await response.json();
|
2019-12-11 18:26:20 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.debug(`Error loading network state: '${error.message}'`);
|
2019-12-11 18:26:20 +01:00
|
|
|
} finally {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._initialized = true;
|
2019-12-11 18:26:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns state
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {Promise<object>}
|
2019-12-11 18:26:20 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async get() {
|
2019-12-11 18:26:20 +01:00
|
|
|
if (!this._initialized) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await this._initializing;
|
2019-12-11 18:26:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return this._state;
|
2019-12-11 18:26:20 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 00:10:44 +02:00
|
|
|
/**
|
|
|
|
* Set metadata/version state
|
|
|
|
*
|
|
|
|
* @param {object} metadata - The metadata/version data to set
|
|
|
|
*/
|
|
|
|
setMetadata(metadata) {
|
|
|
|
this.metadata = metadata;
|
|
|
|
}
|
|
|
|
|
2019-12-11 18:26:20 +01:00
|
|
|
/**
|
|
|
|
* Set state
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} state - The state to set
|
2019-12-11 18:26:20 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async set(state) {
|
2022-10-11 00:10:44 +02:00
|
|
|
if (!this.isSupported) {
|
|
|
|
throw new Error(
|
|
|
|
'Metamask- cannot persist state to local store as this browser does not support this action',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!state) {
|
|
|
|
throw new Error('MetaMask - updated state is missing');
|
|
|
|
}
|
|
|
|
if (!this.metadata) {
|
|
|
|
throw new Error(
|
|
|
|
'MetaMask - metadata must be set on instance of ExtensionStore before calling "set"',
|
|
|
|
);
|
|
|
|
}
|
2019-12-11 18:26:20 +01:00
|
|
|
if (!this._initialized) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await this._initializing;
|
2019-12-11 18:26:20 +01:00
|
|
|
}
|
2022-10-11 00:10:44 +02:00
|
|
|
this._state = { data: state, meta: this._metadata };
|
2019-12-11 18:26:20 +01:00
|
|
|
}
|
|
|
|
}
|