@@ -50,58 +50,6 @@ class RKernel {
50
50
this . socket = socket ;
51
51
resolve ( undefined ) ;
52
52
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
-
105
53
socket . on ( 'end' , ( ) => {
106
54
console . log ( 'socket disconnected' ) ;
107
55
this . socket = undefined ;
@@ -136,6 +84,7 @@ class RKernel {
136
84
if ( this . process ) {
137
85
this . process . kill ( ) ;
138
86
this . process = undefined ;
87
+ this . socket = undefined ;
139
88
}
140
89
}
141
90
@@ -144,20 +93,30 @@ class RKernel {
144
93
await this . start ( ) ;
145
94
}
146
95
147
- public eval ( cell : vscode . NotebookCell ) {
96
+ public async eval ( cell : vscode . NotebookCell ) : Promise < RSessionResponse > {
148
97
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
+ } ) ;
153
112
} ) ;
154
113
}
155
114
}
156
115
157
116
public cancel ( cell : vscode . NotebookCell ) {
158
117
if ( this . socket ) {
159
118
this . request ( {
160
- id : cell . metadata . executionOrder ,
119
+ id : cell ? cell . metadata . executionOrder : 0 ,
161
120
type : 'cancel' ,
162
121
} ) ;
163
122
}
@@ -181,7 +140,7 @@ class RNotebook implements vscode.Disposable {
181
140
this . kernel . restart ( ) ;
182
141
}
183
142
184
- public async eval ( cell : vscode . NotebookCell ) : Promise < void > {
143
+ public async eval ( cell : vscode . NotebookCell ) : Promise < RSessionResponse > {
185
144
await this . kernel . start ( ) ;
186
145
return this . kernel . eval ( cell ) ;
187
146
}
@@ -385,7 +344,51 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
385
344
const start = + new Date ( ) ;
386
345
cell . metadata . runStartTime = start ;
387
346
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
+ }
389
392
} catch ( e ) {
390
393
cell . outputs = [ {
391
394
outputKind : vscode . CellOutputKind . Error ,
@@ -413,7 +416,8 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
413
416
}
414
417
415
418
async cancelAllCellsExecution ( document : vscode . NotebookDocument ) {
416
-
419
+ const notebook = this . notebooks . get ( document . uri . toString ( ) ) ;
420
+ await notebook . cancel ( undefined ) ;
417
421
}
418
422
419
423
public dispose ( ) {
0 commit comments