Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

* Added Away / Back support to smith #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions smith.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,41 @@ inherits(Agent, EventEmitter);
Agent.prototype.connectionTimeout = 10000;

Agent.prototype.connect = function (transport, callback) {
var connection;

if (transport.transport) {
connection = transport;
transport = transport.transport;
}

// If they passed in a raw stream, wrap it.
if (!(transport instanceof Transport)) transport = new Transport(transport);

this.transport = transport;
this.callbacks = {};
this.nextKey = 1;
transport.on("error", this.disconnect);
transport.on("disconnect", this.disconnect);
transport.on("message", this._onMessage);

if (connection) {
var agent = this;

connection.on("disconnect", this.disconnect);
connection.on("away", function(){

});
connection.on("back", function back(e){
if (e)
connection.transport = e.transport;

agent.transport = connection.transport;
agent.transport.on("message", agent._onMessage);
agent.transport.on("drain", agent._onDrain);
});
}
else {
transport.on("disconnect", this.disconnect);
}
transport.on("message", this._onMessage);
transport.on("drain", this._onDrain);

// Handshake with the other end
Expand Down Expand Up @@ -365,9 +391,11 @@ Agent.prototype._onReady = function (names, env) {
if (!self.transport) {
var callback = arguments[arguments.length - 1];
if (typeof callback === "function") {
var err = new Error("ENOTCONNECTED: Agent is offline, try again later");
err.code = "ENOTCONNECTED";
callback(err);
setTimeout(function(){
var err = new Error("ENOTCONNECTED: Agent is offline, try again later");
err.code = "ENOTCONNECTED";
callback(err);
}, 10);
}
return;
}
Expand All @@ -388,7 +416,7 @@ Agent.prototype._emitConnect = function () {
// "disconnect" event with optional error object.
Agent.prototype.disconnect = function (err) {
if (!this.transport) {
if (err) this.emit("error", err);
// Agent is already disconnected
return;
}

Expand Down