ServiceLocator.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 HostExtensionServices from "./HostExtensionServices";
  23. import * as EditorUI from "../ui/EditorUI";
  24. import ProjectBasedExtensionLoader from "./coreExtensions/ProjectBasedExtensionLoader";
  25. import TypescriptLanguageExtension from "./languageExtensions/TypescriptLanguageExtension";
  26. import CSharpLanguageExtension from "./languageExtensions/CSharpLanguageExtension";
  27. import DuktapeDebuggerExtension from "./coreExtensions/DuktapeDebuggerExtension";
  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 ServiceLocatorType implements Editor.HostExtensions.HostServiceLocator {
  33. constructor() {
  34. this.resourceServices = new HostExtensionServices.ResourceServicesProvider();
  35. this.projectServices = new HostExtensionServices.ProjectServicesProvider();
  36. this.sceneServices = new HostExtensionServices.SceneServicesProvider();
  37. this.uiServices = new HostExtensionServices.UIServicesProvider();
  38. }
  39. private eventDispatcher: Atomic.UIWidget = null;
  40. resourceServices: HostExtensionServices.ResourceServicesProvider;
  41. projectServices: HostExtensionServices.ProjectServicesProvider;
  42. sceneServices: HostExtensionServices.SceneServicesProvider;
  43. uiServices: HostExtensionServices.UIServicesProvider;
  44. loadService(service: Editor.HostExtensions.HostEditorService) {
  45. try {
  46. service.initialize(this);
  47. } catch (e) {
  48. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  49. }
  50. }
  51. /**
  52. * This is where the top level window will allow the service locator to listen for events and act on them.
  53. * @param {Atomic.UIWidget} frame
  54. */
  55. subscribeToEvents(frame: Atomic.UIWidget) {
  56. this.eventDispatcher = frame;
  57. this.resourceServices.subscribeToEvents(this);
  58. this.projectServices.subscribeToEvents(this);
  59. this.sceneServices.subscribeToEvents(this);
  60. this.uiServices.subscribeToEvents(this);
  61. }
  62. /**
  63. * Send a custom event. This can be used by services to publish custom events
  64. * @param {string} eventType
  65. * @param {any} data
  66. */
  67. sendEvent<T extends Atomic.EventCallbackMetaData>(eventCallbackMetaData:T)
  68. sendEvent(eventType: string, data: any)
  69. sendEvent(eventTypeOrMetaData: any, data?: any) {
  70. if (this.eventDispatcher) {
  71. if (data) {
  72. this.eventDispatcher.sendEvent(eventTypeOrMetaData, data);
  73. } else {
  74. this.eventDispatcher.sendEvent(eventTypeOrMetaData);
  75. }
  76. }
  77. }
  78. /**
  79. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  80. * @param {string} eventType
  81. * @param {any} callback
  82. * or
  83. * @param {Atomic.EventMetaData} wrappedEvent A Atomic pre-wrapped event object
  84. */
  85. subscribeToEvent(eventType: string, callback: (data: any) => void);
  86. subscribeToEvent(wrappedEvent: Atomic.EventMetaData);
  87. subscribeToEvent(eventTypeOrWrapped: any, callback?: any) {
  88. if (this.eventDispatcher) {
  89. if (callback) {
  90. this.eventDispatcher.subscribeToEvent(eventTypeOrWrapped, callback);
  91. } else {
  92. this.eventDispatcher.subscribeToEvent(eventTypeOrWrapped);
  93. }
  94. }
  95. }
  96. }
  97. // Singleton service locator that can be referenced
  98. const serviceLocator = new ServiceLocatorType();
  99. export default serviceLocator;
  100. // Load up all the internal services
  101. serviceLocator.loadService(new ProjectBasedExtensionLoader());
  102. serviceLocator.loadService(new TypescriptLanguageExtension());
  103. serviceLocator.loadService(new CSharpLanguageExtension());
  104. serviceLocator.loadService(new DuktapeDebuggerExtension());