mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
ac01c5c89a
* Specify type before parameter name Various JSDoc `@param` entries were specified as `name {type}` rather than `{type} name`. A couple of `@return` entries have been given types as well. * Use JSDoc optional syntax rather than Closure syntax * Use @returns rather than @return * Use consistent built-in type capitalization Primitive types are lower-case, and Object is upper-case. * Separate param/return description with a dash
63 lines
1.3 KiB
JavaScript
63 lines
1.3 KiB
JavaScript
import log from 'loglevel'
|
|
|
|
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
|
|
*/
|
|
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 fetch(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
|
|
}
|
|
}
|
|
|
|
export default ReadOnlyNetworkStore
|