Skip to content

Commit e94c22e

Browse files
committed
Update eval
1 parent ba2515f commit e94c22e

File tree

2 files changed

+66
-62
lines changed

2 files changed

+66
-62
lines changed

R/notebook.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ for (expr in exprs) {
1111
r <- callr::r_session$new(
1212
callr::r_session_options(
1313
system_profile = TRUE, user_profile = TRUE, supervise = TRUE),
14-
wait = TRUE
14+
wait = TRUE, wait_timeout = 3000
1515
)
1616

1717
r$run(function() {

src/notebook.ts

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -50,58 +50,6 @@ class RKernel {
5050
this.socket = socket;
5151
resolve(undefined);
5252

53-
socket.on('data', async (data) => {
54-
const response: RSessionResponse = JSON.parse(data.toString());
55-
const cell = this.doc.cells.find(cell => cell.metadata.executionOrder === response.id);
56-
if (cell) {
57-
cell.metadata.runState = vscode.NotebookCellRunState.Success;
58-
cell.metadata.lastRunDuration = +new Date() - cell.metadata.runStartTime;
59-
60-
console.log(`uri: ${cell.uri}, id: ${response.id}, type: ${response.type}, result: ${response.result}`);
61-
switch (response.type) {
62-
case 'text':
63-
cell.outputs = [{
64-
outputKind: vscode.CellOutputKind.Text,
65-
text: response.result,
66-
}];
67-
break;
68-
case 'plot':
69-
cell.outputs = [{
70-
outputKind: vscode.CellOutputKind.Rich,
71-
data: {
72-
'image/svg+xml': (await vscode.workspace.fs.readFile(vscode.Uri.parse(response.result))).toString(),
73-
},
74-
}];
75-
break;
76-
case 'viewer':
77-
cell.outputs = [{
78-
outputKind: vscode.CellOutputKind.Rich,
79-
data: {
80-
'application/json': response.result,
81-
},
82-
}];
83-
break;
84-
case 'browser':
85-
cell.outputs = [{
86-
outputKind: vscode.CellOutputKind.Rich,
87-
data: {
88-
'application/json': response.result,
89-
},
90-
}];
91-
break;
92-
case 'error':
93-
cell.metadata.runState = vscode.NotebookCellRunState.Error;
94-
cell.outputs = [{
95-
outputKind: vscode.CellOutputKind.Error,
96-
evalue: response.result,
97-
ename: '',
98-
traceback: [],
99-
}];
100-
break;
101-
}
102-
}
103-
});
104-
10553
socket.on('end', () => {
10654
console.log('socket disconnected');
10755
this.socket = undefined;
@@ -136,6 +84,7 @@ class RKernel {
13684
if (this.process) {
13785
this.process.kill();
13886
this.process = undefined;
87+
this.socket = undefined;
13988
}
14089
}
14190

@@ -144,20 +93,30 @@ class RKernel {
14493
await this.start();
14594
}
14695

147-
public eval(cell: vscode.NotebookCell) {
96+
public async eval(cell: vscode.NotebookCell): Promise<RSessionResponse> {
14897
if (this.socket) {
149-
this.request({
150-
id: cell.metadata.executionOrder,
151-
type: 'eval',
152-
expr: cell.document.getText(),
98+
return new Promise((resolve, reject) => {
99+
const handler = async (data: Buffer) => {
100+
const response: RSessionResponse = JSON.parse(data.toString());
101+
resolve(response);
102+
this.socket.removeListener('data', handler);
103+
};
104+
105+
this.socket.on('data', handler);
106+
107+
this.request({
108+
id: cell.metadata.executionOrder,
109+
type: 'eval',
110+
expr: cell.document.getText(),
111+
});
153112
});
154113
}
155114
}
156115

157116
public cancel(cell: vscode.NotebookCell) {
158117
if (this.socket) {
159118
this.request({
160-
id: cell.metadata.executionOrder,
119+
id: cell ? cell.metadata.executionOrder : 0,
161120
type: 'cancel',
162121
});
163122
}
@@ -181,7 +140,7 @@ class RNotebook implements vscode.Disposable {
181140
this.kernel.restart();
182141
}
183142

184-
public async eval(cell: vscode.NotebookCell): Promise<void> {
143+
public async eval(cell: vscode.NotebookCell): Promise<RSessionResponse> {
185144
await this.kernel.start();
186145
return this.kernel.eval(cell);
187146
}
@@ -385,7 +344,51 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
385344
const start = +new Date();
386345
cell.metadata.runStartTime = start;
387346
cell.metadata.executionOrder = ++this.runIndex;
388-
await notebook.eval(cell);
347+
const response = await notebook.eval(cell);
348+
cell.metadata.runState = vscode.NotebookCellRunState.Success;
349+
cell.metadata.lastRunDuration = +new Date() - cell.metadata.runStartTime;
350+
console.log(`uri: ${cell.uri}, id: ${response.id}, type: ${response.type}, result: ${response.result}`);
351+
switch (response.type) {
352+
case 'text':
353+
cell.outputs = [{
354+
outputKind: vscode.CellOutputKind.Text,
355+
text: response.result,
356+
}];
357+
break;
358+
case 'plot':
359+
cell.outputs = [{
360+
outputKind: vscode.CellOutputKind.Rich,
361+
data: {
362+
'image/svg+xml': (await vscode.workspace.fs.readFile(vscode.Uri.parse(response.result))).toString(),
363+
},
364+
}];
365+
break;
366+
case 'viewer':
367+
cell.outputs = [{
368+
outputKind: vscode.CellOutputKind.Rich,
369+
data: {
370+
'application/json': response.result,
371+
},
372+
}];
373+
break;
374+
case 'browser':
375+
cell.outputs = [{
376+
outputKind: vscode.CellOutputKind.Rich,
377+
data: {
378+
'application/json': response.result,
379+
},
380+
}];
381+
break;
382+
case 'error':
383+
cell.metadata.runState = vscode.NotebookCellRunState.Error;
384+
cell.outputs = [{
385+
outputKind: vscode.CellOutputKind.Error,
386+
evalue: response.result,
387+
ename: '',
388+
traceback: [],
389+
}];
390+
break;
391+
}
389392
} catch (e) {
390393
cell.outputs = [{
391394
outputKind: vscode.CellOutputKind.Error,
@@ -413,7 +416,8 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
413416
}
414417

415418
async cancelAllCellsExecution(document: vscode.NotebookDocument) {
416-
419+
const notebook = this.notebooks.get(document.uri.toString());
420+
await notebook.cancel(undefined);
417421
}
418422

419423
public dispose() {

0 commit comments

Comments
 (0)