All files account.ts

96.3% Statements 26/27
90% Branches 9/10
100% Functions 4/4
96.15% Lines 25/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57      1x 1x                         8x 8x 8x 8x 8x   8x 12x 4x 2x 2x   2x   8x 2x 1x 1x   1x     8x         2x 2x 2x 2x 2x             1x  
/**
 * Created by Administrator on 2016/11/21.
 */
import { EventEmitter } from "events"
import { utils } from "@swtc/utils"
 
/**
 * account stub for subscribe accounts transaction event
 * can be used for many accounts
 * @param remote
 * @constructor
 */
class Account extends EventEmitter {
  protected _token
  private _accounts
  private _remote
  constructor(remote) {
    super()
    this.setMaxListeners(0)
    this._remote = remote
    this._accounts = {}
    this._token = remote._token || "swt"
 
    this.on("newListener", function(account, listener) {
      if (account === "removeListener") return
      if (!utils.isValidAddress(account)) {
        this.account = new Error("invalid account")
        return this
      }
      this._accounts[account] = listener
    })
    this.on("removeListener", function(account) {
      if (!utils.isValidAddress(account)) {
        this.account = new Error("invalid account")
        return this
      }
      delete this._accounts[account]
    })
    // subscribe all transactions, so just dispatch event by account
    this._remote.on("transactions", this.__infoAffectedAccounts.bind(this))
  }
 
  private __infoAffectedAccounts(data) {
    // dispatch
    const accounts = utils.affectedAccounts(data)
    for (const account of accounts) {
      const callback = this._accounts[accounts[account]]
      const _tx = utils.processTx(data, accounts[account])
      Iif (callback) {
        callback(_tx)
      }
    }
  }
}
 
export { Account }