'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.default = undefined;var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}(); // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

var _lodash = require('lodash');

var _balancesActions = require('./balancesActions');function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}

var instance = null;var

Balances = function () {
  function Balances(store, api) {_classCallCheck(this, Balances);
    this._api = api;
    this._store = store;

    this._apiSubs = [];

    // Throttled `_fetchEthBalances` function
    // that gets called max once every 40s
    this.longThrottledFetch = (0, _lodash.throttle)(
    this._fetchEthBalances,
    40 * 1000,
    { leading: true, trailing: false });


    this.shortThrottledFetch = (0, _lodash.throttle)(
    this._fetchEthBalances,
    2 * 1000,
    { leading: true, trailing: false });


    // Unsubscribe previous instance if it exists
    if (instance) {
      Balances.stop();
    }
  }_createClass(Balances, [{ key: 'subscribeAccountsInfo', value: function subscribeAccountsInfo()























































    {var _this = this;
      // Don't trigger the balances updates on first call (when the
      // subscriptions are setup)
      var firstcall = true;

      return this._api.
      subscribe('parity_allAccountsInfo', function (error, accountsInfo) {
        if (error) {
          return console.warn('balances::subscribeAccountsInfo', error);
        }

        if (firstcall) {
          firstcall = false;
          return;
        }

        _this._store.dispatch((0, _balancesActions.updateTokensFilter)());
        _this.fetchEthBalances();
      }).
      then(function (subId) {
        _this._apiSubs.push(subId);
      }).
      catch(function (error) {
        console.warn('_subscribeAccountsInfo', error);
      });
    } }, { key: 'subscribeBlockNumber', value: function subscribeBlockNumber()

    {var _this2 = this;
      // Don't trigger the balances updates on first call (when the
      // subscriptions are setup)
      var firstcall = true;

      return this._api.
      subscribe('eth_blockNumber', function (error, block) {
        if (error) {
          return console.warn('balances::subscribeBlockNumber', error);
        }

        if (firstcall) {
          firstcall = false;
          return;
        }

        _this2._store.dispatch((0, _balancesActions.queryTokensFilter)());
        return _this2.fetchEthBalances();
      }).
      then(function (subId) {
        _this2._apiSubs.push(subId);
      }).
      catch(function (error) {
        console.warn('_subscribeBlockNumber', error);
      });
    } }, { key: 'fetchEthBalances', value: function fetchEthBalances()

    {var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};var
      syncing = this._store.getState().nodeStatus.syncing;

      if (options.force) {
        return this._fetchEthBalances();
      }

      // If syncing, only retrieve balances once every
      // few seconds
      if (syncing || syncing === null) {
        this.shortThrottledFetch.cancel();
        return this.longThrottledFetch();
      }

      this.longThrottledFetch.cancel();
      return this.shortThrottledFetch();
    } }, { key: '_fetchEthBalances', value: function _fetchEthBalances()

    {var skipNotifications = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;var _store =
      this._store,dispatch = _store.dispatch,getState = _store.getState;

      return (0, _balancesActions.fetchBalances)(null, skipNotifications)(dispatch, getState);
    } }], [{ key: 'get', value: function get(store) {if (!instance && store) {return Balances.init(store);} else if (!instance) {throw new Error('The Balances Provider has not been initialized yet');}return instance;} }, { key: 'init', value: function init(store) {var _store$getState = store.getState(),api = _store$getState.api;if (!instance) {instance = new Balances(store, api);}return instance;} }, { key: 'start', value: function start() {if (!instance) {return Promise.reject(new Error('BalancesProvider has not been initiated yet'));}var self = instance; // Unsubscribe from previous subscriptions
      return Balances.stop().then(function () {var promises = [self.subscribeBlockNumber(), self.subscribeAccountsInfo()];return Promise.all(promises);}).then(function () {return self.fetchEthBalances();});} }, { key: 'stop', value: function stop() {if (!instance) {return Promise.resolve();}var self = instance;var promises = self._apiSubs.map(function (subId) {return self._api.unsubscribe(subId);});return Promise.all(promises).then(function () {self._apiSubs = [];});} }]);return Balances;}();exports.default = Balances;