Skip to content

Commit b4ecc6c

Browse files
committed
bit of cleanup
1 parent 1104b27 commit b4ecc6c

File tree

4 files changed

+59
-46
lines changed

4 files changed

+59
-46
lines changed

src/client.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,11 @@ class RPCClient extends EventEmitter {
376376
* @param {Snowflake} id ID of the voice channel
377377
* @param {Object} [options] Options
378378
* @param {number} [options.timeout] Timeout for the command
379-
* @param {boolean} [options.force] Force this move. This should only be done if you
380379
* have explicit permission from the user.
381380
* @returns {Promise}
382381
*/
383-
selectTextChannel(id, { timeout, force = false } = {}) {
384-
return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout, force });
382+
selectTextChannel(id, { timeout } = {}) {
383+
return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout });
385384
}
386385

387386
/**
@@ -672,7 +671,7 @@ class RPCClient extends EventEmitter {
672671
* Destroy the client
673672
*/
674673
async destroy() {
675-
this.transport.close();
674+
await this.transport.close();
676675
}
677676
}
678677

src/transports/ipc.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@ class IPCTransport extends EventEmitter {
127127
return;
128128
}
129129
if (data.cmd === 'AUTHORIZE' && data.evt !== 'ERROR') {
130-
findEndpoint().then((endpoint) => {
131-
this.client.request.endpoint = endpoint;
132-
});
130+
findEndpoint()
131+
.then((endpoint) => {
132+
this.client.request.endpoint = endpoint;
133+
})
134+
.catch((e) => {
135+
this.client.emit('error', e);
136+
});
133137
}
134138
this.emit('message', data);
135139
break;
@@ -151,9 +155,12 @@ class IPCTransport extends EventEmitter {
151155
this.socket.write(encode(op, data));
152156
}
153157

154-
close() {
155-
this.send({}, OPCodes.CLOSE);
156-
this.socket.end();
158+
async close() {
159+
return new Promise((r) => {
160+
this.once('close', r);
161+
this.send({}, OPCodes.CLOSE);
162+
this.socket.end();
163+
});
157164
}
158165

159166
ping() {

src/transports/websocket.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,62 @@ class WebSocketTransport extends EventEmitter {
1717
this.tries = 0;
1818
}
1919

20-
async connect(tries = this.tries) {
21-
if (this.connected) {
22-
return;
23-
}
24-
const port = 6463 + (tries % 10);
25-
this.hostAndPort = `127.0.0.1:${port}`;
26-
const ws = this.ws = new WebSocket(
27-
`ws://${this.hostAndPort}/?v=1&client_id=${this.client.clientId}`,
20+
async connect() {
21+
const port = 6463 + (this.tries % 10);
22+
this.tries += 1;
23+
24+
this.ws = new WebSocket(
25+
`ws://127.0.0.1:${port}/?v=1&client_id=${this.client.clientId}`,
2826
{
2927
origin: this.client.options.origin,
3028
},
3129
);
32-
ws.onopen = this.onOpen.bind(this);
33-
ws.onclose = ws.onerror = this.onClose.bind(this);
34-
ws.onmessage = this.onMessage.bind(this);
30+
this.ws.onopen = this.onOpen.bind(this);
31+
this.ws.onclose = this.onClose.bind(this);
32+
this.ws.onerror = this.onError.bind(this);
33+
this.ws.onmessage = this.onMessage.bind(this);
3534
}
3635

37-
send(data) {
38-
if (!this.ws) {
39-
return;
40-
}
41-
this.ws.send(pack(data));
36+
onOpen() {
37+
this.emit('open');
4238
}
4339

44-
close() {
45-
if (!this.ws) {
40+
onClose(event) {
41+
if (!event.wasClean) {
4642
return;
4743
}
48-
this.ws.close();
44+
this.emit('close', event);
4945
}
5046

51-
ping() {} // eslint-disable-line no-empty-function
47+
onError(event) {
48+
try {
49+
this.ws.close();
50+
} catch {} // eslint-disable-line no-empty
51+
52+
if (this.tries > 20) {
53+
this.emit('error', event.error);
54+
} else {
55+
setTimeout(() => {
56+
this.connect();
57+
}, 250);
58+
}
59+
}
5260

5361
onMessage(event) {
5462
this.emit('message', unpack(event.data));
5563
}
5664

57-
onOpen() {
58-
this.emit('open');
65+
send(data) {
66+
this.ws.send(pack(data));
5967
}
6068

61-
onClose(e) {
62-
try {
69+
ping() {} // eslint-disable-line no-empty-function
70+
71+
close() {
72+
return new Promise((r) => {
73+
this.once('close', r);
6374
this.ws.close();
64-
} catch (err) {} // eslint-disable-line no-empty
65-
const derr = e.code >= 4000 && e.code < 5000;
66-
if (!e.code || derr) {
67-
this.emit('close', e);
68-
}
69-
if (!derr) {
70-
// eslint-disable-next-line no-plusplus
71-
setTimeout(() => this.connect(e.code === 1006 ? ++this.tries : 0), 250);
72-
}
75+
});
7376
}
7477
}
7578

test/rp.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ const client = new Client({
1212
transport: 'ipc',
1313
});
1414

15-
client.on('ready', () => {
16-
client.subscribe('MESSAGE_CREATE', { channel_id: '381886868708655104' }, console.log)
17-
.catch(console.error);
15+
client.on('ready', async () => {
16+
await client.selectTextChannel('201803114049699849');
17+
console.log(await client.getChannel('201803114049699849'));
18+
client.destroy()
19+
.then(() => {
20+
console.log('closed!');
21+
});
1822
});
1923

2024
client.login(require('./auth')).catch(console.error);

0 commit comments

Comments
 (0)