setMaximumWorkerIdleTime(-1) doesn't disable worker timeout
monaco-editor version: 0.15.6 Browser: Chrome 72.0.3626.96, Firefox 65.0, Edge 44.17763.1.0 OS: Windows 10 Pro 1809 (17763.253)
Steps or JS usage snippet reproducing the issue:
Navigate to the following JSFiddle: https://jsfiddle.net/jcoutch/vb76yo38/
After the Monaco editor loads, wait a few minutes, then start making changes in the loaded model. The 'test-class' reference will no longer be found by Monaco, even though the model is still loaded in memory, and I'm setting the maximum worker idle time to -1 to prevent the worker from going idle.
Even though the code is only setting the worker idle time for TypeScript, I've also tried setting the idle time on monaco.languages.typescript.javascriptDefaults with the same result.
Hmm, that is odd. I'm not 100% sure why monaco-typescript would track the worker and kill it by itself, given that functionality is already implemented in the core editor.
These constants (which cannot be configured currently) control this behaviour: https://github.com/Microsoft/vscode/blob/0a4a3b98282175a82a819b70d2bbe46475e1ebd0/src/vs/editor/common/services/editorWorkerServiceImpl.ts#L24-L32
Any update on this feature?. or any workarounds other than adding to addExtraLib?
Same problem here. More simple repro (can be executed in the Monaco playground):
const editor = monaco.editor.create(document.getElementById("container"));
monaco.editor.createModel(`export const someConst = "someConst";
`, "typescript",
monaco.Uri.file("/a.ts"));
editor.setModel(monaco.editor.createModel(`import { someConst } from "./a";
console.log("hello from main", someConst.length);
`,
"typescript", monaco.Uri.file("/main.ts")));
I was running into the same issue and found a workaround that worked in my case. The workaround makes an edit to the secondary (imported) model every 50s to prevent the typescript worker from killing itself.
The did not change in my case. It might change for projects with multiple models. Happy to hear back from others (if it worked).
Here is an example for the Monaco editor playground:
monaco.languages.typescript.typescriptDefaults.addExtraLib(
`/**
* A simple species interface.
*/
export interface ISpecies {
/**
* Name of the species.
*/
name: string;
/**
* Prints the noise of the animal to the console.
*
* @param type {NoiseType} The noise type.
*/
makeNoise(type: NoiseType): void;
}
export enum NoiseType {
HAPPY,
ANGRY,
}
/**
* A function that let's two species wrestle and returns an array with the winner at index 1
* and the second winner at index 2.
*
* @param species1 {ISpecies} The first species
* @param species2 {ISpecies} The second species
*/
export declare function wrestle(species1: ISpecies, species2: ISpecies): [ISpecies, ISpecies]`,
'file:///node_modules/@types/species/index.d.ts'
);
let secondaryModel = monaco.editor.createModel([
`import { ISpecies, NoiseType } from './species';
abstract class Animal implements ISpecies {
name: string;
constructor(name: string) {
this.name = name;
}
makeNoise(type: NoiseType): void {
throw new Error('implement noise');
}
}
export class Dog extends Animal {
constructor(name: string) {
super(name);
}
makeNoise(type: NoiseType): void {
switch (type) {
case NoiseType.HAPPY:
console.log('woof');
break;
case NoiseType.ANGRY:
console.log('growl');
break;
}
}
}
export class Horse extends Animal {
constructor(name: string) {
super(name);
}
makeNoise(type: NoiseType): void {
switch (type) {
case NoiseType.HAPPY:
console.log('neigh');
break;
case NoiseType.ANGRY:
console.log('$%@Y&%#');
break;
}
}
}`
].join('\n'), "typescript", monaco.Uri.file("./animals.ts"));
let primaryModel = monaco.editor.createModel([
`import { wrestle, NoiseType } from 'species';
import { Dog, Horse } from './animals';
const dog = new Dog('Rantanplan');
const horse = new Horse('Jolly Jumper');
const [victor, vanquished] = wrestle(dog, horse);
console.log(\`The winner is \${victor.name}\`);
victor.makeNoise(NoiseType.HAPPY);
vanquished.makeNoise(NoiseType.ANGRY);
`
].join('\n'), 'typescript', monaco.Uri.file('./main.ts'));
monaco.editor.create(document.getElementById("container"), {
model: primaryModel,
language: "javascript"
});
// The worker service will kill itself after 60-90s. To prevent this
// behavior, we'll make and revert an edit every 50s. This is a workaround
// but it seems to do the trick.
setInterval(() => {
const range = new monaco.Range(1, 1, 1, 1);
const addEmptySpace = {
forceMoveMarkers: true,
range,
text: ' '
};
const invertedEdits = secondaryModel.applyEdits([addEmptySpace]);
secondaryModel.applyEdits(invertedEdits);
}, 50 * 1000);
@raedle I tried your suggestion and it seems to work for me, though it is annoying. I have a varying amount of files so I did this
setInterval(() => {
const range = new monaco.Range(1,1,1,1);
const addEmptySpace = {forceMoveMarkers: true, range, text: ' '};
for (const m of monaco.editor.getModels()) {
const toInvert = m.applyEdits([addEmptySpace]);
m.applyEdits(toInvert);
}
}, 50*1000)
It would be nice if this was something we could set in the configuration.