Skip to content

Commit a953608

Browse files
committed
Minor refactoring
1 parent 0854c6e commit a953608

File tree

11 files changed

+191
-231
lines changed

11 files changed

+191
-231
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Chapter, Language, type SourceError, Variant } from 'js-slang/dist/types';
1+
import { Chapter, Language, type SourceError, type Value, Variant } from 'js-slang/dist/types';
22

33
import type { AchievementState } from '../../features/achievement/AchievementTypes';
44
import type { DashboardState } from '../../features/dashboard/DashboardTypes';
@@ -74,7 +74,7 @@ export type CodeOutput = {
7474
*/
7575
export type ResultOutput = {
7676
type: 'result';
77-
value: any;
77+
value: Value;
7878
consoleLogs: string[];
7979
runtime?: number;
8080
isProgram?: boolean;

src/commons/sagas/PlaygroundSaga.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FSModule } from 'browserfs/dist/node/core/FS';
2-
import { Chapter, Variant } from 'js-slang/dist/types';
2+
import { Chapter } from 'js-slang/dist/types';
33
import { compressToEncodedURIComponent } from 'lz-string';
44
import qs from 'query-string';
55
import { SagaIterator } from 'redux-saga';
@@ -8,16 +8,19 @@ import CseMachine from 'src/features/cseMachine/CseMachine';
88
import { CseMachine as JavaCseMachine } from 'src/features/cseMachine/java/CseMachine';
99

1010
import PlaygroundActions from '../../features/playground/PlaygroundActions';
11-
import { isSchemeLanguage, isSourceLanguage, OverallState } from '../application/ApplicationTypes';
12-
import { ExternalLibraryName } from '../application/types/ExternalTypes';
11+
import {
12+
isSchemeLanguage,
13+
isSourceLanguage,
14+
type OverallState
15+
} from '../application/ApplicationTypes';
1316
import { retrieveFilesInWorkspaceAsRecord } from '../fileSystem/utils';
1417
import { visitSideContent } from '../sideContent/SideContentActions';
1518
import { SideContentType } from '../sideContent/SideContentTypes';
1619
import Constants from '../utils/Constants';
1720
import { showSuccessMessage, showWarningMessage } from '../utils/notifications/NotificationsHelper';
1821
import WorkspaceActions from '../workspace/WorkspaceActions';
19-
import { EditorTabState, PlaygroundWorkspaceState } from '../workspace/WorkspaceTypes';
20-
import { safeTakeEvery as takeEvery } from './SafeEffects';
22+
import type { PlaygroundWorkspaceState } from '../workspace/WorkspaceTypes';
23+
import { safeTakeEvery as takeEvery, selectWorkspace } from './SafeEffects';
2124

2225
export default function* PlaygroundSaga(): SagaIterator {
2326
yield takeEvery(PlaygroundActions.generateLzString.type, updateQueryString);
@@ -127,9 +130,6 @@ export default function* PlaygroundSaga(): SagaIterator {
127130
}
128131

129132
function* updateQueryString() {
130-
const isFolderModeEnabled: boolean = yield select(
131-
(state: OverallState) => state.workspaces.playground.isFolderModeEnabled
132-
);
133133
const fileSystem: FSModule = yield select(
134134
(state: OverallState) => state.fileSystem.inBrowserFileSystem
135135
);
@@ -138,27 +138,20 @@ function* updateQueryString() {
138138
'playground',
139139
fileSystem
140140
);
141-
const editorTabs: EditorTabState[] = yield select(
142-
(state: OverallState) => state.workspaces.playground.editorTabs
143-
);
141+
142+
const {
143+
activeEditorTabIndex,
144+
context: { chapter, variant },
145+
editorTabs,
146+
execTime,
147+
externalLibrary: external,
148+
isFolderModeEnabled
149+
} = yield* selectWorkspace('playground');
150+
144151
const editorTabFilePaths = editorTabs
145-
.map((editorTab: EditorTabState) => editorTab.filePath)
152+
.map(editorTab => editorTab.filePath)
146153
.filter((filePath): filePath is string => filePath !== undefined);
147-
const activeEditorTabIndex: number | null = yield select(
148-
(state: OverallState) => state.workspaces.playground.activeEditorTabIndex
149-
);
150-
const chapter: Chapter = yield select(
151-
(state: OverallState) => state.workspaces.playground.context.chapter
152-
);
153-
const variant: Variant = yield select(
154-
(state: OverallState) => state.workspaces.playground.context.variant
155-
);
156-
const external: ExternalLibraryName = yield select(
157-
(state: OverallState) => state.workspaces.playground.externalLibrary
158-
);
159-
const execTime: number = yield select(
160-
(state: OverallState) => state.workspaces.playground.execTime
161-
);
154+
162155
const newQueryString = qs.stringify({
163156
isFolder: isFolderModeEnabled,
164157
files: compressToEncodedURIComponent(qs.stringify(files)),

src/commons/sagas/SafeEffects.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ export function safeTakeLeading<P extends ActionPattern, Fn extends (...args: an
102102
}
103103

104104
export function* selectWorkspace<T extends WorkspaceLocation>(workspaceLocation: T) {
105-
const workspace: WorkspaceManagerState[T] = yield select((state: OverallState) => state.workspaces[workspaceLocation])
106-
return workspace
105+
const workspace: WorkspaceManagerState[T] = yield select(
106+
(state: OverallState) => state.workspaces[workspaceLocation]
107+
);
108+
return workspace;
107109
}
108110

109111
export function* selectStoryEnv(storyEnv: string) {
110-
const workspace: StoriesEnvState = yield select((state: OverallState) => state.stories.envs[storyEnv])
111-
return workspace
112+
const workspace: StoriesEnvState = yield select(
113+
(state: OverallState) => state.stories.envs[storyEnv]
114+
);
115+
return workspace;
112116
}

src/commons/sagas/StoriesSaga.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
putStoriesUserRole,
1313
updateStory
1414
} from 'src/features/stories/storiesComponents/BackendAccess';
15-
import { StoryData, StoryListView, StoryView } from 'src/features/stories/StoriesTypes';
15+
import type { StoryData, StoryListView, StoryView } from 'src/features/stories/StoriesTypes';
1616

1717
import SessionActions from '../application/actions/SessionActions';
18-
import { OverallState, StoriesRole } from '../application/ApplicationTypes';
18+
import { type OverallState, StoriesRole } from '../application/ApplicationTypes';
1919
import { Tokens } from '../application/types/SessionTypes';
2020
import { combineSagaHandlers } from '../redux/utils';
2121
import { resetSideContent } from '../sideContent/SideContentActions';
@@ -143,8 +143,8 @@ const StoriesSaga = combineSagaHandlers(sagaActions, {
143143
codeFilePath,
144144
context,
145145
execTime,
146-
'stories',
147146
action.type,
147+
'stories',
148148
env
149149
);
150150
},

src/commons/sagas/WorkspaceSaga/helpers/blockExtraMethods.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Context } from 'js-slang';
1+
import type { Context } from 'js-slang';
22
import { call } from 'redux-saga/effects';
33

44
import {
55
getBlockExtraMethodsString,
66
getDifferenceInMethods,
77
getStoreExtraMethodsString
88
} from '../../../utils/JsSlangHelper';
9-
import { EVAL_SILENT, WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
9+
import { EVAL_SILENT, type WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
1010
import { evalCodeSaga } from './evalCode';
1111

1212
export function* blockExtraMethods(
@@ -30,8 +30,8 @@ export function* blockExtraMethods(
3030
storeValuesFilePath,
3131
elevatedContext,
3232
execTime,
33-
workspaceLocation,
34-
EVAL_SILENT
33+
EVAL_SILENT,
34+
workspaceLocation
3535
);
3636
}
3737

@@ -46,7 +46,7 @@ export function* blockExtraMethods(
4646
nullifierFilePath,
4747
elevatedContext,
4848
execTime,
49-
workspaceLocation,
50-
EVAL_SILENT
49+
EVAL_SILENT,
50+
workspaceLocation
5151
);
5252
}

src/commons/sagas/WorkspaceSaga/helpers/clearContext.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import type { Context } from 'js-slang';
22
import { defineSymbol } from 'js-slang/dist/createContext';
3-
import type { Variant } from 'js-slang/dist/types';
43
import { put, select, take } from 'redux-saga/effects';
54
import WorkspaceActions from 'src/commons/workspace/WorkspaceActions';
65

76
import type { OverallState } from '../../../application/ApplicationTypes';
8-
import { ExternalLibraryName } from '../../../application/types/ExternalTypes';
97
import { actions } from '../../../utils/ActionsHelper';
108
import type { WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
9+
import { selectWorkspace } from '../../SafeEffects';
1110

1211
export function* clearContext(workspaceLocation: WorkspaceLocation, entrypointCode: string) {
13-
const [chapter, symbols, externalLibraryName, globals, variant]: [
14-
number,
15-
string[],
16-
ExternalLibraryName,
17-
Array<[string, any]>,
18-
Variant
19-
] = yield select((state: OverallState) => [
20-
state.workspaces[workspaceLocation].context.chapter,
21-
state.workspaces[workspaceLocation].context.externalSymbols,
22-
state.workspaces[workspaceLocation].externalLibrary,
23-
state.workspaces[workspaceLocation].globals,
24-
state.workspaces[workspaceLocation].context.variant
25-
]);
12+
const {
13+
context: { chapter, externalSymbols: symbols, variant },
14+
externalLibrary: externalLibraryName,
15+
globals
16+
} = yield* selectWorkspace(workspaceLocation);
2617

2718
const library = {
2819
chapter,

0 commit comments

Comments
 (0)