Skip to content

Commit 5592455

Browse files
Remove global variable from workspace saga (#2865)
* Remove global variable from workspace saga * Update tests
1 parent 8d24f8b commit 5592455

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ export const createDefaultWorkspace = (workspaceLocation: WorkspaceLocation): Wo
384384
sideContent: {
385385
alerts: [],
386386
dynamicTabs: []
387-
}
387+
},
388+
lastDebuggerResult: undefined,
389+
lastNonDetResult: null
388390
});
389391

390392
const defaultFileName = 'program.js';

src/commons/sagas/WorkspaceSaga.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export default function* WorkspaceSaga(): SagaIterator {
365365
// TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
366366
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, []));
367367
context.runtime.break = false;
368-
lastDebuggerResult = undefined;
368+
yield put(actions.updateLastDebuggerResult(undefined));
369369
});
370370

371371
yield takeEvery(
@@ -595,10 +595,11 @@ export default function* WorkspaceSaga(): SagaIterator {
595595
);
596596
}
597597

598-
let lastDebuggerResult: any;
599-
let lastNonDetResult: Result;
600598
function* updateInspector(workspaceLocation: WorkspaceLocation): SagaIterator {
601599
try {
600+
const lastDebuggerResult = yield select(
601+
(state: OverallState) => state.workspaces[workspaceLocation].lastDebuggerResult
602+
);
602603
const row = lastDebuggerResult.context.runtime.nodes[0].loc.start.line - 1;
603604
// TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
604605
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, []));
@@ -1099,6 +1100,9 @@ export function* evalCode(
10991100
);
11001101

11011102
const entrypointCode = files[entrypointFilePath];
1103+
const lastNonDetResult = yield select(
1104+
(state: OverallState) => state.workspaces[workspaceLocation].lastNonDetResult
1105+
);
11021106

