question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

javascript/typescript typedefs lost when webworker times out

See original GitHub issue

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”.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
RoboPhredcommented, Jun 7, 2017

Workaround:

Convert all non-editing models to typescriptDefaults.addExtraLib() entries, but still use file URIs for the “fileName” field. This bypasses the model / mirror model synchronization altogether and injects them straight into monaco-typescript’s ts service. Thankfully, monaco-typescript will pass the fileName along to typescript itself, so the commonjs module resolution remains intact.

I did some digging, and I suspect the issue lies with EditorModelManager, specifically the third constructor argument “keepIdleModels”. This controls the presence of a timer that makes it ‘forget’ about a model that has not been touched in a few minutes. However, there is no way to affect its behavior, as EditorWorkerClient._getOrCreateModelManager always passes false for this value.

1reaction
ulrichbcommented, May 22, 2019

@skymakerolof

(As a hackaround) I’m overwriting/decorating EditorWorkerClient.prototype._getOrCreateModelManager, calling the original function and then modifying the returned EditorModelManager instance (set a dummy _checkStopModelSync function).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object lost its type and functions when passed by a web worker
I am using create-react-app and the lib d3-delaunay (https://github.com/d3/d3-delaunay) to generate a voronoi diagram. It is generated in a web ...
Read more >
Web workers, React, and TypeScript - LogRocket Blog
In this article, we will learn about web workers and how to use them in a React and TypeScript project by building a...
Read more >
Advanced Concepts - NativeScript Docs
Create a new TypeScript file in the root of your project folder - name it application.android.ts or application.android.js if you are using plain...
Read more >
Pyodide - Read the Docs
Pyodide is a port of CPython to WebAssembly/Emscripten. Pyodide makes it possible to install and run Python packages in the browser with ...
Read more >
WebGPU - W3C
Note: An "agent" refers to a JavaScript "thread" (i.e. main thread, or Web Worker). 3.1.4. Object Descriptors. An object descriptor holds the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found