Skip to content

Commit 3d80cb7

Browse files
authored
fix: queue run code (#1118)
1 parent 2e088d0 commit 3d80cb7

File tree

3 files changed

+67
-65
lines changed

3 files changed

+67
-65
lines changed

client/src/connection/itc/CodeRunner.ts

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,77 @@ import { ITCSession } from ".";
66
import { LogLine, getSession } from "..";
77
import { 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+
}

client/src/connection/itc/ItcLibraryAdapter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
TableRow,
1313
} from "../../components/LibraryNavigator/types";
1414
import { Column, ColumnCollection } from "../rest/api/compute";
15-
import CodeRunner from "./CodeRunner";
15+
import { runCode } from "./CodeRunner";
1616
import { Config } from "./types";
1717

1818
class ItcLibraryAdapter implements LibraryAdapter {
@@ -23,7 +23,6 @@ class ItcLibraryAdapter implements LibraryAdapter {
2323
protected endTag: string = "";
2424
protected outputFinished: boolean = false;
2525
protected config: Config;
26-
protected codeRunner = new CodeRunner();
2726

2827
public async connect(): Promise<void> {
2928
this.hasEstablishedConnection = true;
@@ -270,7 +269,7 @@ class ItcLibraryAdapter implements LibraryAdapter {
270269
endTag: string = "",
271270
): Promise<string> {
272271
try {
273-
return await this.codeRunner.runCode(code, startTag, endTag);
272+
return await runCode(code, startTag, endTag);
274273
} catch (e) {
275274
onRunError(e);
276275
commands.executeCommand("setContext", "SAS.librariesDisplayed", false);

client/test/connection/itc/Coderunner.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "chai";
22
import sinon from "sinon";
33

44
import * as connection from "../../../src/connection";
5-
import CodeRunner from "../../../src/connection/itc/CodeRunner";
5+
import { runCode } from "../../../src/connection/itc/CodeRunner";
66
import { Session } from "../../../src/connection/session";
77

88
export class MockSession extends Session {
@@ -70,12 +70,7 @@ describe("CodeRunner tests", () => {
7070
// postfixed sas code
7171
`;
7272

73-
const codeRunner = new CodeRunner();
74-
const results = await codeRunner.runCode(
75-
codeString,
76-
"<CodeTag>",
77-
"</CodeTag>",
78-
);
73+
const results = await runCode(codeString, "<CodeTag>", "</CodeTag>");
7974

8075
expect(results).to.equal("Test Code");
8176
});
@@ -87,8 +82,7 @@ describe("CodeRunner tests", () => {
8782
// postfixed sas code
8883
`;
8984

90-
const codeRunner = new CodeRunner();
91-
const results = await codeRunner.runCode(codeString);
85+
const results = await runCode(codeString);
9286

9387
expect(results).to.equal(
9488
codeString

0 commit comments

Comments
 (0)