Skip to content

Commit faea62c

Browse files
committed
Update socket
1 parent 8596a89 commit faea62c

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

R/notebook.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ local({
77
eval(expr, env)
88
}
99

10-
print(ls.str(env))
10+
ls.str(env)
1111
while (TRUE) {
12+
cat("Waiting\n")
1213
con <- socketConnection(host = "127.0.0.1", port = env$port,
1314
blocking = TRUE, server = TRUE,
1415
open = "r+")
@@ -22,7 +23,7 @@ local({
2223
type = if (inherits(res, "try-error")) "error" else "output",
2324
result = utils::capture.output(print(res))
2425
)
25-
result <- jsonlite::toJSON(str, force = TRUE)
26+
result <- jsonlite::toJSON(str, auto_unbox = TRUE, force = TRUE)
2627
writeLines(result, con)
2728
close(con)
2829
}

src/notebook.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import net = require('net');
33
import { spawn, ChildProcess } from 'child_process';
4-
import { dirname } from 'path';
4+
import { dirname, resolve } from 'path';
55
import getPort = require('get-port');
66

77
class RKernel {
@@ -24,14 +24,18 @@ class RKernel {
2424
env.LANG = 'en_US.UTF-8';
2525

2626
this.port = await getPort();
27-
const childProcess = spawn('R', ['--quite', '--slave', '-f', this.kernelScript, '--args', `port=${this.port}`],
27+
const childProcess = spawn('R', ['--quiet', '--slave', '-f', this.kernelScript, '--args', `port=${this.port}`],
2828
{ cwd: this.cwd, env: env });
2929
childProcess.stderr.on('data', (chunk: Buffer) => {
3030
const str = chunk.toString();
31-
console.log(`R process (${childProcess.pid}): ${str}`);
31+
console.log(`R stderr (${childProcess.pid}): ${str}`);
32+
});
33+
childProcess.stdout.on('data', (chunk: Buffer) => {
34+
const str = chunk.toString();
35+
console.log(`R stdout (${childProcess.pid}): ${str}`);
3236
});
3337
childProcess.on('exit', (code, signal) => {
34-
console.log(`R process exited with code ${code}`);
38+
console.log(`R exited with code ${code}`);
3539
});
3640
this.process = childProcess;
3741
}
@@ -50,33 +54,29 @@ class RKernel {
5054

5155
public async eval(cell: vscode.NotebookCell): Promise<string> {
5256
if (this.process) {
53-
let outputBuffer = '';
54-
await new Promise(res => setTimeout(res, 1000));
55-
5657
const client = net.createConnection({ port: this.port }, () => {
5758
console.log('connected to server!');
59+
const json = `{"time":"${Date.now().toString()}","expr":"rnorm(3)"}\n`;
60+
console.log(`Send: ${json}`);
61+
client.write(json);
62+
});
63+
64+
client.on('end', () => {
65+
console.log('disconnected from server');
66+
});
67+
68+
return new Promise((resolve, reject) => {
5869
client.on('data', (data) => {
5970
const result = data.toString();
6071
console.log(result);
61-
outputBuffer += result;
6272
client.end();
73+
resolve(result);
6374
});
6475

65-
client.on('end', () => {
66-
console.log('disconnected from server');
67-
});
68-
69-
const json = JSON.stringify({
70-
time: Date.now(),
71-
uri: cell.uri.toString(),
72-
expr: '1+1',
76+
client.on('error', (err) => {
77+
reject(err.message);
7378
});
74-
75-
console.log(`Send: ${json}`);
76-
client.write(json);
7779
});
78-
79-
return Promise.resolve(outputBuffer);
8080
}
8181
}
8282
}
@@ -118,7 +118,9 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
118118
vscode.notebook.onDidOpenNotebookDocument(document => {
119119
const docKey = document.uri.toString();
120120
if (!this.notebooks.has(docKey)) {
121-
this.notebooks.set(docKey, new RNotebook(this.kernelScript, document));
121+
const notebook = new RNotebook(this.kernelScript, document);
122+
notebook.restartKernel();
123+
this.notebooks.set(docKey, notebook);
122124
}
123125
}),
124126
vscode.notebook.onDidCloseNotebookDocument(document => {

0 commit comments

Comments
 (0)