Skip to content

Change sys.write to async in tsserver #5354

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

Merged
merged 3 commits into from
Oct 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ts.server {
input: process.stdin,
output: process.stdout,
terminal: false,
});
});

class Logger implements ts.server.Logger {
fd = -1;
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace ts.server {
isVerbose() {
return this.loggingEnabled() && (this.level == "verbose");
}


msg(s: string, type = "Err") {
if (this.fd < 0) {
Expand Down Expand Up @@ -89,18 +89,18 @@ namespace ts.server {
}

exit() {
this.projectService.log("Exiting...","Info");
this.projectService.log("Exiting...", "Info");
this.projectService.closeLog();
process.exit(0);
}

listen() {
rl.on('line',(input: string) => {
rl.on('line', (input: string) => {
var message = input.trim();
this.onMessage(message);
});

rl.on('close',() => {
rl.on('close', () => {
this.exit();
});
}
Expand Down Expand Up @@ -155,6 +155,38 @@ namespace ts.server {

var logger = createLoggerFromEnv();

let pending: string[] = [];
function queueMessage(s: string) {
pending.push(s);
if (pending.length === 1) {
drain();
}
}

function drain() {
Debug.assert(pending.length > 0);
writeBuffer(new Buffer(pending[0], "utf8"), 0);
}

function writeBuffer(buffer: Buffer, offset: number) {
const toWrite = buffer.length - offset;
fs.write(1, buffer, offset, toWrite, undefined, (err, written, buffer) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we care about errors here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are retrying instead of handling the error anyway, it may only be used for printing error messages I think.

if (toWrite > written) {
writeBuffer(buffer, offset + written);
}
else {
Debug.assert(pending.length > 0);
pending.shift();
if (pending.length > 0) {
drain();
}
}
});
}

// Override sys.write because fs.writeSync is not reliable on Node 4
ts.sys.write = (s: string) => queueMessage(s);

var ioSession = new IOSession(ts.sys, logger);
process.on('uncaughtException', function(err: Error) {
ioSession.logError(err, "unknown");
Expand Down