11import * as vscode from 'vscode' ;
22import net = require( 'net' ) ;
33import { spawn , ChildProcess } from 'child_process' ;
4- import { dirname } from 'path' ;
4+ import { dirname , resolve } from 'path' ;
55import getPort = require( 'get-port' ) ;
66
77class RKernel {
@@ -24,14 +24,18 @@ class RKernel {
2424 env . LANG = 'en_US.UTF-8' ;
2525
2626 this . port = await getPort ( ) ;
27- const childProcess = spawn ( 'R' , [ '--quite ' , '--slave' , '-f' , this . kernelScript , '--args' , `port=${ this . port } ` ] ,
27+ const childProcess = spawn ( 'R' , [ '--quiet ' , '--slave' , '-f' , this . kernelScript , '--args' , `port=${ this . port } ` ] ,
2828 { cwd : this . cwd , env : env } ) ;
2929 childProcess . stderr . on ( 'data' , ( chunk : Buffer ) => {
3030 const str = chunk . toString ( ) ;
31- console . log ( `R process (${ childProcess . pid } ): ${ str } ` ) ;
31+ console . log ( `R stderr (${ childProcess . pid } ): ${ str } ` ) ;
32+ } ) ;
33+ childProcess . stdout . on ( 'data' , ( chunk : Buffer ) => {
34+ const str = chunk . toString ( ) ;
35+ console . log ( `R stdout (${ childProcess . pid } ): ${ str } ` ) ;
3236 } ) ;
3337 childProcess . on ( 'exit' , ( code , signal ) => {
34- console . log ( `R process exited with code ${ code } ` ) ;
38+ console . log ( `R exited with code ${ code } ` ) ;
3539 } ) ;
3640 this . process = childProcess ;
3741 }
@@ -50,33 +54,29 @@ class RKernel {
5054
5155 public async eval ( cell : vscode . NotebookCell ) : Promise < string > {
5256 if ( this . process ) {
53- let outputBuffer = '' ;
54- await new Promise ( res => setTimeout ( res , 1000 ) ) ;
55-
5657 const client = net . createConnection ( { port : this . port } , ( ) => {
5758 console . log ( 'connected to server!' ) ;
59+ const json = `{"time":"${ Date . now ( ) . toString ( ) } ","expr":"rnorm(3)"}\n` ;
60+ console . log ( `Send: ${ json } ` ) ;
61+ client . write ( json ) ;
62+ } ) ;
63+
64+ client . on ( 'end' , ( ) => {
65+ console . log ( 'disconnected from server' ) ;
66+ } ) ;
67+
68+ return new Promise ( ( resolve , reject ) => {
5869 client . on ( 'data' , ( data ) => {
5970 const result = data . toString ( ) ;
6071 console . log ( result ) ;
61- outputBuffer += result ;
6272 client . end ( ) ;
73+ resolve ( result ) ;
6374 } ) ;
6475
65- client . on ( 'end' , ( ) => {
66- console . log ( 'disconnected from server' ) ;
67- } ) ;
68-
69- const json = JSON . stringify ( {
70- time : Date . now ( ) ,
71- uri : cell . uri . toString ( ) ,
72- expr : '1+1' ,
76+ client . on ( 'error' , ( err ) => {
77+ reject ( err . message ) ;
7378 } ) ;
74-
75- console . log ( `Send: ${ json } ` ) ;
76- client . write ( json ) ;
7779 } ) ;
78-
79- return Promise . resolve ( outputBuffer ) ;
8080 }
8181 }
8282}
@@ -118,7 +118,9 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
118118 vscode . notebook . onDidOpenNotebookDocument ( document => {
119119 const docKey = document . uri . toString ( ) ;
120120 if ( ! this . notebooks . has ( docKey ) ) {
121- this . notebooks . set ( docKey , new RNotebook ( this . kernelScript , document ) ) ;
121+ const notebook = new RNotebook ( this . kernelScript , document ) ;
122+ notebook . restartKernel ( ) ;
123+ this . notebooks . set ( docKey , notebook ) ;
122124 }
123125 } ) ,
124126 vscode . notebook . onDidCloseNotebookDocument ( document => {
0 commit comments