Skip to content

Commit

Permalink
[FEATURE] Emit event from the listener object explicitly to get corre…
Browse files Browse the repository at this point in the history
…ct scope.
  • Loading branch information
Steven Zeiler committed Jan 3, 2014
1 parent 24c27de commit debfe19
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/account_listener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
WebSocket = require('ws')
EventEmitter = require('events').EventEmitter
Payment = require('./payment')

function AccountListener(options) {
this.accounts = options.accounts
Expand All @@ -10,10 +11,11 @@ function AccountListener(options) {
AccountListener.prototype.__proto__ = EventEmitter.prototype

AccountListener.prototype.connect = function() {
handlers = this.getHandlers()
try {
this.webSocket.on('open', this.handlers.onOpen)
this.webSocket.on('message', this.handlers.onMessage)
this.webSocket.on('close', this.handlers.onClose)
this.webSocket.on('open', handlers.onOpen)
this.webSocket.on('message', handlers.onMessage)
this.webSocket.on('close', handlers.onClose)
} catch(e) {
delete this.webSocket
this.webSocket = new WebSocket(this.url)
Expand All @@ -23,7 +25,8 @@ AccountListener.prototype.connect = function() {
this.webSocket.send('{"command":"subscribe","id":0,"accounts":'+accounts+'}')
}

AccountListener.prototype.handlers = (function(){

AccountListener.prototype.getHandlers = function(){
listener = this

function onOpen() {
Expand All @@ -42,6 +45,6 @@ AccountListener.prototype.handlers = (function(){
}

return { onOpen: onOpen, onClose: onClose, onMessage: onMessage }
})()
}

module.exports = AccountListener

0 comments on commit debfe19

Please sign in to comment.