@@ -6,68 +6,77 @@ import { ITCSession } from ".";
66import { LogLine , getSession } from ".." ;
77import { useRunStore } from "../../store" ;
88
9- class CodeRunner {
10- public async runCode (
11- code : string ,
12- startTag : string = "" ,
13- endTag : string = "" ,
14- ) : Promise < string > {
15- // If we're already executing code, lets wait for it
16- // to finish up.
17- let unsubscribe ;
18- if ( useRunStore . getState ( ) . isExecutingCode ) {
19- await new Promise ( ( resolve ) => {
20- unsubscribe = useRunStore . subscribe (
21- ( state ) => state . isExecutingCode ,
22- ( isExecutingCode ) => ! isExecutingCode && resolve ( true ) ,
23- ) ;
24- } ) ;
25- }
9+ let wait : Promise < string > | undefined ;
2610
27- const { setIsExecutingCode } = useRunStore . getState ( ) ;
28- setIsExecutingCode ( true ) ;
29- commands . executeCommand ( "setContext" , "SAS.running" , true ) ;
30- const session = getSession ( ) ;
11+ export async function runCode (
12+ code : string ,
13+ startTag : string = "" ,
14+ endTag : string = "" ,
15+ ) : Promise < string > {
16+ const task = ( ) => _runCode ( code , startTag , endTag ) ;
3117
32- let logText = "" ;
33- const onExecutionLogFn = session . onExecutionLogFn ;
34- const outputLines = [ ] ;
18+ wait = wait ? wait . then ( task ) : task ( ) ;
19+ return wait ;
20+ }
21+
22+ async function _runCode (
23+ code : string ,
24+ startTag : string = "" ,
25+ endTag : string = "" ,
26+ ) : Promise < string > {
27+ // If we're already executing code, lets wait for it
28+ // to finish up.
29+ let unsubscribe ;
30+ if ( useRunStore . getState ( ) . isExecutingCode ) {
31+ await new Promise ( ( resolve ) => {
32+ unsubscribe = useRunStore . subscribe (
33+ ( state ) => state . isExecutingCode ,
34+ ( isExecutingCode ) => ! isExecutingCode && resolve ( true ) ,
35+ ) ;
36+ } ) ;
37+ }
3538
36- const addLine = ( logLines : LogLine [ ] ) =>
37- outputLines . push ( ...logLines . map ( ( { line } ) => line ) ) ;
39+ const { setIsExecutingCode } = useRunStore . getState ( ) ;
40+ setIsExecutingCode ( true ) ;
41+ commands . executeCommand ( "setContext" , "SAS.running" , true ) ;
42+ const session = getSession ( ) ;
3843
39- try {
40- await session . setup ( true ) ;
44+ let logText = "" ;
45+ const onExecutionLogFn = session . onExecutionLogFn ;
46+ const outputLines = [ ] ;
4147
42- // Lets capture output to use it on
43- session . onExecutionLogFn = addLine ;
48+ const addLine = ( logLines : LogLine [ ] ) =>
49+ outputLines . push ( ... logLines . map ( ( { line } ) => line ) ) ;
4450
45- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
46- await ( session as ITCSession ) . run ( code , true ) ;
51+ try {
52+ await session . setup ( true ) ;
4753
48- const logOutput = outputLines . filter ( ( line ) => line . trim ( ) ) . join ( "" ) ;
54+ // Lets capture output to use it on
55+ session . onExecutionLogFn = addLine ;
4956
50- logText =
51- startTag && endTag
52- ? logOutput
53- . slice (
54- logOutput . lastIndexOf ( startTag ) ,
55- logOutput . lastIndexOf ( endTag ) ,
56- )
57- . replace ( startTag , "" )
58- . replace ( endTag , "" )
59- : logOutput ;
60- } finally {
61- unsubscribe && unsubscribe ( ) ;
62- // Lets update our session to write to the log
63- session . onExecutionLogFn = onExecutionLogFn ;
57+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
58+ await ( session as ITCSession ) . run ( code , true ) ;
6459
65- setIsExecutingCode ( false ) ;
66- commands . executeCommand ( "setContext" , "SAS.running" , false ) ;
67- }
60+ const logOutput = outputLines . filter ( ( line ) => line . trim ( ) ) . join ( "" ) ;
6861
69- return logText ;
62+ logText =
63+ startTag && endTag
64+ ? logOutput
65+ . slice (
66+ logOutput . lastIndexOf ( startTag ) ,
67+ logOutput . lastIndexOf ( endTag ) ,
68+ )
69+ . replace ( startTag , "" )
70+ . replace ( endTag , "" )
71+ : logOutput ;
72+ } finally {
73+ unsubscribe && unsubscribe ( ) ;
74+ // Lets update our session to write to the log
75+ session . onExecutionLogFn = onExecutionLogFn ;
76+
77+ setIsExecutingCode ( false ) ;
78+ commands . executeCommand ( "setContext" , "SAS.running" , false ) ;
7079 }
71- }
7280
73- export default CodeRunner ;
81+ return logText ;
82+ }
0 commit comments