JS: Fix transparent caching of ledger entries.

This commit is contained in:
Arthur Britto
2012-11-29 15:33:23 -08:00
committed by Stefan Thomas
parent 8cb65c7d28
commit 0d52f87c8b

View File

@@ -584,18 +584,25 @@ Remote.prototype.request_ledger_entry = function (type) {
var self = this; var self = this;
var request = new Request(this, 'ledger_entry'); var request = new Request(this, 'ledger_entry');
if (type) // Transparent caching. When .request() is invoked, look in the Remote object for the result.
this.type = type; // If not found, listen, cache result, and emit it.
//
// Transparent caching: // Transparent caching:
this.request_default = this.request; if ('account_root' === type) {
this.request = function () { // Intercept default request. request.request_default = request.request;
if (self._ledger_hash) {
// XXX Add caching. request.request = function () { // Intercept default request.
} // .self = Remote
// this = Request
// console.log('request_ledger_entry: caught');
// if (self._ledger_hash) {
// // XXX Add caching.
// }
// else if (req.ledger_index) // else if (req.ledger_index)
// else if ('ripple_state' === this.type) // YYY Could be cached per ledger. // else if ('ripple_state' === request.type) // YYY Could be cached per ledger.
else if ('account_root' === this.type) { if ('account_root' === type) {
var cache = self.ledgers.current.account_root; var cache = self.ledgers.current.account_root;
if (!cache) if (!cache)
@@ -607,7 +614,8 @@ Remote.prototype.request_ledger_entry = function (type) {
if (node) { if (node) {
// Emulate fetch of ledger entry. // Emulate fetch of ledger entry.
self.emit('success', { // console.log('request_ledger_entry: emulating');
request.emit('success', {
// YYY Missing lots of fields. // YYY Missing lots of fields.
'node' : node, 'node' : node,
}); });
@@ -616,19 +624,23 @@ Remote.prototype.request_ledger_entry = function (type) {
// Was not cached. // Was not cached.
// XXX Only allow with trusted mode. Must sync response with advance. // XXX Only allow with trusted mode. Must sync response with advance.
switch (response.type) { switch (type) {
case 'account_root': case 'account_root':
request.on('success', function (message) { request.on('success', function (message) {
// Cache node. // Cache node.
// console.log('request_ledger_entry: caching');
self.ledgers.current.account_root[message.node.Account] = message.node; self.ledgers.current.account_root[message.node.Account] = message.node;
}); });
break; break;
default: default:
// This type not cached. // This type not cached.
// console.log('request_ledger_entry: non-cached type');
} }
this.request_default(); // console.log('request_ledger_entry: invoking');
request.request_default();
}
} }
} }
}; };