ServiceLocator.ts 4.1 KB

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