Skip to content

Commit afec0fe

Browse files
committed
Update notebook2
1 parent daea7f4 commit afec0fe

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

R/notebook2.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ for (expr in exprs) {
100100

101101
con <- socketConnection(host = "127.0.0.1", port = env$port, open = "r+b")
102102

103+
request_id <- 0L
103104
while (TRUE) {
104105
if (socketSelect(list(con), timeout = 0)) {
105106
header <- readLines(con, 1, encoding = "UTF-8")
106-
n <- as.integer(gsub("^Content-Length: (\\d+)$", "\\1", header))
107+
n <- as.integer(gsub("^Content-Length\\: (\\d+)$", "\\1", header))
107108
content <- readChar(con, n, useBytes = TRUE)
108109
Encoding(content) <- "UTF-8"
109-
print(content)
110+
cat("request ", request_id, ": ", content, "\n", sep = "")
111+
request_id <- request_id + 1L
110112
}
111113
Sys.sleep(0.1)
112114
}

src/notebook.ts

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RKernel {
2626
private request(obj: any) {
2727
if (this.socket) {
2828
const json = JSON.stringify(obj);
29-
this.socket.write(`Content-Length: ${json.length}\n${json}\n`);
29+
this.socket.write(`Content-Length: ${json.length}\n${json}`);
3030
}
3131
}
3232

@@ -89,46 +89,50 @@ class RKernel {
8989
public async eval(cell: vscode.NotebookCell): Promise<REvalOutput> {
9090
if (this.socket) {
9191
this.request({
92-
uri: cell.uri,
92+
uri: cell.uri.toString(),
9393
time: Date.now(),
9494
expr: cell.document.getText(),
9595
});
9696
}
97-
if (this.process) {
98-
const client = net.createConnection({ host: '127.0.0.1', port: this.port }, () => {
99-
console.log(`uri: ${cell.uri}, connect`);
100-
const request = JSON.stringify({
101-
time: Date.now(),
102-
expr: cell.document.getText(),
103-
}).concat('\n');
104-
console.log(`uri: ${cell.uri}, write: ${request}`);
105-
client.write(request);
106-
});
107-
108-
client.on('end', () => {
109-
console.log(`uri: ${cell.uri}, end`);
110-
});
111-
112-
return new Promise((resolve, reject) => {
113-
client.on('data', (data) => {
114-
const response = data.toString();
115-
console.log(`uri: ${cell.uri}, data: ${response}`);
116-
client.end();
117-
const output: REvalOutput = JSON.parse(response);
118-
resolve(output);
119-
});
120-
121-
client.on('error', (err) => {
122-
console.log(`uri: ${cell.uri}, error: ${err.name}, ${err.message}`);
123-
reject({
124-
type: 'error',
125-
result: [
126-
err.message
127-
],
128-
});
129-
});
130-
});
131-
}
97+
return {
98+
type: 'text',
99+
result: 'test',
100+
};
101+
// if (this.process) {
102+
// const client = net.createConnection({ host: '127.0.0.1', port: this.port }, () => {
103+
// console.log(`uri: ${cell.uri}, connect`);
104+
// const request = JSON.stringify({
105+
// time: Date.now(),
106+
// expr: cell.document.getText(),
107+
// }).concat('\n');
108+
// console.log(`uri: ${cell.uri}, write: ${request}`);
109+
// client.write(request);
110+
// });
111+
112+
// client.on('end', () => {
113+
// console.log(`uri: ${cell.uri}, end`);
114+
// });
115+
116+
// return new Promise((resolve, reject) => {
117+
// client.on('data', (data) => {
118+
// const response = data.toString();
119+
// console.log(`uri: ${cell.uri}, data: ${response}`);
120+
// client.end();
121+
// const output: REvalOutput = JSON.parse(response);
122+
// resolve(output);
123+
// });
124+
125+
// client.on('error', (err) => {
126+
// console.log(`uri: ${cell.uri}, error: ${err.name}, ${err.message}`);
127+
// reject({
128+
// type: 'error',
129+
// result: [
130+
// err.message
131+
// ],
132+
// });
133+
// });
134+
// });
135+
// }
132136
}
133137
}
134138

0 commit comments

Comments
 (0)