mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-03 18:45:48 +00:00
Revert "Add offline queue, update code comments"
This reverts commit 9dd337a16c.
This commit is contained in:
@@ -58,9 +58,7 @@ util.inherits(Request, EventEmitter);
|
|||||||
|
|
||||||
// Send the request to a remote.
|
// Send the request to a remote.
|
||||||
Request.prototype.request = function(remote) {
|
Request.prototype.request = function(remote) {
|
||||||
if (!this.remote._connected) {
|
if (!this.requested) {
|
||||||
this.remote._offline_queue.push(this);
|
|
||||||
} else if (!this.requested) {
|
|
||||||
this.requested = true;
|
this.requested = true;
|
||||||
this.remote.request(this);
|
this.remote.request(this);
|
||||||
this.emit('request', remote);
|
this.emit('request', remote);
|
||||||
@@ -255,72 +253,39 @@ Request.prototype.books = function(books, snapshot) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
Interface to manage the connection to a Ripple server.
|
Interface to manage the connection to a Ripple server.
|
||||||
|
|
||||||
This implementation uses WebSockets.
|
This implementation uses WebSockets.
|
||||||
|
|
||||||
Configuration options:
|
Keys for opts:
|
||||||
|
|
||||||
+ `trusted` {Boolean}
|
trusted : truthy, if remote is trusted
|
||||||
if remote is trusted
|
websocket_ip
|
||||||
|
websocket_port
|
||||||
+ `trace` {Boolean}
|
websocket_ssl
|
||||||
|
trace
|
||||||
+ `maxListeners` {Number}
|
maxListeners
|
||||||
set maxListeners for EventEmitters to prevent
|
|
||||||
leak warnings. set to 0 for infinite
|
|
||||||
|
|
||||||
+ `servers` {Array}
|
|
||||||
list of remote servers to use. each entry
|
|
||||||
has the form:
|
|
||||||
|
|
||||||
{
|
|
||||||
host: <hostname>
|
|
||||||
port: <port>
|
|
||||||
secure: <use SSL>
|
|
||||||
}
|
|
||||||
|
|
||||||
Events:
|
Events:
|
||||||
|
'connect'
|
||||||
+ 'connect'
|
'connected' (DEPRECATED)
|
||||||
at least one server has connected. the remote
|
'disconnect'
|
||||||
is ready to begin processing requests
|
'disconnected' (DEPRECATED)
|
||||||
|
'state':
|
||||||
+ 'connected' (DEPRECATED)
|
- 'online' : Connected and subscribed.
|
||||||
|
- 'offline' : Not subscribed or not connected.
|
||||||
+ 'disconnect'
|
'subscribed' : This indicates stand-alone is available.
|
||||||
there are no more available servers. the
|
|
||||||
remote is unprepared to process requests
|
|
||||||
|
|
||||||
+ 'disconnected' (DEPRECATED)
|
|
||||||
|
|
||||||
+ 'state'
|
|
||||||
either 'online' or 'offline'
|
|
||||||
|
|
||||||
+ 'online'
|
|
||||||
connected and subscribed
|
|
||||||
|
|
||||||
+ 'offline'
|
|
||||||
not subscribed or not connected
|
|
||||||
|
|
||||||
+ 'subscribed'
|
|
||||||
this indicates stand-alone is available
|
|
||||||
|
|
||||||
Server events:
|
Server events:
|
||||||
|
'ledger_closed' : A good indicate of ready to serve.
|
||||||
|
'transaction' : Transactions we receive based on current subscriptions.
|
||||||
|
'transaction_all' : Listening triggers a subscribe to all transactions
|
||||||
|
globally in the network.
|
||||||
|
|
||||||
+ 'ledger_closed'
|
@param opts Connection options.
|
||||||
a good indicate of ready to serve
|
@param trace
|
||||||
|
|
||||||
+ 'transaction'
|
|
||||||
transactions we receive based on current subscriptions
|
|
||||||
|
|
||||||
+ 'transaction_all'
|
|
||||||
listening triggers a subscribe to all transactions
|
|
||||||
globally in the network
|
|
||||||
|
|
||||||
@param {Object} opts Connection options.
|
|
||||||
@param {Boolean} trace
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Remote = function(opts, trace) {
|
var Remote = function(opts, trace) {
|
||||||
@@ -343,12 +308,10 @@ var Remote = function(opts, trace) {
|
|||||||
this._testnet = void(0);
|
this._testnet = void(0);
|
||||||
this._transaction_subs = 0;
|
this._transaction_subs = 0;
|
||||||
this.online_target = false;
|
this.online_target = false;
|
||||||
this._connected = false;
|
|
||||||
this._online_state = 'closed'; // 'open', 'closed', 'connecting', 'closing'
|
this._online_state = 'closed'; // 'open', 'closed', 'connecting', 'closing'
|
||||||
this.state = 'offline'; // 'online', 'offline'
|
this.state = 'offline'; // 'online', 'offline'
|
||||||
this.retry_timer = void(0);
|
this.retry_timer = void(0);
|
||||||
this.retry = void(0);
|
this.retry = void(0);
|
||||||
this._offline_queue = [ ];
|
|
||||||
|
|
||||||
this._load_base = 256;
|
this._load_base = 256;
|
||||||
this._load_factor = 1.0;
|
this._load_factor = 1.0;
|
||||||
@@ -441,14 +404,6 @@ var Remote = function(opts, trace) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.once('connect', function offlineQueueListener() {
|
|
||||||
var offline_queue = self._offline_queue;
|
|
||||||
var request;
|
|
||||||
while (request = offline_queue.shift()) {
|
|
||||||
request.request();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(Remote, EventEmitter);
|
util.inherits(Remote, EventEmitter);
|
||||||
@@ -535,9 +490,7 @@ Remote.prototype.server_fatal = function() {
|
|||||||
|
|
||||||
// Set the emitted state: 'online' or 'offline'
|
// Set the emitted state: 'online' or 'offline'
|
||||||
Remote.prototype._set_state = function(state) {
|
Remote.prototype._set_state = function(state) {
|
||||||
if (this.trace) {
|
if (this.trace) console.log('remote: set_state: %s', state);
|
||||||
console.log('remote: set_state: %s', state);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.state !== state) {
|
if (this.state !== state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
@@ -546,15 +499,13 @@ Remote.prototype._set_state = function(state) {
|
|||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case 'online':
|
case 'online':
|
||||||
this._online_state = 'open';
|
this._online_state = 'open';
|
||||||
this._connected = true;
|
|
||||||
this.emit('connect');
|
this.emit('connect');
|
||||||
this.emit('connected');
|
this.emit('connected');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'offline':
|
case 'offline':
|
||||||
this._online_state = 'closed';
|
this._online_state = 'closed';
|
||||||
this._connected = false;
|
|
||||||
this.emit('disconnect');
|
this.emit('disconnect');
|
||||||
this.emit('disconnected');
|
this.emit('disconnected');
|
||||||
break;
|
break;
|
||||||
@@ -571,13 +522,7 @@ Remote.prototype.set_trace = function(trace) {
|
|||||||
/**
|
/**
|
||||||
* Connect to the Ripple network.
|
* Connect to the Ripple network.
|
||||||
*/
|
*/
|
||||||
Remote.prototype.connect = function(online, callback) {
|
Remote.prototype.connect = function(online) {
|
||||||
if (typeof online === 'function') {
|
|
||||||
callback = online;
|
|
||||||
online = void(0);
|
|
||||||
this.once('connect', callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Downwards compatibility
|
// Downwards compatibility
|
||||||
if (typeof online !== 'undefined' && !online) {
|
if (typeof online !== 'undefined' && !online) {
|
||||||
this.disconnect();
|
this.disconnect();
|
||||||
@@ -590,7 +535,6 @@ Remote.prototype.connect = function(online, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -753,11 +697,15 @@ Remote.prototype._get_server = function() {
|
|||||||
// Send a request.
|
// Send a request.
|
||||||
// <-> request: what to send, consumed.
|
// <-> request: what to send, consumed.
|
||||||
Remote.prototype.request = function(request) {
|
Remote.prototype.request = function(request) {
|
||||||
var server = this._get_server();
|
if (!this._servers.length) {
|
||||||
if (server) {
|
request.emit('error', new Error('No servers available'));
|
||||||
server.request(request);
|
|
||||||
} else {
|
} else {
|
||||||
request.emit('error', new Error('No servers availale'));
|
var server = this._get_server();
|
||||||
|
if (server) {
|
||||||
|
server.request(request);
|
||||||
|
} else {
|
||||||
|
request.emit('error', new Error('No servers availale'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -803,7 +751,7 @@ Remote.prototype.request_ledger = function(ledger, opts, callback) {
|
|||||||
request.message.full = true;
|
request.message.full = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.callback(callback);
|
return request.callback(callback);;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only for unit testing.
|
// Only for unit testing.
|
||||||
@@ -839,8 +787,7 @@ Remote.prototype.request_ledger_entry = function(type, callback) {
|
|||||||
// If not found, listen, cache result, and emit it.
|
// If not found, listen, cache result, and emit it.
|
||||||
//
|
//
|
||||||
// Transparent caching:
|
// Transparent caching:
|
||||||
|
if ('account_root' === type) {
|
||||||
if (type === 'account_root') {
|
|
||||||
request.request_default = request.request;
|
request.request_default = request.request;
|
||||||
|
|
||||||
request.request = function() { // Intercept default request.
|
request.request = function() { // Intercept default request.
|
||||||
@@ -857,7 +804,7 @@ Remote.prototype.request_ledger_entry = function(type, callback) {
|
|||||||
}
|
}
|
||||||
// else if (req.ledger_index)
|
// else if (req.ledger_index)
|
||||||
// else if ('ripple_state' === request.type) // YYY Could be cached per ledger.
|
// else if ('ripple_state' === request.type) // YYY Could be cached per ledger.
|
||||||
else if (type === 'account_root') {
|
else if ('account_root' === type) {
|
||||||
var cache = self.ledgers.current.account_root;
|
var cache = self.ledgers.current.account_root;
|
||||||
|
|
||||||
if (!cache) {
|
if (!cache) {
|
||||||
@@ -910,7 +857,10 @@ Remote.prototype.request_subscribe = function(streams, callback) {
|
|||||||
var request = new Request(this, 'subscribe');
|
var request = new Request(this, 'subscribe');
|
||||||
|
|
||||||
if (streams) {
|
if (streams) {
|
||||||
request.message.streams = Array.isArray(streams) ? streams : [ streams ];
|
if ('object' !== typeof streams) {
|
||||||
|
streams = [streams];
|
||||||
|
}
|
||||||
|
request.message.streams = streams;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.callback(callback);
|
return request.callback(callback);
|
||||||
@@ -920,7 +870,10 @@ Remote.prototype.request_unsubscribe = function(streams, callback) {
|
|||||||
var request = new Request(this, 'unsubscribe');
|
var request = new Request(this, 'unsubscribe');
|
||||||
|
|
||||||
if (streams) {
|
if (streams) {
|
||||||
request.message.streams = Array.isArray(streams) ? streams : [ streams ];
|
if ('object' !== typeof streams) {
|
||||||
|
streams = [streams];
|
||||||
|
}
|
||||||
|
request.message.streams = streams;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.callback(callback);
|
return request.callback(callback);
|
||||||
@@ -932,7 +885,7 @@ Remote.prototype.request_unsubscribe = function(streams, callback) {
|
|||||||
Remote.prototype.request_transaction_entry = function(hash, callback) {
|
Remote.prototype.request_transaction_entry = function(hash, callback) {
|
||||||
//utils.assert(this.trusted); // If not trusted, need to check proof, maybe talk packet protocol.
|
//utils.assert(this.trusted); // If not trusted, need to check proof, maybe talk packet protocol.
|
||||||
|
|
||||||
return (new Request(this, 'transaction_entry')).tx_hash(hash).callback(callback);
|
return (new Request(this, 'transaction_entry', callback)).tx_hash(hash);
|
||||||
};
|
};
|
||||||
|
|
||||||
// DEPRECATED: use request_transaction_entry
|
// DEPRECATED: use request_transaction_entry
|
||||||
|
|||||||
Reference in New Issue
Block a user