-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Rewrite Client.join to return a more useful promise.
- Loading branch information
Showing
2 changed files
with
80 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; | ||
}; |