@@ -4,10 +4,17 @@ import { spawn, ChildProcess } from 'child_process';
44import { dirname } from 'path' ;
55import * as fs from 'fs' ;
66
7- interface REvalOutput {
7+ interface RSessionRequest {
88 id : number ;
99 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' ;
1118 result : string ;
1219}
1320
@@ -32,10 +39,9 @@ class RKernel {
3239 }
3340 }
3441
35- private async handleResponse ( response : REvalOutput ) {
42+ private async handleResponse ( response : RSessionResponse ) {
3643 const cell = this . doc . cells . find ( ( cell ) => cell . metadata . executionOrder == response . id ) ;
3744 if ( cell ) {
38- cell . metadata . runnable = true ;
3945 cell . metadata . runState = vscode . NotebookCellRunState . Success ;
4046 cell . metadata . lastRunDuration = + new Date ( ) - cell . metadata . runStartTime ;
4147
@@ -97,13 +103,6 @@ class RKernel {
97103 this . socket = socket ;
98104 resolve ( undefined ) ;
99105
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-
107106 socket . on ( 'end' , ( ) => {
108107 console . log ( 'socket disconnected' ) ;
109108 this . socket = undefined ;
@@ -150,10 +149,21 @@ class RKernel {
150149 this . request ( {
151150 id : cell . metadata . executionOrder ,
152151 uri : cell . uri . toString ( ) ,
152+ type : 'eval' ,
153153 expr : cell . document . getText ( ) ,
154154 } ) ;
155155 }
156156 }
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+ }
157167}
158168
159169class RNotebook implements vscode . Disposable {
@@ -369,9 +379,8 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
369379 return ;
370380 }
371381
372- if ( notebook ) {
382+ if ( notebook && cell . metadata . runState !== vscode . NotebookCellRunState . Running ) {
373383 try {
374- cell . metadata . runnable = false ;
375384 cell . metadata . runState = vscode . NotebookCellRunState . Running ;
376385 const start = + new Date ( ) ;
377386 cell . metadata . runStartTime = start ;
0 commit comments