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.
This commit is contained in:
Stefan Thomas
2012-11-26 15:52:59 -08:00
parent 11d0e93cd8
commit 5c3ffb4fd3

View File

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