From debfe192f931657a268b7634d5d8c9e6ffef7f08 Mon Sep 17 00:00:00 2001 From: Steven Zeiler Date: Fri, 3 Jan 2014 13:10:36 -0800 Subject: [PATCH] [FEATURE] Emit event from the listener object explicitly to get correct scope. --- lib/account_listener.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/account_listener.js b/lib/account_listener.js index 6ae2e83..fd30994 100644 --- a/lib/account_listener.js +++ b/lib/account_listener.js @@ -1,5 +1,6 @@ WebSocket = require('ws') EventEmitter = require('events').EventEmitter +Payment = require('./payment') function AccountListener(options) { this.accounts = options.accounts @@ -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) @@ -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() { @@ -42,6 +45,6 @@ AccountListener.prototype.handlers = (function(){ } return { onOpen: onOpen, onClose: onClose, onMessage: onMessage } -})() +} module.exports = AccountListener