profile
viewpoint

Ask questionsjavascript/typescript typedefs lost when webworker times out

monaco-editor version: 0.8.3 (playground) Browser: Chrome 58.0.3029.110 (64-bit) OS: Windows 10

When typescript is set to use commonjs modules, and the additional modules are provided as other editor models, the editor will handle imports correctly up until the typescript worker times out. Once the typescript worker times out, the old models will not be taken into account by the new worker.

To reproduce: Run this in the monaco playground. Mouse over the "typed", "direct", and "impl" variables and note that they have typing info. Open the chrome debug console, and make a note that typescript has a web worker present. Move focus back to the playground code window, and go do something else for 5 minutes. After the idle time has been reached, the typescript web worker will be disposed. Note its absence in the debug console. Run the custom "Debug Monaco: get models" action, and note that the models still exist. Try to mouse over the variables again. The webworker will be re-created, but the variables now show up as the "any" type, indicating that the models are being ignored.

const useLanguage = "typescript";

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
    "target": monaco.languages.typescript.ScriptTarget.ES5,
    "moduleResolution": monaco.languages.typescript.ModuleResolutionKind.Classic,
    "module": monaco.languages.typescript.ModuleKind.CommonJS,
    "noEmit": true,
    "noImplicitAny": true,
    "noErrorTruncation": true,
    "suppressExcessPropertyErrors": false,
    "suppressImplicitAnyIndexErrors": true,
    "noLib": true,
    "allowJs": true
});

monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
    noSemanticValidation: false,
    noSyntaxValidation: false
});


function createDef(name, code) {
    const mod = monaco.editor.createModel(code, useLanguage, monaco.Uri.file("" + name));
    mod.onWillDispose(() => {
        console.log("Disposing typedef model", name);
    });
}

createDef(
    'directmod.d.ts', `
    declare module "directmod" {
        export = 42;
    }
    `
);

createDef(
    'implicitmod.js', `
    /**
     * 
     * @param {string} str - A string
     * @returns {number} The length of the string
     */
    function someFunc(str) {
        return str.length;
    }

    module.exports = {
        someFunc
    };
    `
);

createDef('typedefmod.d.ts', `
export function foo(): void;
`);


function createModel() {
    return monaco.editor.createModel(
`
const typed = require("typedefmod");
const direct = require("directmod");
const impl = require("implicitmod");

a.foo();
b.someFunc();
`,
        useLanguage,
        monaco.Uri.file("src/usage.js")
    );
}

const editor = monaco.editor.create(document.getElementById("container"), {
	model: createModel()
});

editor.addAction({
    id: "get-models",
    label: "Debug Monaco: Get Models",
    run: () => {
        alert(monaco.editor.getModels().map(x => x.uri.toString()).join("\n"))
    }
});

Background: I have multiple commonjs-module javascript libraries that the user can edit. I am trying with reasonable success to enable autocomplete and other typed features by keeping the other libraries around as models. However, after the worker times out, all such typing features are lost and the types revert to "any".

microsoft/monaco-editor

Answer questions ulrichb

@KayoMaciek

I had the same issue that my imported TypeScript models disappeared (got import errors) after 90 sec.

But in my case not even a setEagerModelSync(true) + setMaximumWorkerIdleTime(30 * 1000) helped :/

I had to completely disable the _checkStopModelSync() timeout call (happening every 30 secs) to avoid that it calls _stopModelSync() for all the unused models at the third callback (after 90 sec).

@alexandrudima: Is it okay to disable this timer? If yes, it would be cool if the Monaco editor API exposes the boolean keepIdleModels c'tor parameter

useful!

Related questions

Monarch: How do you add "@keywords" key to "language object"? (undocumented) hot 3
How to get the line count including line wraps hot 2
Error: Cannot find module 'monaco-editor/esm/vs/editor/contrib/gotoSymbol/goToCommands' hot 2
Removing the tooltip on the read-only editor that says 'cannot edit in read-only editor' hot 1
Monaco paste event hot 1
Registering/Providing custom Commands/CodeActions hot 1
Error: Cannot find module 'monaco-editor/esm/vs/editor/contrib/goToDefinition/goToDefinitionCommands' hot 1
TypeScript declaration preview from extra lib results in error hot 1
Editor props/extends the actual width of the parent that is a flexbox item hot 1
Problem with insane.js while using monaco-editor@0.18.0 hot 1
Change the language of an existing editor hot 1
Can't scroll in firefox hot 1
How to add simple custom language support? hot 1
monaco-editor polute the global variables, making incompatible with other JS libraries, such as CodeMirror, RequireJS, SeaJS, etc. hot 1
Highlight merge conflicts in monaco like in VSCode hot 1
Github User Rank List