Skip to content

Commit e2ab910

Browse files
committed
refactor: inject an instance of typescript-vfs.worker
Modified tsVfsWorker worker initialization logic
1 parent f1c7417 commit e2ab910

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

adev/src/app/editor/code-editor/code-mirror-editor.service.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export class CodeMirrorEditor {
7171

7272
// An instance of web worker used to run virtual TypeScript environment in the browser.
7373
// It allows to enrich CodeMirror UX for TypeScript files.
74-
private tsVfsWorker: Worker | null = null;
7574
// EventManager gives ability to communicate between tsVfsWorker and CodeMirror instance
7675
private readonly eventManager$ = new Subject<ActionMessage>();
7776

@@ -81,6 +80,7 @@ export class CodeMirrorEditor {
8180
private readonly typingsLoader = inject(TypingsLoader);
8281
private readonly destroyRef = inject(DestroyRef);
8382
private readonly diagnosticsState = inject(DiagnosticsState);
83+
private readonly tsVfsWorker = inject(Worker, {optional: true});
8484

8585
private _editorView: EditorView | null = INITIAL_STATES._editorView;
8686
private readonly _editorStates = new Map<EditorFile['filename'], EditorState>();
@@ -182,14 +182,10 @@ export class CodeMirrorEditor {
182182
}
183183

184184
private initTypescriptVfsWorker(): void {
185-
if (this.tsVfsWorker) {
185+
if (!this.tsVfsWorker) {
186186
return;
187187
}
188188

189-
this.tsVfsWorker = new Worker(new URL('./workers/typescript-vfs.worker', import.meta.url), {
190-
type: 'module',
191-
});
192-
193189
this.tsVfsWorker.addEventListener('message', ({data}: MessageEvent<ActionMessage>) => {
194190
this.eventManager$.next(data);
195191
});
@@ -214,10 +210,13 @@ export class CodeMirrorEditor {
214210

215211
// Method is responsible for sending request to Typescript VFS worker.
216212
private sendRequestToTsVfs = <T>(request: ActionMessage<T>) => {
217-
if (!this.tsVfsWorker) return;
213+
if (!this.tsVfsWorker) {
214+
console.warn('TypeScript VFS worker not available');
215+
return;
216+
}
218217

219218
// Send message to tsVfsWorker only when current file is TypeScript file.
220-
if (!this.currentFile().filename.endsWith('.ts')) return;
219+
if (!this.currentFile()?.filename.endsWith('.ts')) return;
221220

222221
this.tsVfsWorker.postMessage(request);
223222
};

0 commit comments

Comments
 (0)