ServiceLocator.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import HostInteropType from "../interop";
  8. import * as ClientExtensionServices from "./ClientExtensionServices";
  9. // Initialize and configure the extensions
  10. import tsExtension from "./languageExtensions/typescript/TypescriptLanguageExtension";
  11. import jsExtension from "./languageExtensions/javascript/JavascriptLanguageExtension";
  12. /**
  13. * Generic service locator of editor services that may be injected by either a plugin
  14. * or by the editor itself.
  15. */
  16. export class ClientServiceLocatorType implements Editor.ClientExtensions.ClientServiceLocator {
  17. constructor() {
  18. this.services = new ClientExtensionServices.ExtensionServiceRegistry();
  19. this.services.subscribeToEvents(this);
  20. }
  21. private services: ClientExtensionServices.ExtensionServiceRegistry;
  22. private eventDispatcher: Editor.Extensions.EventDispatcher = new ClientExtensionServices.EventDispatcher();
  23. /**
  24. * Returns the Host Interop module
  25. * @return {Editor.ClientExtensions.HostInterop}
  26. */
  27. getHostInterop(): Editor.ClientExtensions.HostInterop {
  28. return HostInteropType.getInstance();
  29. }
  30. loadService(service: Editor.ClientExtensions.ClientEditorService) {
  31. try {
  32. this.services.register(service);
  33. service.initialize(this);
  34. } catch (e) {
  35. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  36. }
  37. }
  38. /**
  39. * Send a custom event. This can be used by services to publish custom events
  40. * @param {string} eventType
  41. * @param {any} data
  42. */
  43. sendEvent(eventType: string, data: any) {
  44. if (this.eventDispatcher) {
  45. this.eventDispatcher.sendEvent(eventType, data);
  46. }
  47. }
  48. /**
  49. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  50. * @param {string} eventType
  51. * @param {any} callback
  52. */
  53. subscribeToEvent(eventType: string, callback: (data: any) => void) {
  54. if (this.eventDispatcher) {
  55. this.eventDispatcher.subscribeToEvent(eventType, callback);
  56. }
  57. }
  58. }
  59. // Singleton service locator that can be referenced
  60. const serviceLocator = new ClientServiceLocatorType();
  61. export default serviceLocator;
  62. // Load up all the internal services
  63. serviceLocator.loadService(new tsExtension());
  64. serviceLocator.loadService(new jsExtension());