@@ -29,6 +29,7 @@ import {TsVfsWorkerActions} from './workers/enums/actions';
2929import { CodeChangeRequest } from './workers/interfaces/code-change-request' ;
3030import { ActionMessage } from './workers/interfaces/message' ;
3131import { NodeRuntimeState } from '../node-runtime-state.service' ;
32+ import { TYPESCRIPT_VFS_WORKER_FACTORY } from './workers/factory-provider' ;
3233
3334export interface EditorFile {
3435 filename : string ;
@@ -71,7 +72,6 @@ export class CodeMirrorEditor {
7172
7273 // An instance of web worker used to run virtual TypeScript environment in the browser.
7374 // It allows to enrich CodeMirror UX for TypeScript files.
74- private tsVfsWorker : Worker | null = null ;
7575 // EventManager gives ability to communicate between tsVfsWorker and CodeMirror instance
7676 private readonly eventManager$ = new Subject < ActionMessage > ( ) ;
7777
@@ -81,6 +81,8 @@ export class CodeMirrorEditor {
8181 private readonly typingsLoader = inject ( TypingsLoader ) ;
8282 private readonly destroyRef = inject ( DestroyRef ) ;
8383 private readonly diagnosticsState = inject ( DiagnosticsState ) ;
84+ private readonly tsVfsWorkerFactory = inject ( TYPESCRIPT_VFS_WORKER_FACTORY ) ;
85+ private tsVfsWorker : Worker | null = null ;
8486
8587 private _editorView : EditorView | null = INITIAL_STATES . _editorView ;
8688 private readonly _editorStates = new Map < EditorFile [ 'filename' ] , EditorState > ( ) ;
@@ -182,14 +184,11 @@ export class CodeMirrorEditor {
182184 }
183185
184186 private initTypescriptVfsWorker ( ) : void {
185- if ( this . tsVfsWorker ) {
187+ if ( this . tsVfsWorker || ! this . tsVfsWorkerFactory ) {
186188 return ;
187189 }
188190
189- this . tsVfsWorker = new Worker ( new URL ( './workers/typescript-vfs.worker' , import . meta. url ) , {
190- type : 'module' ,
191- } ) ;
192-
191+ this . tsVfsWorker = this . tsVfsWorkerFactory ( ) ;
193192 this . tsVfsWorker . addEventListener ( 'message' , ( { data} : MessageEvent < ActionMessage > ) => {
194193 this . eventManager$ . next ( data ) ;
195194 } ) ;
@@ -214,10 +213,13 @@ export class CodeMirrorEditor {
214213
215214 // Method is responsible for sending request to Typescript VFS worker.
216215 private sendRequestToTsVfs = < T > ( request : ActionMessage < T > ) => {
217- if ( ! this . tsVfsWorker ) return ;
216+ if ( ! this . tsVfsWorker ) {
217+ console . warn ( 'TypeScript VFS worker not available' ) ;
218+ return ;
219+ }
218220
219221 // Send message to tsVfsWorker only when current file is TypeScript file.
220- if ( ! this . currentFile ( ) . filename . endsWith ( '.ts' ) ) return ;
222+ if ( ! this . currentFile ( ) ? .filename . endsWith ( '.ts' ) ) return ;
221223
222224 this . tsVfsWorker . postMessage ( request ) ;
223225 } ;
0 commit comments