workerManager.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { editor } from './fillers/monaco-editor-core.js';
  6. var STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min
  7. var WorkerManager = /** @class */ (function () {
  8. function WorkerManager(defaults) {
  9. var _this = this;
  10. this._defaults = defaults;
  11. this._worker = null;
  12. this._idleCheckInterval = setInterval(function () { return _this._checkIfIdle(); }, 30 * 1000);
  13. this._lastUsedTime = 0;
  14. this._configChangeListener = this._defaults.onDidChange(function () { return _this._stopWorker(); });
  15. }
  16. WorkerManager.prototype._stopWorker = function () {
  17. if (this._worker) {
  18. this._worker.dispose();
  19. this._worker = null;
  20. }
  21. this._client = null;
  22. };
  23. WorkerManager.prototype.dispose = function () {
  24. clearInterval(this._idleCheckInterval);
  25. this._configChangeListener.dispose();
  26. this._stopWorker();
  27. };
  28. WorkerManager.prototype._checkIfIdle = function () {
  29. if (!this._worker) {
  30. return;
  31. }
  32. var timePassedSinceLastUsed = Date.now() - this._lastUsedTime;
  33. if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) {
  34. this._stopWorker();
  35. }
  36. };
  37. WorkerManager.prototype._getClient = function () {
  38. this._lastUsedTime = Date.now();
  39. if (!this._client) {
  40. this._worker = editor.createWebWorker({
  41. // module that exports the create() method and returns a `JSONWorker` instance
  42. moduleId: 'vs/language/json/jsonWorker',
  43. label: this._defaults.languageId,
  44. // passed in to the create() method
  45. createData: {
  46. languageSettings: this._defaults.diagnosticsOptions,
  47. languageId: this._defaults.languageId,
  48. enableSchemaRequest: this._defaults.diagnosticsOptions.enableSchemaRequest
  49. }
  50. });
  51. this._client = this._worker.getProxy();
  52. }
  53. return this._client;
  54. };
  55. WorkerManager.prototype.getLanguageServiceWorker = function () {
  56. var _this = this;
  57. var resources = [];
  58. for (var _i = 0; _i < arguments.length; _i++) {
  59. resources[_i] = arguments[_i];
  60. }
  61. var _client;
  62. return this._getClient()
  63. .then(function (client) {
  64. _client = client;
  65. })
  66. .then(function (_) {
  67. return _this._worker.withSyncedResources(resources);
  68. })
  69. .then(function (_) { return _client; });
  70. };
  71. return WorkerManager;
  72. }());
  73. export { WorkerManager };