bf5123cc985048960e4b69fd41535ab0a83eaf861ffd2ea3d04a105cd26c184408ad02419d3e6492f21ce0c6e4f197c32f486753ae457bdf61810825c156fe 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 { EditorWorkerClient } from './editorWorkerService.js';
  6. import * as types from '../../../base/common/types.js';
  7. /**
  8. * Create a new web worker that has model syncing capabilities built in.
  9. * Specify an AMD module to load that will `create` an object that will be proxied.
  10. */
  11. export function createWebWorker(modelService, languageConfigurationService, opts) {
  12. return new MonacoWebWorkerImpl(modelService, languageConfigurationService, opts);
  13. }
  14. class MonacoWebWorkerImpl extends EditorWorkerClient {
  15. constructor(modelService, languageConfigurationService, opts) {
  16. super(modelService, opts.keepIdleModels || false, opts.label, languageConfigurationService);
  17. this._foreignModuleId = opts.moduleId;
  18. this._foreignModuleCreateData = opts.createData || null;
  19. this._foreignModuleHost = opts.host || null;
  20. this._foreignProxy = null;
  21. }
  22. // foreign host request
  23. fhr(method, args) {
  24. if (!this._foreignModuleHost || typeof this._foreignModuleHost[method] !== 'function') {
  25. return Promise.reject(new Error('Missing method ' + method + ' or missing main thread foreign host.'));
  26. }
  27. try {
  28. return Promise.resolve(this._foreignModuleHost[method].apply(this._foreignModuleHost, args));
  29. }
  30. catch (e) {
  31. return Promise.reject(e);
  32. }
  33. }
  34. _getForeignProxy() {
  35. if (!this._foreignProxy) {
  36. this._foreignProxy = this._getProxy().then((proxy) => {
  37. const foreignHostMethods = this._foreignModuleHost ? types.getAllMethodNames(this._foreignModuleHost) : [];
  38. return proxy.loadForeignModule(this._foreignModuleId, this._foreignModuleCreateData, foreignHostMethods).then((foreignMethods) => {
  39. this._foreignModuleCreateData = null;
  40. const proxyMethodRequest = (method, args) => {
  41. return proxy.fmr(method, args);
  42. };
  43. const createProxyMethod = (method, proxyMethodRequest) => {
  44. return function () {
  45. const args = Array.prototype.slice.call(arguments, 0);
  46. return proxyMethodRequest(method, args);
  47. };
  48. };
  49. const foreignProxy = {};
  50. for (const foreignMethod of foreignMethods) {
  51. foreignProxy[foreignMethod] = createProxyMethod(foreignMethod, proxyMethodRequest);
  52. }
  53. return foreignProxy;
  54. });
  55. });
  56. }
  57. return this._foreignProxy;
  58. }
  59. getProxy() {
  60. return this._getForeignProxy();
  61. }
  62. withSyncedResources(resources) {
  63. return this._withSyncedResources(resources).then(_ => this.getProxy());
  64. }
  65. }