MCPcopy
hub / github.com/microsoft/monaco-editor / WorkerManager

Class WorkerManager

src/language/css/workerManager.ts:13–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min
12
13export class WorkerManager {
14 private _defaults: LanguageServiceDefaults;
15 private _idleCheckInterval: number;
16 private _lastUsedTime: number;
17 private _configChangeListener: IDisposable;
18
19 private _worker: editor.MonacoWebWorker<CSSWorker> | null;
20 private _client: Promise<CSSWorker> | null;
21
22 constructor(defaults: LanguageServiceDefaults) {
23 this._defaults = defaults;
24 this._worker = null;
25 this._client = null;
26 this._idleCheckInterval = window.setInterval(() => this._checkIfIdle(), 30 * 1000);
27 this._lastUsedTime = 0;
28 this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker());
29 }
30
31 private _stopWorker(): void {
32 if (this._worker) {
33 this._worker.dispose();
34 this._worker = null;
35 }
36 this._client = null;
37 }
38
39 dispose(): void {
40 clearInterval(this._idleCheckInterval);
41 this._configChangeListener.dispose();
42 this._stopWorker();
43 }
44
45 private _checkIfIdle(): void {
46 if (!this._worker) {
47 return;
48 }
49 let timePassedSinceLastUsed = Date.now() - this._lastUsedTime;
50 if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) {
51 this._stopWorker();
52 }
53 }
54
55 private _getClient(): Promise<CSSWorker> {
56 this._lastUsedTime = Date.now();
57
58 if (!this._client) {
59 this._worker = createWebWorker<CSSWorker>({
60 // module that exports the create() method and returns a `CSSWorker` instance
61 moduleId: 'vs/language/css/cssWorker',
62 createWorker: () => new Worker(new URL('./css.worker', import.meta.url), { type: 'module' }),
63 label: this._defaults.languageId,
64
65 // passed in to the create() method
66 createData: {
67 options: this._defaults.options,
68 languageId: this._defaults.languageId
69 }
70 });

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…