ServiceLocator.ts 3.4 KB

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