| 13 | const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min |
| 14 | |
| 15 | export class WorkerManager { |
| 16 | private _defaults: MonacoGraphQLAPI; |
| 17 | private _idleCheckInterval: number; |
| 18 | private _lastUsedTime = 0; |
| 19 | private _configChangeListener: IDisposable; |
| 20 | private _worker: editor.MonacoWebWorker<GraphQLWorker> | null = null; |
| 21 | private _client: Promise<GraphQLWorker> | null = null; |
| 22 | |
| 23 | constructor(defaults: MonacoGraphQLAPI) { |
| 24 | this._defaults = defaults; |
| 25 | this._idleCheckInterval = window.setInterval( |
| 26 | () => this._checkIfIdle(), |
| 27 | 30 * 1000, |
| 28 | ); |
| 29 | // this is where we re-start the worker on config changes |
| 30 | this._configChangeListener = this._defaults.onDidChange(() => { |
| 31 | this._stopWorker(); |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | private _stopWorker(): void { |
| 36 | if (this._worker) { |
| 37 | this._worker.dispose(); |
| 38 | this._worker = null; |
| 39 | } |
| 40 | this._client = null; |
| 41 | } |
| 42 | |
| 43 | dispose(): void { |
| 44 | clearInterval(this._idleCheckInterval); |
| 45 | this._configChangeListener.dispose(); |
| 46 | this._stopWorker(); |
| 47 | } |
| 48 | |
| 49 | private _checkIfIdle(): void { |
| 50 | if (!this._worker) { |
| 51 | return; |
| 52 | } |
| 53 | const timePassedSinceLastUsed = Date.now() - this._lastUsedTime; |
| 54 | if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) { |
| 55 | this._stopWorker(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private async _getClient(): Promise<GraphQLWorker> { |
| 60 | this._lastUsedTime = Date.now(); |
| 61 | if (!this._client && !this._worker) { |
| 62 | try { |
| 63 | this._worker = editor.createWebWorker<GraphQLWorker>({ |
| 64 | // module that exports the create() method and returns a `GraphQLWorker` instance |
| 65 | moduleId: 'monaco-graphql/esm/GraphQLWorker.js', |
| 66 | |
| 67 | label: this._defaults.languageId, |
| 68 | // passed in to the create() method |
| 69 | createData: { |
| 70 | languageId: this._defaults.languageId, |
| 71 | formattingOptions: this._defaults.formattingOptions, |
| 72 | // only string based config can be passed from the main process |
nothing calls this directly
no outgoing calls
no test coverage detected