Promisify

This commit is contained in:
Amish Shah
2016-08-23 21:04:15 +01:00
parent e370ccf806
commit d0a76f3f38
3 changed files with 25 additions and 10 deletions

File diff suppressed because one or more lines are too long

View File

@@ -32,8 +32,8 @@ class ClientVoiceManager {
throw new Error('Guild not pending');
}
if (pendingRequest.token && pendingRequest.sessionID && pendingRequest.endpoint) {
const { token, sessionID, endpoint } = pendingRequest;
const voiceConnection = new VoiceConnection(this, guildID, token, sessionID, endpoint);
const { token, sessionID, endpoint, resolve, reject } = pendingRequest;
const voiceConnection = new VoiceConnection(this, guildID, token, sessionID, endpoint, resolve, reject);
this.connections.set(guildID, voiceConnection);
}
}
@@ -93,13 +93,17 @@ class ClientVoiceManager {
* @returns {null}
*/
joinChannel(channel) {
this.pending.set(channel.guild.id, {
channel,
sessionID: null,
token: null,
endpoint: null,
return new Promise((resolve, reject) => {
this.pending.set(channel.guild.id, {
channel,
sessionID: null,
token: null,
endpoint: null,
resolve,
reject,
});
this._sendWSJoin(channel);
});
this._sendWSJoin(channel);
}
}

View File

@@ -3,21 +3,32 @@ const VoiceConnectionUDPClient = require('./VoiceConnectionUDPClient');
const EventEmitter = require('events').EventEmitter;
class VoiceConnection extends EventEmitter {
constructor(manager, serverID, token, sessionID, endpoint) {
constructor(manager, serverID, token, sessionID, endpoint, resolve, reject) {
super();
this.manager = manager;
this.endpoint = endpoint;
this.websocket = new VoiceConnectionWebSocket(this, serverID, token, sessionID, endpoint);
this.ready = false;
this._resolve = resolve;
this._reject = reject;
this.bindListeners();
}
_onError(e) {
this._reject(e);
this.emit('error', e);
}
bindListeners() {
this.websocket.on('error', err => this._onError(err));
this.websocket.on('ready-for-udp', data => {
this.udp = new VoiceConnectionUDPClient(this, data);
this.udp.on('error', err => this._onError(err));
});
this.websocket.on('ready', () => {
this.ready = true;
this.emit('ready');
this._resolve(this);
});
}
}