aboutsummaryrefslogblamecommitdiffstats
path: root/app/scripts/controllers/infura.js
blob: 8f6dd837ec8342d816e6fe338eeb5307f241044e (plain) (tree)
1
2
3
4
5
6

                                            
                               

                    
                                       















                                                                               






                                                                            

   
                                 



                                                 
                                                     




                                 
const ObservableStore = require('obs-store')
const extend = require('xtend')
const log = require('loglevel')

// every ten minutes
const POLLING_INTERVAL = 10 * 60 * 1000

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.
  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
  }

  scheduleInfuraNetworkCheck () {
    if (this.conversionInterval) {
      clearInterval(this.conversionInterval)
    }
    this.conversionInterval = setInterval(() => {
      this.checkInfuraNetworkStatus().catch(log.warn)
    }, POLLING_INTERVAL)
  }
}

module.exports = InfuraController