11031107
function call_variant(variant: Variant) {
11041108
if (variant === Variant.NON_DET) {
@@ -1155,6 +1159,10 @@ export function* evalCode(
11551159
const isLazy: boolean = context.variant === Variant.LAZY;
11561160
const isWasm: boolean = context.variant === Variant.WASM;
11571161

1162+
const lastDebuggerResult = yield select(
1163+
(state: OverallState) => state.workspaces[workspaceLocation].lastDebuggerResult
1164+
);
1165+
11581166
// Handles `console.log` statements in fullJS
11591167
const detachConsole: () => void =
11601168
context.chapter === Chapter.FULL_JS
@@ -1208,14 +1216,14 @@ export function* evalCode(
12081216

12091217
if (paused) {
12101218
yield put(actions.endDebuggerPause(workspaceLocation));
1211-
lastDebuggerResult = manualToggleDebugger(context);
1219+
yield put(actions.updateLastDebuggerResult(manualToggleDebugger(context)));
12121220
yield call(updateInspector, workspaceLocation);
12131221
yield call(showWarningMessage, 'Execution paused', 750);
12141222
return;
12151223
}
12161224

12171225
if (actionType === EVAL_EDITOR) {
1218-
lastDebuggerResult = result;
1226+
yield put(actions.updateLastDebuggerResult(result));
12191227
}
12201228

12211229
// do not highlight for stories
@@ -1257,7 +1265,7 @@ export function* evalCode(
12571265
if (result.value === 'cut') {
12581266
result.value = undefined;
12591267
}
1260-
lastNonDetResult = result;
1268+
yield put(actions.updateLastNonDetResult(result));
12611269
}
12621270

12631271
yield* dumpDisplayBuffer(workspaceLocation, isStoriesBlock, storyEnv);

src/commons/sagas/__tests__/WorkspaceSaga.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ describe('evalCode', () => {
844844
envSteps: -1
845845
};
846846
lastDebuggerResult = { status: 'error' };
847-
state = generateDefaultState(workspaceLocation);
847+
state = generateDefaultState(workspaceLocation, { lastDebuggerResult: { status: 'error' } });
848848
});
849849

850850
describe('on EVAL_EDITOR action without interruptions or pausing', () => {

src/commons/workspace/WorkspaceActions.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createAction } from '@reduxjs/toolkit';
2-
import { Context } from 'js-slang';
2+
import { Context, Result } from 'js-slang';
33
import { Chapter, Variant } from 'js-slang/dist/types';
44

55
import { SET_IS_EDITOR_READONLY } from '../../features/sourceRecorder/sourcecast/SourcecastTypes';
@@ -64,6 +64,8 @@ import {
6464
UPDATE_EDITOR_BREAKPOINTS,
6565
UPDATE_EDITOR_VALUE,
6666
UPDATE_HAS_UNSAVED_CHANGES,
67+
UPDATE_LAST_DEBUGGER_RESULT,
68+
UPDATE_LAST_NON_DET_RESULT,
6769
UPDATE_REPL_VALUE,
6870
UPDATE_STEPSTOTAL,
6971
UPDATE_SUBLANGUAGE,
@@ -492,3 +494,17 @@ export const updateBreakpointSteps = createAction(
492494
payload: { breakpointSteps, workspaceLocation }
493495
})
494496
);
497+
498+
export const updateLastDebuggerResult = createAction(
499+
UPDATE_LAST_DEBUGGER_RESULT,
500+
(lastDebuggerResult: any) => ({
501+
payload: { lastDebuggerResult }
502+
})
503+
);
504+
505+
export const updateLastNonDetResult = createAction(
506+
UPDATE_LAST_NON_DET_RESULT,
507+
(lastNonDetResult: Result) => ({
508+
payload: { lastNonDetResult }
509+
})
510+
);

src/commons/workspace/WorkspaceReducer.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ import {
8383
UPDATE_EDITOR_BREAKPOINTS,
8484
UPDATE_EDITOR_VALUE,
8585
UPDATE_HAS_UNSAVED_CHANGES,
86+
UPDATE_LAST_DEBUGGER_RESULT,
87+
UPDATE_LAST_NON_DET_RESULT,
8688
UPDATE_REPL_VALUE,
8789
UPDATE_STEPSTOTAL,
8890
UPDATE_SUBLANGUAGE,
@@ -1075,6 +1077,23 @@ const oldWorkspaceReducer: Reducer<WorkspaceManagerState, SourceActionType> = (
10751077
breakpointSteps: action.payload.breakpointSteps
10761078
}
10771079
};
1080+
case UPDATE_LAST_DEBUGGER_RESULT:
1081+
return {
1082+
...state,
1083+
[workspaceLocation]: {
1084+
...state[workspaceLocation],
1085+
lastDebuggerResult: action.payload.lastDebuggerResult
1086+
}
1087+
};
1088+
1089+
case UPDATE_LAST_NON_DET_RESULT:
1090+
return {
1091+
...state,
1092+
[workspaceLocation]: {
1093+
...state[workspaceLocation],
1094+
lastNonDetResult: action.payload.lastNonDetResult
1095+
}
1096+
};
10781097
case NOTIFY_PROGRAM_EVALUATED: {
10791098
const debuggerContext = {
10801099
...state[workspaceLocation].debuggerContext,

src/commons/workspace/WorkspaceTypes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Context } from 'js-slang';
1+
import { Context, Result } from 'js-slang';
22

33
import { SourcecastWorkspaceState } from '../../features/sourceRecorder/sourcecast/SourcecastTypes';
44
import { SourcereelWorkspaceState } from '../../features/sourceRecorder/sourcereel/SourcereelTypes';
@@ -63,6 +63,8 @@ export const UPDATE_CURRENTSTEP = 'UPDATE_CURRENTSTEP';
6363
export const UPDATE_STEPSTOTAL = 'UPDATE_STEPSTOTAL';
6464
export const UPDATE_BREAKPOINTSTEPS = 'UPDATE_BREAKPOINTSTEPS';
6565
export const CHANGE_SUBLANGUAGE = 'CHANGE_SUBLANGUAGE';
66+
export const UPDATE_LAST_DEBUGGER_RESULT = 'UPDATE_LAST_DEBUGGER_RESULT';
67+
export const UPDATE_LAST_NON_DET_RESULT = 'UPDATE_LAST_NON_DET_RESULT';
6668

6769
export type WorkspaceLocation = keyof WorkspaceManagerState;
6870
export type WorkspaceLocationsWithTools = Extract<WorkspaceLocation, 'playground' | 'sicp'>;
@@ -146,6 +148,8 @@ export type WorkspaceState = {
146148
readonly globals: Array<[string, any]>;
147149
readonly debuggerContext: DebuggerContext;
148150
readonly sideContent: SideContentState;
151+
readonly lastDebuggerResult: any;
152+
readonly lastNonDetResult: Result | null;
149153
};
150154

151155
type ReplHistory = {

0 commit comments

Comments
 (0)