Skip to content

Commit

Permalink
Enhancement: Rewrite Client.join to return a more useful promise.
Browse files Browse the repository at this point in the history
  • Loading branch information
Havvy committed Jan 20, 2015
1 parent 7dfd2b8 commit 2215a38
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 44 deletions.
23 changes: 1 addition & 22 deletions src/plugin/action/index.sjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,7 @@ module.exports = ActionPlugin = {
rawf("NOTICE %s :%s", target, body);
}

function join (channel) {
return new Promise(function (resolve, reject) {
var unsubscribe = function () {
logger.debug("Join response or timeout occured.");
client.off('join', onJoin);
};

var onJoin = function (join) {
if (join.nickname !== nickname() || join.channel !== channel) {
return;
}

unsubscribe();
logger.debug("Resolving with join body.");
resolve(join);
};

client.on('join', onJoin);

rawf("JOIN :%s", channel);
});
}
const join = require('./join')(client, rawf);


function part (channel, reason) {
Expand Down
101 changes: 79 additions & 22 deletions src/plugin/action/join.sjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,87 @@
/*
module.exports = function (client, action_module) {
const server = action_module.imports.server;
const format = require("util").format;
const Promise = require("bluebird");

return function join (channel) {
const deferred = Q.defer();
const rawf = action_module.exports.rawf;
const result = {};
module.exports = function (client, rawf) {
return function (channel) {
return new Promise(function (resolve, reject) {
if (channel === undefined || channel === "") {
reject(new Error("No channel given to join action."));
return;
}

const unsubscribe = function () {
messageHandler.off('join', onJoin);
};
const response = {
names: []
};

const onJoin = function (join) {
if (join.nickname !== client.nickname() || join.channel !== channel) {
return;
// Only listen to events for the channel we care about.
const forChannel = function (handler) {
return function (message) {
if (message.channel === channel) {
handler(message);
}
}
}

result = join;
deferred.resolve(result);
};
// const onJoin = forChannel(function (join) {
// // Nothing?
// });

const onTopic = forChannel(function (topic) {
response.topic = topic.topic;
});

const onTopicWhoTime = forChannel(function (topicWhoTime) {
response.topicChange = {
who: topicWhoTime.who,
timestamp: topicWhoTime.timestamp
};
});

const onNames = forChannel(function (names) {
response.names = response.names.concat(names);
});

const onNamesEnd = forChannel(function (endofnames) {
unsubscribe();
Promise.resolve(response);
});

const onJoinError = forChannel(function (err) {
unsubscribe();
Promise.reject(err);
});

const handlers = {
//join: onJoin,
rpl_topic: onTopic,
rpl_topicwhotime: onTopicWhoTime,
rpl_namreply: onNames,
rpl_endofnames: onNamesEnd,
err_nosuchchannel: onJoinError,
err_unavailresource: onJoinError,
err_channelisfull: onJoinError,
err_toomanychannels: onJoinError,
err_inviteonlychan: onJoinError,
err_bannedfromchan: onJoinError,
err_badchannelkey: onJoinError,
err_needreggednick: onJoinError,
err_operonly: onJoinError
};

client.on(handlers);

messageHandler.on('join', onJoin);
// Assume failure in an hour.
setTimeout(function () {
unsubscribe();
client.error(format("Attempt to join %s failed.", channel));
}, 3600 * 1000);

rawf("JOIN :%s", channel);
const unsubscribe = function () {
client.debug("PluginAction", format("Unsubscribing events for JOIN %s", channel));
client.off(handlers);
};

return deferred.promise;
}
};
*/
rawf("JOIN :%s", channel);
});
};
};

0 comments on commit 2215a38

Please sign in to comment.