Skip to content

Commit

Permalink
commands: make join res/rej based on channel name
Browse files Browse the repository at this point in the history
Fixed #342
  • Loading branch information
AlcaDesign committed Jun 15, 2019
1 parent 4d504f6 commit 226cd5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
12 changes: 7 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ client.prototype.handleMessage = function handleMessage(message) {
"_promiseSubscribersoff",
"_promiseEmoteonly",
"_promiseEmoteonlyoff"
], noticeAndMsgid);
], noticeAndMsgid, channel);
break;

// Unrecognized command..
Expand Down Expand Up @@ -812,11 +812,11 @@ client.prototype.handleMessage = function handleMessage(message) {
// The message on join contains all room settings.
case "ROOMSTATE":
// We use this notice to know if we successfully joined a channel..
if (_.channel(this.lastJoined) === _.channel(message.params[0])) { this.emit("_promiseJoin", null); }
if (_.channel(this.lastJoined) === channel) { this.emit("_promiseJoin", null, channel); }

// Provide the channel name in the tags before emitting it..
message.tags.channel = _.channel(message.params[0]);
this.emit("roomstate", _.channel(message.params[0]), message.tags);
message.tags.channel = channel;
this.emit("roomstate", channel, message.tags);

if (!message.tags.hasOwnProperty("subs-only")) {
// Handle slow mode here instead of the slow_on/off notice..
Expand Down Expand Up @@ -1157,7 +1157,9 @@ client.prototype._getPromiseDelay = function _getPromiseDelay() {
client.prototype._sendCommand = function _sendCommand(delay, channel, command, fn) {
// Race promise against delay..
return new Promise((resolve, reject) => {
_.promiseDelay(delay).then(() => { reject("No response from Twitch."); });
if(typeof delay === 'number') {
_.promiseDelay(delay).then(() => { reject("No response from Twitch."); });
}

// Make sure the socket is opened..
if (!_.isNull(this.ws) && this.ws.readyState !== 2 && this.ws.readyState !== 3) {
Expand Down
22 changes: 15 additions & 7 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,21 @@ module.exports = {
join: function join(channel) {
channel = _.channel(channel);

// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), null, `JOIN ${channel}`, (resolve, reject) => {
// Received _promiseJoin event, resolve or reject..
this.once("_promiseJoin", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
// Send the command to the server ..
return this._sendCommand(null, null, `JOIN ${channel}`, (resolve, reject) => {
var eventName = "_promiseJoin";
var listener = (err, joinedChannel) => {
if(channel === _.channel(joinedChannel)) {
// Received _promiseJoin event for the target channel, resolve or reject..
this.removeListener(eventName, listener);
if(!err) { resolve([channel]); }
else { reject(err); }
}
};
this.on(eventName, listener);
// Race the Promise against a delay..
var delay = this._getPromiseDelay();
_.promiseDelay(delay).then(() => { this.emit(eventName, "No response from Twitch."); });
});
},

Expand Down

0 comments on commit 226cd5a

Please sign in to comment.