UNPKG

@unilogin/sdk

Version:

SDK is a JS library, that communicates with relayer. SDK allows managing contract, by creating basic contract-calling messages.

50 lines 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var AggregateBalanceObserver = /** @class */ (function () { function AggregateBalanceObserver(balanceObserver, priceObserver, tokensValueConverter) { this.balanceObserver = balanceObserver; this.priceObserver = priceObserver; this.tokensValueConverter = tokensValueConverter; this.tokensPrices = {}; this.tokenDetailsWithBalance = []; this.callbacks = []; } AggregateBalanceObserver.prototype.subscribe = function (callback) { var _this = this; this.callbacks.push(callback); if (this.callbacks.length === 1) { this.unsubscribeBalanceObserver = this.balanceObserver.subscribe(this.balanceObserverCallback.bind(this)); this.unsubscribePriceObserver = this.priceObserver.subscribe(this.priceObserverCallback.bind(this)); } var unsubscribe = function () { _this.callbacks = _this.callbacks.filter(function (element) { return callback !== element; }); if (_this.callbacks.length === 0) { _this.unsubscribeBalanceObserver(); _this.unsubscribePriceObserver(); _this.tokenDetailsWithBalance = []; } }; return unsubscribe; }; AggregateBalanceObserver.prototype.priceObserverCallback = function (tokensPrices) { this.tokensPrices = tokensPrices; this.refreshPrices(); }; AggregateBalanceObserver.prototype.balanceObserverCallback = function (tokenDetailsWithBalance) { this.tokenDetailsWithBalance = tokenDetailsWithBalance; this.refreshPrices(); }; AggregateBalanceObserver.prototype.refreshPrices = function () { if (Object.keys(this.tokensPrices).length === 0 || this.tokenDetailsWithBalance.length === 0) { return; } var totalWorth = this.tokensValueConverter.getTotal(this.tokenDetailsWithBalance, this.tokensPrices); this.notifyListeners(totalWorth); }; AggregateBalanceObserver.prototype.notifyListeners = function (totalWorth) { this.callbacks.forEach(function (callback) { return callback(totalWorth); }); }; return AggregateBalanceObserver; }()); exports.AggregateBalanceObserver = AggregateBalanceObserver; //# sourceMappingURL=AggregateBalanceObserver.js.map