ServiceLocator.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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<T extends Atomic.EventCallbackMetaData>(eventCallbackMetaData:T)
  51. sendEvent(eventType: string, data: any)
  52. sendEvent(eventTypeOrWrapped: any, data?: any) {
  53. if (this.eventDispatcher) {
  54. if (data) {
  55. this.eventDispatcher.sendEvent(eventTypeOrWrapped, data);
  56. } else {
  57. this.eventDispatcher.sendEvent(eventTypeOrWrapped);
  58. }
  59. }
  60. }
  61. /**
  62. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  63. * @param {string} eventType
  64. * @param {any} callback
  65. */
  66. subscribeToEvent(eventType: string, callback: (...params) => any);
  67. subscribeToEvent(wrapped: Atomic.EventMetaData);
  68. subscribeToEvent(eventTypeOrWrapped: any, callback?: any) {
  69. if (this.eventDispatcher) {
  70. if (callback) {
  71. this.eventDispatcher.subscribeToEvent(eventTypeOrWrapped, callback);
  72. } else {
  73. this.eventDispatcher.subscribeToEvent(eventTypeOrWrapped);
  74. }
  75. }
  76. }
  77. }
  78. // Singleton service locator that can be referenced
  79. const serviceLocator = new ClientServiceLocatorType();
  80. export default serviceLocator;
  81. // Load up all the internal services
  82. serviceLocator.loadService(new tsExtension());
  83. serviceLocator.loadService(new jsExtension());
  84. serviceLocator.loadService(new tbExtension());