Skip to content

Commit

Permalink
Merge branch 'master' into better-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
NotSugden authored Sep 17, 2020
2 parents b734da0 + c6a309e commit 321a9a3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 49 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14'
- run: "npm install"
- run: "npm run docs"
- uses: JamesIves/[email protected]
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: docs
FOLDER: docs-out
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules/**
package-lock.json
docs.json
browser.js
test/auth.js
.vscode
docs-out
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"lint": "eslint src test --ext=js",
"docs": "docgen --source src --output docs.json --jsdoc jsdoc.json --custom docgen.json",
"docs": "mkdir -p docs-out && docgen --source src --output docs-out/master.json --jsdoc jsdoc.json --custom docgen.json",
"example": "electron example/main.js",
"build:browser": "webpack-cli",
"prepublishOnly": "npm run lint && npm run build:browser"
Expand Down
8 changes: 3 additions & 5 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,10 @@ class RPCClient extends EventEmitter {
* @param {Snowflake} id ID of the voice channel
* @param {Object} [options] Options
* @param {number} [options.timeout] Timeout for the command
* @param {boolean} [options.force=false] Force this move. This should only be done if you
* have explicit permission from the user.
* @returns {Promise<Object<string, *>>}
*/
selectTextChannel(id, { timeout, force = false } = {}) {
return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout, force });
selectTextChannel(id, { timeout } = {}) {
return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout });
}

/**
Expand Down Expand Up @@ -847,7 +845,7 @@ class RPCClient extends EventEmitter {
* @returns {Promise<undefined>}
*/
async destroy() {
this.transport.close();
await this.transport.close();
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/transports/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@ class IPCTransport extends EventEmitter {
return;
}
if (data.cmd === 'AUTHORIZE' && data.evt !== 'ERROR') {
findEndpoint().then((endpoint) => {
this.client.request.endpoint = endpoint;
});
findEndpoint()
.then((endpoint) => {
this.client.request.endpoint = endpoint;
})
.catch((e) => {
this.client.emit('error', e);
});
}
this.emit('message', data);
break;
Expand All @@ -151,9 +155,12 @@ class IPCTransport extends EventEmitter {
this.socket.write(encode(op, data));
}

close() {
this.send({}, OPCodes.CLOSE);
this.socket.end();
async close() {
return new Promise((r) => {
this.once('close', r);
this.send({}, OPCodes.CLOSE);
this.socket.end();
});
}

ping() {
Expand Down
69 changes: 36 additions & 33 deletions src/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,62 @@ class WebSocketTransport extends EventEmitter {
this.tries = 0;
}

async connect(tries = this.tries) {
if (this.connected) {
return;
}
const port = 6463 + (tries % 10);
this.hostAndPort = `127.0.0.1:${port}`;
const ws = this.ws = new WebSocket(
`ws://${this.hostAndPort}/?v=1&client_id=${this.client.clientId}`,
async connect() {
const port = 6463 + (this.tries % 10);
this.tries += 1;

this.ws = new WebSocket(
`ws://127.0.0.1:${port}/?v=1&client_id=${this.client.clientId}`,
{
origin: this.client.options.origin,
},
);
ws.onopen = this.onOpen.bind(this);
ws.onclose = ws.onerror = this.onClose.bind(this);
ws.onmessage = this.onMessage.bind(this);
this.ws.onopen = this.onOpen.bind(this);
this.ws.onclose = this.onClose.bind(this);
this.ws.onerror = this.onError.bind(this);
this.ws.onmessage = this.onMessage.bind(this);
}

send(data) {
if (!this.ws) {
return;
}
this.ws.send(pack(data));
onOpen() {
this.emit('open');
}

close() {
if (!this.ws) {
onClose(event) {
if (!event.wasClean) {
return;
}
this.ws.close();
this.emit('close', event);
}

ping() {} // eslint-disable-line no-empty-function
onError(event) {
try {
this.ws.close();
} catch {} // eslint-disable-line no-empty

if (this.tries > 20) {
this.emit('error', event.error);
} else {
setTimeout(() => {
this.connect();
}, 250);
}
}

onMessage(event) {
this.emit('message', unpack(event.data));
}

onOpen() {
this.emit('open');
send(data) {
this.ws.send(pack(data));
}

onClose(e) {
try {
ping() {} // eslint-disable-line no-empty-function

close() {
return new Promise((r) => {
this.once('close', r);
this.ws.close();
} catch (err) {} // eslint-disable-line no-empty
const derr = e.code >= 4000 && e.code < 5000;
if (!e.code || derr) {
this.emit('close', e);
}
if (!derr) {
// eslint-disable-next-line no-plusplus
setTimeout(() => this.connect(e.code === 1006 ? ++this.tries : 0), 250);
}
});
}
}

Expand Down
10 changes: 7 additions & 3 deletions test/rp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ const client = new Client({
transport: 'ipc',
});

client.on('ready', () => {
client.subscribe('MESSAGE_CREATE', { channel_id: '381886868708655104' }, console.log)
.catch(console.error);
client.on('ready', async () => {
await client.selectTextChannel('201803114049699849');
console.log(await client.getChannel('201803114049699849'));
client.destroy()
.then(() => {
console.log('closed!');
});
});

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

0 comments on commit 321a9a3

Please sign in to comment.