ClientExtensionServices.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // Entry point for web view extensions -- extensions that live inside the web view
  8. interface EventSubscription {
  9. eventName: string;
  10. callback: (data: any) => any;
  11. }
  12. /**
  13. * Implements an event dispatcher for the client services
  14. */
  15. export class EventDispatcher implements Editor.Extensions.EventDispatcher {
  16. private subscriptions: EventSubscription[] = [];
  17. sendEvent(eventType: string, data: any) {
  18. this.subscriptions.forEach(sub => {
  19. if (sub.eventName == eventType) {
  20. sub.callback(data);
  21. }
  22. });
  23. }
  24. subscribeToEvent(eventType, callback) {
  25. this.subscriptions.push({
  26. eventName: eventType,
  27. callback: callback
  28. });
  29. }
  30. }
  31. /**
  32. * Generic registry for storing Editor Extension Services
  33. */
  34. class ServiceRegistry<T extends Editor.Extensions.EditorService> implements Editor.Extensions.ServiceRegistry<T> {
  35. registeredServices: T[] = [];
  36. /**
  37. * Adds a service to the registered services list for this type of service
  38. * @param {T} service the service to register
  39. */
  40. register(service: T) {
  41. this.registeredServices.push(service);
  42. }
  43. }
  44. export class ExtensionServiceRegistry extends ServiceRegistry<Editor.ClientExtensions.WebViewService> {
  45. /**
  46. * Allow this service registry to subscribe to events that it is interested in
  47. * @param {EventDispatcher} eventDispatcher The global event dispatcher
  48. */
  49. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  50. eventDispatcher.subscribeToEvent("CodeLoadedNotification", (ev) => this.codeLoaded(ev));
  51. eventDispatcher.subscribeToEvent("ConfigureEditor", (ev) => this.configureEditor(ev));
  52. }
  53. /**
  54. * Called when code is loaded
  55. * @param {Editor.EditorEvents.CodeLoadedEvent} ev Event info about the file that is being loaded
  56. */
  57. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  58. this.registeredServices.forEach((service) => {
  59. try {
  60. // Notify services that the project has just been loaded
  61. if (service.codeLoaded) {
  62. service.codeLoaded(ev);
  63. }
  64. } catch (e) {
  65. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  66. }
  67. });
  68. }
  69. /**
  70. * Called when the editor is requesting to be configured for a particular file
  71. * @param {Editor.EditorEvents.EditorFileEvent} ev
  72. */
  73. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  74. this.registeredServices.forEach((service) => {
  75. try {
  76. // Notify services that the project has just been loaded
  77. if (service.configureEditor) {
  78. service.configureEditor(ev);
  79. }
  80. } catch (e) {
  81. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  82. }
  83. });
  84. }
  85. }