@@ -4,10 +4,17 @@ import { spawn, ChildProcess } from 'child_process';
4
4
import { dirname } from 'path' ;
5
5
import * as fs from 'fs' ;
6
6
7
- interface REvalOutput {
7
+ interface RSessionRequest {
8
8
id : number ;
9
9
uri : string ;
10
- type : 'text' | 'plot' | 'viewer' | 'browser' | 'error' | 'cancelled' ;
10
+ type : 'eval' | 'cancel' ;
11
+ args : any ;
12
+ }
13
+
14
+ interface RSessionResponse {
15
+ id : number ;
16
+ uri : string ;
17
+ type : 'text' | 'plot' | 'viewer' | 'browser' | 'error' | 'cancel' ;
11
18
result : string ;
12
19
}
13
20
@@ -32,10 +39,9 @@ class RKernel {
32
39
}
33
40
}
34
41
35
- private async handleResponse ( response : REvalOutput ) {
42
+ private async handleResponse ( response : RSessionResponse ) {
36
43
const cell = this . doc . cells . find ( ( cell ) => cell . metadata . executionOrder == response . id ) ;
37
44
if ( cell ) {
38
- cell . metadata . runnable = true ;
39
45
cell . metadata . runState = vscode . NotebookCellRunState . Success ;
40
46
cell . metadata . lastRunDuration = + new Date ( ) - cell . metadata . runStartTime ;
41
47
@@ -97,13 +103,6 @@ class RKernel {
97
103
this . socket = socket ;
98
104
resolve ( undefined ) ;
99
105
100
- socket . on ( 'data' , ( chunk : Buffer ) => {
101
- const str = chunk . toString ( ) ;
102
- console . log ( `socket (${ socket . localAddress } :${ socket . localPort } ): ${ str } ` ) ;
103
- const response : REvalOutput = JSON . parse ( str ) ;
104
- this . handleResponse ( response ) ;
105
- } ) ;
106
-
107
106
socket . on ( 'end' , ( ) => {
108
107
console . log ( 'socket disconnected' ) ;
109
108
this . socket = undefined ;
@@ -150,10 +149,21 @@ class RKernel {
150
149
this . request ( {
151
150
id : cell . metadata . executionOrder ,
152
151
uri : cell . uri . toString ( ) ,
152
+ type : 'eval' ,
153
153
expr : cell . document . getText ( ) ,
154
154
} ) ;
155
155
}
156
156
}
157
+
158
+ public async cancel ( cell : vscode . NotebookCell ) : Promise < void > {
159
+ if ( this . socket && cell . metadata . runState === vscode . NotebookCellRunState . Running ) {
160
+ this . request ( {
161
+ id : cell . metadata . executionOrder ,
162
+ uri : cell . uri . toString ( ) ,
163
+ type : 'cancel' ,
164
+ } )
165
+ }
166
+ }
157
167
}
158
168
159
169
class RNotebook implements vscode . Disposable {
@@ -369,9 +379,8 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
369
379
return ;
370
380
}
371
381
372
- if ( notebook ) {
382
+ if ( notebook && cell . metadata . runState !== vscode . NotebookCellRunState . Running ) {
373
383
try {
374
- cell . metadata . runnable = false ;
375
384
cell . metadata . runState = vscode . NotebookCellRunState . Running ;
376
385
const start = + new Date ( ) ;
377
386
cell . metadata . runStartTime = start ;
0 commit comments