2020-01-09 04:34:58 +01:00
|
|
|
import ObservableStore from 'obs-store'
|
|
|
|
import log from 'loglevel'
|
2017-06-22 21:32:08 +02:00
|
|
|
|
|
|
|
// every ten minutes
|
2017-08-03 00:54:59 +02:00
|
|
|
const POLLING_INTERVAL = 10 * 60 * 1000
|
2017-06-22 21:32:08 +02:00
|
|
|
|
|
|
|
class InfuraController {
|
|
|
|
|
|
|
|
constructor (opts = {}) {
|
2020-01-13 19:59:36 +01:00
|
|
|
const initState = Object.assign({
|
2017-06-22 21:32:08 +02:00
|
|
|
infuraNetworkStatus: {},
|
|
|
|
}, opts.initState)
|
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// PUBLIC METHODS
|
|
|
|
//
|
|
|
|
|
|
|
|
// Responsible for retrieving the status of Infura's nodes. Can return either
|
|
|
|
// ok, degraded, or down.
|
2018-04-03 19:33:10 +02:00
|
|
|
async checkInfuraNetworkStatus () {
|
2020-04-15 19:23:27 +02:00
|
|
|
const response = await window.fetch('https://api.infura.io/v1/status/metamask')
|
2018-04-03 19:33:10 +02:00
|
|
|
const parsedResponse = await response.json()
|
|
|
|
this.store.updateState({
|
|
|
|
infuraNetworkStatus: parsedResponse,
|
|
|
|
})
|
|
|
|
return parsedResponse
|
2017-06-22 21:32:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 21:32:34 +02:00
|
|
|
scheduleInfuraNetworkCheck () {
|
2017-06-22 21:32:08 +02:00
|
|
|
if (this.conversionInterval) {
|
|
|
|
clearInterval(this.conversionInterval)
|
|
|
|
}
|
|
|
|
this.conversionInterval = setInterval(() => {
|
2018-04-03 19:33:10 +02:00
|
|
|
this.checkInfuraNetworkStatus().catch(log.warn)
|
2017-06-22 21:32:08 +02:00
|
|
|
}, POLLING_INTERVAL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default InfuraController
|