-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add typescript dependency, check jsdoc types #38
Conversation
- Add typescript dependency - Add test-types npm script - Call npm test-types in npm test script - Add CommandServer class extends WebSocketServer - Add @types for dependencies that need them
clientTracking: true, | ||
path: '/session', | ||
port, | ||
}); | ||
await new Promise(resolve => server.once('listening', resolve)); | ||
|
||
server.broadcast = broadcast.bind(null, server); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a key not defined on the type (in this case WebSocketServer
) produces an error. So I extended WebSocketServer
as CommandServer
and added the method to it.
lib/create-voice-server.js
Outdated
@@ -45,3 +45,5 @@ module.exports = function createVoiceServer(handle) { | |||
server.on('connection', onConnection.bind(null, server)); | |||
}); | |||
}; | |||
|
|||
/** @typedef {import("events").EventEmitter} EventEmitter */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@types/node
doesn't add EventEmitter globally. Alternatively to this we could const {EventEmitter} = require('events')
at the top of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that alternative leaves a dangling import. Since EventEmitter isn't used in the code, only in comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for offering alternatives, but I agree with the choice you've made here. I prefer declaring types at the top of the file, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer declaring types at the top as well. Applying that how should we manage what the comment modifies? Like in this file if it is put after the imports, it would be documenting onConnection
.
I see two solutions for that while keeping the type in the file:
- Add a comment documenting a line of code between typedef and none code comments.
- Put comments like these at the end of the file.
To make the PR smaller and easier to review I chose the second.
I'll put these typedefs to the top and add comments like stated in 1. Does that sound good? Do you have something else in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm. Are you asking for a change to be made here? On second review of your comment I'm not sure if you are commenting on it or asking for a change.
If so, I'd be happy to do the stroke-out section I stated:
I'll put these typedefs to the top and add comments like stated in 1. Does that sound good? Do you have something else in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was asking for a change, although I thought @typedef
always works as a standalone declaration. If we have to insert some weird comment as a delimiter, then I guess that's still slightly better than having types squirreled away at the bottom. Is a delimiter like that really necessary, though? On my machine, the compiler seems fine with this change:
diff --git a/lib/create-voice-server.js b/lib/create-voice-server.js
index c1386c4..e14f78d 100644
--- a/lib/create-voice-server.js
+++ b/lib/create-voice-server.js
@@ -2,6 +2,8 @@
const net = require('net');
+/** @typedef {import("events").EventEmitter} EventEmitter */
+
const onConnection = (server, socket) => {
let emitted = '';
socket.on('data', buffer => (emitted += buffer.toString()));
@@ -46,4 +48,3 @@ module.exports = function createVoiceServer(handle) {
});
};
-/** @typedef {import("events").EventEmitter} EventEmitter */
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could also be wrong and remembering something from an older version of typescript? I retract that stuff I said.
(It's pretty nice to see I'm wrong on this. I super thought this was a thing.)
@@ -19,7 +19,7 @@ const DEFAULT_PORT = 4382; | |||
const log = (...args) => console.error(new Date().toISOString(), ...args); | |||
|
|||
module.exports = async process => { | |||
const argv = yargs(hideBin(process.argv)) | |||
const argv = await yargs(hideBin(process.argv)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yargs
can return a promise. So I added an await
here in case that happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @mzgoddard!
lib/create-voice-server.js
Outdated
@@ -45,3 +45,5 @@ module.exports = function createVoiceServer(handle) { | |||
server.on('connection', onConnection.bind(null, server)); | |||
}); | |||
}; | |||
|
|||
/** @typedef {import("events").EventEmitter} EventEmitter */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for offering alternatives, but I agree with the choice you've made here. I prefer declaring types at the top of the file, though.
@@ -70,27 +58,56 @@ const onConnection = websocket => { | |||
}); | |||
}; | |||
|
|||
class CommandServer extends WebSocketServer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful
*/ | ||
broadcast(message) { | ||
const packedMessage = JSON.stringify(message); | ||
/** @type {Set<WebSocketWithData>} */ (this.clients).forEach(websocket => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of makes me feel like we should be using a WeakMap to associate the session ID. Not in this patch, of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that sounds like a good idea. Adding post-hoc properties like sessionId
makes it hard to track with type analysis where it is assigned or used.
I moved |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. On.
@types
for dependencies that need them