ServiceLocator.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import debuggerHookExtension from "./languageExtensions/debugger/EditorDebuggingHook";
  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.clientServices = new ClientExtensionServices.WebViewServicesProvider();
  35. this.clientServices.subscribeToEvents(this);
  36. }
  37. private eventDispatcher: Editor.Extensions.EventDispatcher = new ClientExtensionServices.EventDispatcher();
  38. clientServices: ClientExtensionServices.WebViewServicesProvider;
  39. loadService(service: Editor.ClientExtensions.ClientEditorService) {
  40. try {
  41. service.initialize(this);
  42. } catch (e) {
  43. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  44. }
  45. }
  46. /**
  47. * Send a custom event. This can be used by services to publish custom events
  48. * @param {string} eventType
  49. * @param {any} data
  50. */
  51. sendEvent<T extends Atomic.EventCallbackMetaData>(eventCallbackMetaData:T)
  52. sendEvent(eventType: string, data: any)
  53. sendEvent(eventTypeOrWrapped: any, data?: any) {
  54. if (this.eventDispatcher) {
  55. if (data) {
  56. this.eventDispatcher.sendEvent(eventTypeOrWrapped, data);
  57. } else {
  58. this.eventDispatcher.sendEvent(eventTypeOrWrapped);
  59. }
  60. }
  61. }
  62. /**
  63. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  64. * @param {string} eventType
  65. * @param {any} callback
  66. */
  67. subscribeToEvent(eventType: string, callback: (...params) => any);
  68. subscribeToEvent(wrapped: Atomic.EventMetaData);
  69. subscribeToEvent(eventTypeOrWrapped: any, callback?: any) {
  70. if (this.eventDispatcher) {
  71. if (callback) {
  72. this.eventDispatcher.subscribeToEvent(eventTypeOrWrapped, callback);
  73. } else {
  74. this.eventDispatcher.subscribeToEvent(eventTypeOrWrapped);
  75. }
  76. }
  77. }
  78. }
  79. // Singleton service locator that can be referenced
  80. const serviceLocator = new ClientServiceLocatorType();
  81. export default serviceLocator;
  82. // Load up all the internal services
  83. serviceLocator.loadService(new tsExtension());
  84. serviceLocator.loadService(new jsExtension());
  85. serviceLocator.loadService(new tbExtension());
  86. serviceLocator.loadService(new debuggerHookExtension());