From dff2ff4a900a4aa3b9b4d23464d00f979d4d34f5 Mon Sep 17 00:00:00 2001 From: mscdex Date: Sun, 21 Jul 2013 20:23:35 -0400 Subject: [PATCH] Connection: make end() more graceful, add destroy() for more forceful --- README.md | 4 +++- lib/Connection.js | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d1b196..5eaffa8 100644 --- a/README.md +++ b/README.md @@ -396,7 +396,9 @@ Connection Instance Methods * **connect**() - _(void)_ - Attempts to connect and authenticate with the IMAP server. -* **end**() - _(void)_ - Closes the connection to the server. +* **end**() - _(void)_ - Closes the connection to the server after all requests in the queue have been sent. + +* **destroy**() - _(void)_ - Immediately destroys the connection to the server. * **openBox**(< _string_ >mailboxName[, < _boolean_ >openReadOnly=false[, < _object_ >modifiers]], < _function_ >callback) - _(void)_ - Opens a specific mailbox that exists on the server. `mailboxName` should include any necessary prefix/path. `modifiers` is used by IMAP extensions. `callback` has 2 parameters: < _Error_ >err, < _Box_ >mailbox. diff --git a/lib/Connection.js b/lib/Connection.js index 478b88d..62ee8c4 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -67,6 +67,7 @@ function Connection(config) { this._idle = {}; this._parser = undefined; this._curReq = undefined; + this._ending = false; this.delimiter = undefined; this.namespaces = undefined; this.state = 'disconnected'; @@ -80,6 +81,17 @@ Connection.prototype.connect = function() { socket = new Socket(); socket.setKeepAlive(true); socket.setTimeout(0); + this._sock = undefined; + this._tagcount = 0; + this._tmrConn = undefined; + this._queue = []; + this._box = undefined; + this._idle = {}; + this._parser = undefined; + this._curReq = undefined; + this._ending = false; + this.delimiter = undefined; + this.namespaces = undefined; this.state = 'disconnected'; if (config.tls) { @@ -159,7 +171,6 @@ Connection.prototype.connect = function() { stream.resume(); // a body we didn't ask for? }); parser.on('continue', function(info) { - // only needed for IDLE and APPEND var type = self._curReq.type; if (type === 'IDLE') { // now idling @@ -217,10 +228,19 @@ Connection.prototype.serverSupports = function(cap) { return (this._caps && this._caps.indexOf(cap) > -1); }; -Connection.prototype.end = function() { +Connection.prototype.destroy = function() { + this._queue = []; + this._curReq = undefined; this._sock.end(); }; +Connection.prototype.end = function() { + var self = this; + this._enqueue('LOGOUT', function() { + self.destroy(); + }); +}; + Connection.prototype.append = function(data, options, cb) { if (typeof options === 'function') { cb = options; @@ -1498,7 +1518,7 @@ Connection.prototype._starttls = function() { }; Connection.prototype._processQueue = function() { - if (this._curReq || !this._queue.length || !this._sock.writable) + if (this._curReq || !this._queue.length || !this._sock || !this._sock.writable) return; this._curReq = this._queue.shift();