mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 04:35:49 +00:00
JS: Handle account events through Account class.
This commit is contained in:
@@ -9,15 +9,74 @@
|
||||
|
||||
// var network = require("./network.js");
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var Amount = require('./amount.js').Amount;
|
||||
var UInt160 = require('./amount.js').UInt160;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var Amount = require('./amount').Amount;
|
||||
var UInt160 = require('./uint160').UInt160;
|
||||
|
||||
var Account = function (network, account) {
|
||||
this._network = network;
|
||||
this._account = UInt160.json_rewrite(account);
|
||||
var Account = function (remote, account) {
|
||||
var self = this;
|
||||
|
||||
return this;
|
||||
this._remote = remote;
|
||||
this._account = UInt160.from_json(account);
|
||||
|
||||
// Ledger entry object
|
||||
// Important: This must never be overwritten, only extend()-ed
|
||||
this._entry = {};
|
||||
|
||||
this.on('newListener', function (type, listener) {
|
||||
if (Account.subscribe_events.indexOf(type) !== -1) {
|
||||
if (!this._subs && 'open' === this._remote._online_state) {
|
||||
this._remote.request_subscribe()
|
||||
.accounts(this._account.to_json())
|
||||
.request();
|
||||
}
|
||||
this._subs += 1;
|
||||
}
|
||||
});
|
||||
|
||||
this.on('removeListener', function (type, listener) {
|
||||
if (Account.subscribe_events.indexOf(type) !== -1) {
|
||||
this._subs -= 1;
|
||||
|
||||
if (!this._subs && 'open' === this._remote._online_state) {
|
||||
this._remote.request_unsubscribe([ 'transactions' ])
|
||||
.accounts(this._account.to_json())
|
||||
.request();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this._remote.on('connect', function () {
|
||||
if (self._subs) {
|
||||
this._remote.request_subscribe()
|
||||
.accounts(this._account.to_json())
|
||||
.request();
|
||||
}
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
Account.prototype = new EventEmitter;
|
||||
|
||||
/**
|
||||
* List of events that require a remote subscription to the account.
|
||||
*/
|
||||
Account.subscribe_events = ['transaction', 'entry'];
|
||||
|
||||
Account.prototype.to_json = function ()
|
||||
{
|
||||
return this._account.to_json();
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether the AccountId is valid.
|
||||
*
|
||||
* Note: This does not tell you whether the account exists in the ledger.
|
||||
*/
|
||||
Account.prototype.is_valid = function ()
|
||||
{
|
||||
return this._account.is_valid();
|
||||
};
|
||||
|
||||
exports.Account = Account;
|
||||
|
||||
Reference in New Issue
Block a user