constructor()

in src/components/operations/operation-details/react/runtime/operation-console/graphql-utilities/languageFeatures.ts [33:98]


    constructor(private worker: WorkerAccessor, private defaults: LanguageServiceApi) {
        const monacoEditorInstance = (<any>window).monaco.editor;

        const onModelAdd = (model: editor.IModel): void => {
            const modeId = model.getModeId();
            if (modeId !== this.defaults.languageId) {
                return;
            }

            let handle: number;
            this.listener[model.uri.toString()] = model.onDidChangeContent(() => {
                clearTimeout(handle);
                // @ts-ignore
                handle = setTimeout(() => this.doValidate(model, modeId), 200);
            });

            this.doValidate(model, modeId);
        };

        const onModelRemoved = (model: editor.IModel): void => {
            monacoEditorInstance.setModelMarkers(model, this.defaults.languageId, []);
            const uriStr = model.uri.toString();
            const listener = this.listener[uriStr];

            if (listener) {
                listener.dispose();
                delete this.listener[uriStr];
            }
        };

        this.disposables.push(monacoEditorInstance.onDidCreateModel(onModelAdd));

        this.disposables.push(
            monacoEditorInstance.onWillDisposeModel(model => {
                onModelRemoved(model);
            }),
        );

        this.disposables.push(
            monacoEditorInstance.onDidChangeModelLanguage(event => {
                onModelRemoved(event.model);
                onModelAdd(event.model);
            }),
        );

        this.disposables.push(
            defaults.onDidChange((_: any) => {
                monacoEditorInstance.getModels().forEach(model => {
                    if (model.getModeId() === this.defaults.languageId) {
                        onModelRemoved(model);
                        onModelAdd(model);
                    }
                });
            }),
        );

        this.disposables.push({
            dispose: () => {
                for (const key in this.listener) {
                    this.listener[key].dispose();
                }
            }
        });

        monacoEditorInstance.getModels().forEach(onModelAdd);
    }