mardi 6 février 2018

Handling Disconnect from Parity Wallet

Im running an application that uses the web3.js library to connect to a parity node being injected by the parity chrome extension. The following is the code I've implemented as a service in my Ember.js application.

import Service from '@ember/service';
import { get, set } from '@ember/object';

import { task, timeout } from 'ember-concurrency';
import Web3 from 'npm:web3';

const { web3 } = window;

export default Service.extend({
  provider: null,

  init() {
    this._super(...arguments);

    this.setProvider();
    get(this, 'maintainProvider').perform();
  },
  setProvider() {
    let provider;

    try {
      provider = new Web3(web3.currentProvider);
    } catch(reason) {
      throw reason;
    }

    set(this, 'provider', provider);
  },
  maintainProvider: task(function * () {
    let listening = yield get(this, 'provider').eth.net.isListening();

    if (!listening) {
      get(this, 'setProvider')();
    }

    yield timeout(1000);

    get(this, 'maintainProvider').perform();
  })
});

Basically, the idea is that I'd like to set a provider using the injected web3 object (this works fine). Then, if you stop the parity application, I'd like the app to notify the user and attempt to reconnect.

What's happening now is, when parity is closed the extension throws an error and haults execution of any app code, which is preventing me from trying to reconnect. Any ideas would be really helpful.




Aucun commentaire:

Enregistrer un commentaire