2017-06-22 21:32:08 +02:00
|
|
|
const ObservableStore = require('obs-store')
|
|
|
|
const extend = require('xtend')
|
2018-04-12 23:06:59 +02:00
|
|
|
const log = require('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 = {}) {
|
|
|
|
const initState = extend({
|
|
|
|
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 () {
|
|
|
|
const response = await fetch('https://api.infura.io/v1/status/metamask')
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = InfuraController
|