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'); throw new Error('Guild not pending');
} }
if (pendingRequest.token && pendingRequest.sessionID && pendingRequest.endpoint) { if (pendingRequest.token && pendingRequest.sessionID && pendingRequest.endpoint) {
const { token, sessionID, endpoint } = pendingRequest; const { token, sessionID, endpoint, resolve, reject } = pendingRequest;
const voiceConnection = new VoiceConnection(this, guildID, token, sessionID, endpoint); const voiceConnection = new VoiceConnection(this, guildID, token, sessionID, endpoint, resolve, reject);
this.connections.set(guildID, voiceConnection); this.connections.set(guildID, voiceConnection);
} }
} }
@@ -93,13 +93,17 @@ class ClientVoiceManager {
* @returns {null} * @returns {null}
*/ */
joinChannel(channel) { joinChannel(channel) {
this.pending.set(channel.guild.id, { return new Promise((resolve, reject) => {
channel, this.pending.set(channel.guild.id, {
sessionID: null, channel,
token: null, sessionID: null,
endpoint: 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; const EventEmitter = require('events').EventEmitter;
class VoiceConnection extends EventEmitter { class VoiceConnection extends EventEmitter {
constructor(manager, serverID, token, sessionID, endpoint) { constructor(manager, serverID, token, sessionID, endpoint, resolve, reject) {
super(); super();
this.manager = manager; this.manager = manager;
this.endpoint = endpoint; this.endpoint = endpoint;
this.websocket = new VoiceConnectionWebSocket(this, serverID, token, sessionID, endpoint); this.websocket = new VoiceConnectionWebSocket(this, serverID, token, sessionID, endpoint);
this.ready = false; this.ready = false;
this._resolve = resolve;
this._reject = reject;
this.bindListeners(); this.bindListeners();
} }
_onError(e) {
this._reject(e);
this.emit('error', e);
}
bindListeners() { bindListeners() {
this.websocket.on('error', err => this._onError(err));
this.websocket.on('ready-for-udp', data => { this.websocket.on('ready-for-udp', data => {
this.udp = new VoiceConnectionUDPClient(this, data); this.udp = new VoiceConnectionUDPClient(this, data);
this.udp.on('error', err => this._onError(err));
}); });
this.websocket.on('ready', () => { this.websocket.on('ready', () => {
this.ready = true; this.ready = true;
this.emit('ready');
this._resolve(this);
}); });
} }
} }