@@ -29,6 +29,7 @@ import {
2929 highlightLine ,
3030 highlightLineForControl
3131} from '../../utils/JsSlangHelper' ;
32+ import { getJsSlangContext } from '../../utils/JsSlangContextStore' ;
3233import {
3334 showSuccessMessage ,
3435 showWarningMessage
@@ -139,10 +140,15 @@ const WorkspaceSaga = combineSagaHandlers({
139140 const {
140141 activeEditorTabIndex,
141142 editorTabs,
142- context ,
143+ contextId ,
143144 externalLibrary : extLib ,
144145 programPrependValue : prepend
145146 } = yield * selectWorkspace ( workspaceLocation ) ;
147+
148+ const context = getJsSlangContext ( contextId ) ;
149+ if ( ! context ) {
150+ throw new Error ( `Context not found for workspace ${ workspaceLocation } ` ) ;
151+ }
146152
147153 const editorValue = editorTabs [ activeEditorTabIndex ?? 0 ] . value ;
148154
@@ -211,9 +217,13 @@ const WorkspaceSaga = combineSagaHandlers({
211217 yield put ( actions . beginInterruptExecution ( workspaceLocation ) ) ;
212218 yield put ( actions . clearReplInput ( workspaceLocation ) ) ;
213219 yield put ( actions . sendReplInputToOutput ( code , workspaceLocation ) ) ;
214- const context : Context = yield select (
215- ( state : OverallState ) => state . workspaces [ workspaceLocation ] . context
220+ const contextId : string = yield select (
221+ ( state : OverallState ) => state . workspaces [ workspaceLocation ] . contextId
216222 ) ;
223+ const context = getJsSlangContext ( contextId ) ;
224+ if ( ! context ) {
225+ throw new Error ( `Context not found for workspace ${ workspaceLocation } ` ) ;
226+ }
217227 // Reset old context.errors
218228 context . errors = [ ] ;
219229 const codeFilePath = '/code.js' ;
@@ -242,9 +252,13 @@ const WorkspaceSaga = combineSagaHandlers({
242252 yield put ( actions . beginInterruptExecution ( workspaceLocation ) ) ;
243253 /** Clear the context, with the same chapter and externalSymbols as before. */
244254 yield put ( actions . clearReplOutput ( workspaceLocation ) ) ;
245- const context : Context = yield select (
246- ( state : OverallState ) => state . workspaces [ workspaceLocation ] . context
255+ const contextId : string = yield select (
256+ ( state : OverallState ) => state . workspaces [ workspaceLocation ] . contextId
247257 ) ;
258+ const context = getJsSlangContext ( contextId ) ;
259+ if ( ! context ) {
260+ throw new Error ( `Context not found for workspace ${ workspaceLocation } ` ) ;
261+ }
248262 // TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
249263 yield put ( actions . setEditorHighlightedLines ( workspaceLocation , 0 , [ ] ) ) ;
250264 const codeFilePath = '/code.js' ;
@@ -263,9 +277,13 @@ const WorkspaceSaga = combineSagaHandlers({
263277 } ,
264278 [ InterpreterActions . debuggerReset . type ] : function * ( action ) {
265279 const workspaceLocation = action . payload . workspaceLocation ;
266- const context : Context = yield select (
267- ( state : OverallState ) => state . workspaces [ workspaceLocation ] . context
280+ const contextId : string = yield select (
281+ ( state : OverallState ) => state . workspaces [ workspaceLocation ] . contextId
268282 ) ;
283+ const context = getJsSlangContext ( contextId ) ;
284+ if ( ! context ) {
285+ throw new Error ( `Context not found for workspace ${ workspaceLocation } ` ) ;
286+ }
269287 yield put ( actions . clearReplOutput ( workspaceLocation ) ) ;
270288 // TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
271289 yield put ( actions . setEditorHighlightedLines ( workspaceLocation , 0 , [ ] ) ) ;
@@ -318,19 +336,20 @@ const WorkspaceSaga = combineSagaHandlers({
318336 } ,
319337 [ WorkspaceActions . chapterSelect . type ] : function * ( action ) {
320338 const { workspaceLocation, chapter : newChapter , variant : newVariant } = action . payload ;
321- const [ oldVariant , oldChapter , symbols , globals , externalLibraryName ] : [
322- Variant ,
323- Chapter ,
324- string [ ] ,
339+ const [ contextId , globals , externalLibraryName ] : [
340+ string ,
325341 Array < [ string , any ] > ,
326342 ExternalLibraryName
327343 ] = yield select ( ( state : OverallState ) => [
328- state . workspaces [ workspaceLocation ] . context . variant ,
329- state . workspaces [ workspaceLocation ] . context . chapter ,
330- state . workspaces [ workspaceLocation ] . context . externalSymbols ,
344+ state . workspaces [ workspaceLocation ] . contextId ,
331345 state . workspaces [ workspaceLocation ] . globals ,
332346 state . workspaces [ workspaceLocation ] . externalLibrary
333347 ] ) ;
348+
349+ const context = getJsSlangContext ( contextId ) ;
350+ const oldVariant = context ?. variant || Variant . DEFAULT ;
351+ const oldChapter = context ?. chapter || Chapter . SOURCE_1 ;
352+ const oldExternalSymbols = context ?. externalSymbols || [ ] ;
334353
335354 const chapterChanged : boolean = newChapter !== oldChapter || newVariant !== oldVariant ;
336355 const toChangeChapter : boolean =
@@ -346,7 +365,7 @@ const WorkspaceSaga = combineSagaHandlers({
346365 variant : newVariant ,
347366 external : {
348367 name : externalLibraryName ,
349- symbols
368+ symbols : oldExternalSymbols
350369 } ,
351370 globals
352371 } ;
@@ -374,15 +393,18 @@ const WorkspaceSaga = combineSagaHandlers({
374393 */
375394 [ WorkspaceActions . externalLibrarySelect . type ] : function * ( action ) {
376395 const { workspaceLocation, externalLibraryName : newExternalLibraryName } = action . payload ;
377- const [ chapter , globals , oldExternalLibraryName ] : [
378- Chapter ,
396+ const [ contextId , globals , oldExternalLibraryName ] : [
397+ string ,
379398 Array < [ string , any ] > ,
380399 ExternalLibraryName
381400 ] = yield select ( ( state : OverallState ) => [
382- state . workspaces [ workspaceLocation ] . context . chapter ,
401+ state . workspaces [ workspaceLocation ] . contextId ,
383402 state . workspaces [ workspaceLocation ] . globals ,
384403 state . workspaces [ workspaceLocation ] . externalLibrary
385404 ] ) ;
405+
406+ const context = getJsSlangContext ( contextId ) ;
407+ const chapter = context ?. chapter || Chapter . SOURCE_1 ;
386408 const symbols = externalLibraries . get ( newExternalLibraryName ) ! ;
387409 const library : Library = {
388410 chapter,
@@ -434,9 +456,13 @@ const WorkspaceSaga = combineSagaHandlers({
434456 // TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
435457 ( state : OverallState ) => state . workspaces [ workspaceLocation ] . editorTabs [ 0 ] . value
436458 ) ;
437- const context : Context = yield select (
438- ( state : OverallState ) => state . workspaces [ workspaceLocation ] . context
459+ const contextId : string = yield select (
460+ ( state : OverallState ) => state . workspaces [ workspaceLocation ] . contextId
439461 ) ;
462+ const context = getJsSlangContext ( contextId ) ;
463+ if ( ! context ) {
464+ throw new Error ( `Context not found for workspace ${ workspaceLocation } ` ) ;
465+ }
440466
441467 const result = findDeclaration ( code , context , {
442468 line : action . payload . cursorPosition . row + 1 ,
0 commit comments