From 5c3ffb4fd334691ace32cee9b57af08860a9839c Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Mon, 26 Nov 2012 15:52:59 -0800 Subject: [PATCH] If you need override a method, do it straight up, don't abuse events. Events are meant for managing asynchronous activity. Don't replace normal method calls with events. Look at the implementation for request() in the removed code. We can see that an event is being emitted, but the code catching it could be anywhere in this file or a different file or even in multiple places. Code containing too many events quickly becomes unmaintainable. Events are the most complex tool we have, callbacks are less complex and synchronous calls are the least complex. Use the least complex tool that can solve your problem. --- src/js/remote.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/js/remote.js b/src/js/remote.js index af2e012891..8ebadb6a2a 100644 --- a/src/js/remote.js +++ b/src/js/remote.js @@ -39,23 +39,16 @@ var Request = function (remote, command) { }; this.remote = remote; this.requested = false; - - this.on('request', function () { - self.request_default(); - }); }; Request.prototype = new EventEmitter; // Send the request to a remote. Request.prototype.request = function (remote) { - this.emit('request', remote); -}; - -Request.prototype.request_default = function () { if (!this.requested) { this.requested = true; this.remote.request(this); + this.emit('request', remote); } }; @@ -595,7 +588,8 @@ Remote.prototype.request_ledger_entry = function (type) { this.type = type; // Transparent caching: - request.on('request', function (remote) { // Intercept default request. + this.request_default = this.request; + this.request = function () { // Intercept default request. if (self._ledger_hash) { // XXX Add caching. } @@ -613,7 +607,7 @@ Remote.prototype.request_ledger_entry = function (type) { if (node) { // Emulate fetch of ledger entry. - this.request.emit('success', { + self.emit('success', { // YYY Missing lots of fields. 'node' : node, }); @@ -637,7 +631,7 @@ Remote.prototype.request_ledger_entry = function (type) { this.request_default(); } } - }); + }; return request; };