Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions adev/src/app/editor/code-editor/code-mirror-editor.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {EmbeddedTutorialManager} from '../embedded-tutorial-manager.service';
import {CodeMirrorEditor, EDITOR_CONTENT_CHANGE_DELAY_MILLIES} from './code-mirror-editor.service';
import {TutorialConfig, TutorialMetadata} from '@angular/docs';
import {TYPESCRIPT_VFS_WORKER_FACTORY} from './workers/factory-provider';
import {LANGUAGES} from './constants/code-editor-languages';
import {TsVfsWorkerActions} from './workers/enums/actions';

class FakeNodeRuntimeSandbox {
async writeFile(path: string, content: string) {}
Expand Down Expand Up @@ -180,6 +182,17 @@ describe('CodeMirrorEditor', () => {
expect(service.files().find((file) => file.filename === newFile)).toBeDefined();
});

it('should not duplicate listeners when the editor is initialized again', () => {
service.disable();
service.init(document.createElement('div'));

const createVfsEnvSpy = spyOn(service as any, 'createVfsEnv');
service.files.set([{filename: 'main.ts', content: '', language: LANGUAGES['ts']}]);
service['eventManager$'].next({action: TsVfsWorkerActions.INIT_DEFAULT_FILE_SYSTEM_MAP});

expect(createVfsEnvSpy).toHaveBeenCalledTimes(1);
});

it('should keep openFiles order', () => {
service['setProjectFiles']();

Expand Down
17 changes: 15 additions & 2 deletions adev/src/app/editor/code-editor/code-mirror-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const INITIAL_STATES = {
contentChangeListenerSubscription$: undefined,
tutorialChangeListener$: undefined,
createdFile$: undefined,
librariesTypesListener$: undefined,
vfsEnvListener$: undefined,
};

@Service()
Expand Down Expand Up @@ -99,6 +101,11 @@ export class CodeMirrorEditor {

private createdFileListener$: Subscription | undefined = INITIAL_STATES.createdFile$;

private librariesTypesListener$: Subscription | undefined =
INITIAL_STATES.librariesTypesListener$;

private vfsEnvListener$: Subscription | undefined = INITIAL_STATES.vfsEnvListener$;

init(parentElement: HTMLElement): void {
if (this._editorView) return;

Expand Down Expand Up @@ -138,7 +145,7 @@ export class CodeMirrorEditor {

// Create TypeScript virtual filesystem when default files map is created
// and files are set
this.eventManager$
this.vfsEnvListener$ = this.eventManager$
.pipe(
filter(
(event) =>
Expand Down Expand Up @@ -170,6 +177,12 @@ export class CodeMirrorEditor {

this.createdFileListener$?.unsubscribe();
this.createdFileListener$ = INITIAL_STATES.createdFile$;

this.librariesTypesListener$?.unsubscribe();
this.librariesTypesListener$ = INITIAL_STATES.librariesTypesListener$;

this.vfsEnvListener$?.unsubscribe();
this.vfsEnvListener$ = INITIAL_STATES.vfsEnvListener$;
}

changeCurrentFile(fileName: string): void {
Expand Down Expand Up @@ -219,7 +232,7 @@ export class CodeMirrorEditor {
}

private saveLibrariesTypes(): void {
this.typingsLoader.typings$
this.librariesTypesListener$ = this.typingsLoader.typings$
.pipe(
filter((typings) => typings.length > 0),
takeUntilDestroyed(this.destroyRef),
Expand Down
Loading