Skip to content

Commit e4f23cb

Browse files
authored
Merge branch 'master' into refactor-clean-code
2 parents 7b44515 + c6a309e commit e4f23cb

File tree

7 files changed

+74
-45
lines changed

7 files changed

+74
-45
lines changed

.github/workflows/docs.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: actions/setup-node@v1
12+
with:
13+
node-version: '14'
14+
- run: "npm install"
15+
- run: "npm run docs"
16+
- uses: JamesIves/[email protected]
17+
with:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
BRANCH: docs
20+
FOLDER: docs-out

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node_modules/**
22
package-lock.json
3-
docs.json
43
browser.js
54
test/auth.js
65
.vscode
6+
docs-out

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"scripts": {
2525
"lint": "eslint src test --ext=js",
26-
"docs": "docgen --source src --output docs.json --jsdoc jsdoc.json --custom docgen.json",
26+
"docs": "mkdir -p docs-out && docgen --source src --output docs-out/master.json --jsdoc jsdoc.json --custom docgen.json",
2727
"example": "electron example/main.js",
2828
"build:browser": "webpack-cli",
2929
"prepublishOnly": "npm run lint && npm run build:browser"

src/client.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,11 @@ class RPCClient extends EventEmitter {
468468
* @param {Snowflake} id ID of the voice channel
469469
* @param {Object} [options] Options
470470
* @param {number} [options.timeout] Timeout for the command
471-
* @param {boolean} [options.force] Force this move. This should only be done if you
472471
* have explicit permission from the user.
473472
* @returns {Promise}
474473
*/
475-
selectTextChannel(id, { timeout, force = false } = {}) {
476-
return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout, force });
474+
selectTextChannel(id, { timeout } = {}) {
475+
return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout });
477476
}
478477

479478
/**
@@ -758,7 +757,7 @@ class RPCClient extends EventEmitter {
758757
* Destroy the client
759758
*/
760759
async destroy() {
761-
this.transport.close();
760+
await this.transport.close();
762761
this._removeListeners(true);
763762
this.emit('destroyed');
764763
}

src/transports/ipc.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,12 @@ class IPCTransport extends EventEmitter {
152152
this.socket.write(encode(op, data));
153153
}
154154

155-
close() {
156-
this.send({}, OPCodes.CLOSE);
157-
this.socket.end();
155+
async close() {
156+
return new Promise((r) => {
157+
this.once('close', r);
158+
this.send({}, OPCodes.CLOSE);
159+
this.socket.end();
160+
});
158161
}
159162

160163
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)