workerManager.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 `HTMLWorker` instance
  42. moduleId: 'vs/language/html/htmlWorker',
  43. // passed in to the create() method
  44. createData: {
  45. languageSettings: this._defaults.options,
  46. languageId: this._defaults.languageId
  47. },
  48. label: this._defaults.languageId
  49. });
  50. this._client = this._worker.getProxy();
  51. }
  52. return this._client;
  53. };
  54. WorkerManager.prototype.getLanguageServiceWorker = function () {
  55. var _this = this;
  56. var resources = [];
  57. for (var _i = 0; _i < arguments.length; _i++) {
  58. resources[_i] = arguments[_i];
  59. }
  60. var _client;
  61. return this._getClient()
  62. .then(function (client) {
  63. _client = client;
  64. })
  65. .then(function (_) {
  66. if (_this._worker) {
  67. return _this._worker.withSyncedResources(resources);
  68. }
  69. })
  70. .then(function (_) { return _client; });
  71. };
  72. return WorkerManager;
  73. }());
  74. export { WorkerManager };