monaco-sql-languages
Version:
SQL languages for the Monaco Editor, based on monaco-languages.
59 lines (58 loc) • 2.1 kB
JavaScript
import { editor } from './fillers/monaco-editor-core';
const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min
export class WorkerManager {
constructor(defaults) {
this._defaults = defaults;
this._worker = null;
this._client = null;
this._idleCheckInterval = window.setInterval(() => this._checkIfIdle(), 30 * 1000);
this._lastUsedTime = 0;
this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker());
}
_stopWorker() {
if (this._worker) {
this._worker.dispose();
this._worker = null;
}
this._client = null;
}
dispose() {
clearInterval(this._idleCheckInterval);
this._configChangeListener.dispose();
this._stopWorker();
}
_checkIfIdle() {
if (!this._worker) {
return;
}
let timePassedSinceLastUsed = Date.now() - this._lastUsedTime;
if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) {
this._stopWorker();
}
}
_getClient() {
this._lastUsedTime = Date.now();
if (!this._client) {
this._worker = editor.createWebWorker({
// module that exports the create() method and returns a `CSSWorker` instance
moduleId: this._defaults.languageId,
label: this._defaults.languageId,
// passed in to the create() method
createData: {
languageId: this._defaults.languageId
}
});
this._client = this._worker.getProxy();
}
return this._client;
}
getLanguageServiceWorker(..._resources) {
const resources = _resources === null || _resources === void 0 ? void 0 : _resources.filter(Boolean);
return this._getClient().then((client) => {
if (resources && (resources === null || resources === void 0 ? void 0 : resources.length) && this._worker) {
this._worker.withSyncedResources(resources);
}
return client;
});
}
}