ServiceLocator.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import * as ClientExtensionServices from "./ClientExtensionServices";
  23. // Initialize and configure the extensions
  24. import tsExtension from "./languageExtensions/typescript/TypescriptLanguageExtension";
  25. import jsExtension from "./languageExtensions/javascript/JavascriptLanguageExtension";
  26. import tbExtension from "./languageExtensions/turbobadger/TurboBadgerLanguageExtension";
  27. /**
  28. * Generic service locator of editor services that may be injected by either a plugin
  29. * or by the editor itself.
  30. */
  31. export class ClientServiceLocatorType implements Editor.ClientExtensions.ClientServiceLocator {
  32. constructor() {
  33. this.clientServices = new ClientExtensionServices.WebViewServicesProvider();
  34. this.clientServices.subscribeToEvents(this);
  35. }
  36. private eventDispatcher: Editor.Extensions.EventDispatcher = new ClientExtensionServices.EventDispatcher();
  37. clientServices: ClientExtensionServices.WebViewServicesProvider;
  38. loadService(service: Editor.ClientExtensions.ClientEditorService) {
  39. try {
  40. service.initialize(this);
  41. } catch (e) {
  42. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  43. }
  44. }
  45. /**
  46. * Send a custom event. This can be used by services to publish custom events
  47. * @param {string} eventType
  48. * @param {any} data
  49. */
  50. sendEvent(eventType: string, data: any) {
  51. if (this.eventDispatcher) {
  52. this.eventDispatcher.sendEvent(eventType, data);
  53. }
  54. }
  55. /**
  56. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  57. * @param {string} eventType
  58. * @param {any} callback
  59. */
  60. subscribeToEvent(eventType: string, callback: (data: any) => void) {
  61. if (this.eventDispatcher) {
  62. this.eventDispatcher.subscribeToEvent(eventType, callback);
  63. }
  64. }
  65. }
  66. // Singleton service locator that can be referenced
  67. const serviceLocator = new ClientServiceLocatorType();
  68. export default serviceLocator;
  69. // Load up all the internal services
  70. serviceLocator.loadService(new tsExtension());
  71. serviceLocator.loadService(new jsExtension());
  72. serviceLocator.loadService(new tbExtension());