Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional callbacks to connect and disconnect #44

Merged
merged 4 commits into from
Oct 27, 2011
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ You can read more about the IRC protocol by reading [RFC
Emitted when ever the server responds with an error-type message. The message
parameter is exactly as in the 'raw' event.

### Client.connect(retryCount, callback)

Connects to the server. Used when `autoConnect` in the options is set to false. If `retryCount` is a function it will be treated as the `callback`.

### Client.send(command, arg1, arg2, ...)

Sends a raw message to the server, generally speaking it's best not to use this
Expand Down Expand Up @@ -242,6 +246,6 @@ described above.

`target` is either a nickname, or a channel.

### Client.disconnect(message)
### Client.disconnect(message, callback)

Disconnects from the IRC server sending the specified parting message.
Disconnects from the IRC server sending the specified parting message. If `message` is a function it will be treated as the `callback`.
32 changes: 19 additions & 13 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,14 @@ util.inherits(Client, process.EventEmitter);
Client.prototype.conn = null;
Client.prototype.chans = {};
Client.prototype._whoisData = {};
Client.prototype.connect = function ( retryCount ) { // {{{
retryCount = retryCount || 0;
Client.prototype.connect = function ( retryCount, callback ) { // {{{
if (!retryCount || typeof(retryCount) === 'function') {
callback = retryCount;
retryCount = 0;
}
if (typeof(callback) === 'function') {
this.once('registered', callback);
}
var self = this;
self.chans = {};
// try to connect to the server
Expand Down Expand Up @@ -918,13 +924,19 @@ Client.prototype.connect = function ( retryCount ) { // {{{
}, self.opt.retryDelay );
});
}; // }}}
Client.prototype.disconnect = function ( message ) { // {{{
message = message || "node-irc says goodbye";
Client.prototype.disconnect = function ( message, callback ) { // {{{
if (!message || typeof(message) === 'function') {
callback = message;
message = "node-irc says goodbye";
}
var self = this;
if ( self.conn.readyState == 'open' ) {
self.send( "QUIT", message );
}
self.conn.requestedDisconnect = true;
if (typeof(callback) === 'function') {
self.conn.once('end', callback);
}
self.conn.end();
}; // }}}
Client.prototype.send = function(command) { // {{{
Expand Down Expand Up @@ -971,8 +983,7 @@ Client.prototype.activateFloodProtection = function() { // {{{

}; // }}}
Client.prototype.join = function(channel, callback) { // {{{
var callbackWrapper = function () {
this.removeListener('join' + channel, callbackWrapper);
this.once('join' + channel, function () {
// if join is successful, add this channel to opts.channels
// so that it will be re-joined upon reconnect (as channels
// specified in options are)
Expand All @@ -983,17 +994,12 @@ Client.prototype.join = function(channel, callback) { // {{{
if ( typeof(callback) == 'function' ) {
return callback.apply(this, arguments);
}
};
this.addListener('join' + channel, callbackWrapper);
});
this.send('JOIN', channel);
} // }}}
Client.prototype.part = function(channel, callback) { // {{{
if ( typeof(callback) == 'function' ) {
var callbackWrapper = function () {
this.removeListener('part' + channel, callbackWrapper);
return callback.apply(this, arguments);
};
this.addListener('part' + channel, callbackWrapper);
this.once('part' + channel, callback);
}

// remove this channel from this.opt.channels so we won't rejoin
Expand Down