workerManager.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 = window.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 `CSSWorker` instance
  42. moduleId: 'vs/language/css/cssWorker',
  43. label: this._defaults.languageId,
  44. // passed in to the create() method
  45. createData: {
  46. options: this._defaults.options,
  47. languageId: this._defaults.languageId
  48. }
  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. return _this._worker.withSyncedResources(resources);
  67. })
  68. .then(function (_) { return _client; });
  69. };
  70. return WorkerManager;
  71. }());
  72. export { WorkerManager };