JS: Handle account events through Account class.

This commit is contained in:
Stefan Thomas
2013-02-12 18:35:30 +01:00
parent 540b52081e
commit cfe76ef92e
3 changed files with 174 additions and 7 deletions

View File

@@ -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;