ServiceLocator.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 HostInteropType from "../interop";
  23. import * as ClientExtensionServices from "./ClientExtensionServices";
  24. // Initialize and configure the extensions
  25. import tsExtension from "./languageExtensions/typescript/TypescriptLanguageExtension";
  26. import jsExtension from "./languageExtensions/javascript/JavascriptLanguageExtension";
  27. import tbExtension from "./languageExtensions/turbobadger/TurboBadgerLanguageExtension";
  28. /**
  29. * Generic service locator of editor services that may be injected by either a plugin
  30. * or by the editor itself.
  31. */
  32. export class ClientServiceLocatorType implements Editor.ClientExtensions.ClientServiceLocator {
  33. constructor() {
  34. this.services = new ClientExtensionServices.ExtensionServiceRegistry();
  35. this.services.subscribeToEvents(this);
  36. }
  37. private services: ClientExtensionServices.ExtensionServiceRegistry;
  38. private eventDispatcher: Editor.Extensions.EventDispatcher = new ClientExtensionServices.EventDispatcher();
  39. /**
  40. * Returns the Host Interop module
  41. * @return {Editor.ClientExtensions.HostInterop}
  42. */
  43. getHostInterop(): Editor.ClientExtensions.HostInterop {
  44. return HostInteropType.getInstance();
  45. }
  46. loadService(service: Editor.ClientExtensions.ClientEditorService) {
  47. try {
  48. this.services.register(service);
  49. service.initialize(this);
  50. } catch (e) {
  51. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  52. }
  53. }
  54. /**
  55. * Send a custom event. This can be used by services to publish custom events
  56. * @param {string} eventType
  57. * @param {any} data
  58. */
  59. sendEvent(eventType: string, data: any) {
  60. if (this.eventDispatcher) {
  61. this.eventDispatcher.sendEvent(eventType, data);
  62. }
  63. }
  64. /**
  65. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  66. * @param {string} eventType
  67. * @param {any} callback
  68. */
  69. subscribeToEvent(eventType: string, callback: (data: any) => void) {
  70. if (this.eventDispatcher) {
  71. this.eventDispatcher.subscribeToEvent(eventType, callback);
  72. }
  73. }
  74. }
  75. // Singleton service locator that can be referenced
  76. const serviceLocator = new ClientServiceLocatorType();
  77. export default serviceLocator;
  78. // Load up all the internal services
  79. serviceLocator.loadService(new tsExtension());
  80. serviceLocator.loadService(new jsExtension());
  81. serviceLocator.loadService(new tbExtension());