ClientExtensionServices.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 ClientExtensionEventNames from "./ClientExtensionEventNames";
  23. // Entry point for web view extensions -- extensions that live inside the web view
  24. interface EventSubscription {
  25. eventName: string;
  26. callback: (data: any) => any;
  27. }
  28. /**
  29. * Implements an event dispatcher for the client services
  30. */
  31. export class EventDispatcher implements Editor.Extensions.EventDispatcher {
  32. private subscriptions: EventSubscription[] = [];
  33. sendEvent(eventType: string, data: any) {
  34. this.subscriptions.forEach(sub => {
  35. if (sub.eventName == eventType) {
  36. sub.callback(data);
  37. }
  38. });
  39. }
  40. subscribeToEvent(eventType, callback) {
  41. this.subscriptions.push({
  42. eventName: eventType,
  43. callback: callback
  44. });
  45. }
  46. }
  47. /**
  48. * Generic registry for storing Editor Extension Services
  49. */
  50. class ServiceRegistry<T extends Editor.Extensions.EditorService> implements Editor.Extensions.ServiceRegistry<T> {
  51. registeredServices: T[] = [];
  52. /**
  53. * Adds a service to the registered services list for this type of service
  54. * @param {T} service the service to register
  55. */
  56. register(service: T) {
  57. this.registeredServices.push(service);
  58. }
  59. }
  60. export class ExtensionServiceRegistry extends ServiceRegistry<Editor.ClientExtensions.WebViewService> {
  61. /**
  62. * Allow this service registry to subscribe to events that it is interested in
  63. * @param {EventDispatcher} eventDispatcher The global event dispatcher
  64. */
  65. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  66. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.CodeLoadedEvent, (ev) => this.codeLoaded(ev));
  67. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ConfigureEditorEvent, (ev) => this.configureEditor(ev));
  68. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ResourceRenamedEvent, (ev) => this.renameResource(ev));
  69. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ProjectUnloadedEvent, (ev) => this.projectUnloaded());
  70. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ResourceDeletedEvent, (ev) => this.deleteResource(ev));
  71. }
  72. /**
  73. * Called when code is loaded
  74. * @param {Editor.EditorEvents.CodeLoadedEvent} ev Event info about the file that is being loaded
  75. */
  76. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  77. this.registeredServices.forEach((service) => {
  78. try {
  79. // Notify services that the project has just been loaded
  80. if (service.codeLoaded) {
  81. service.codeLoaded(ev);
  82. }
  83. } catch (e) {
  84. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  85. }
  86. });
  87. }
  88. /**
  89. * Called after a resource has been saved
  90. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  91. */
  92. saveResource(ev: Editor.EditorEvents.SaveResourceEvent) {
  93. // run through and find any services that can handle this.
  94. this.registeredServices.forEach((service) => {
  95. try {
  96. // Verify that the service contains the appropriate methods and that it can save
  97. if (service.save) {
  98. service.save(ev);
  99. }
  100. } catch (e) {
  101. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  102. }
  103. });
  104. }
  105. /**
  106. * Called when a resource has been deleted
  107. */
  108. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  109. this.registeredServices.forEach((service) => {
  110. try {
  111. // Verify that the service contains the appropriate methods and that it can delete
  112. if (service.delete) {
  113. service.delete(ev);
  114. }
  115. } catch (e) {
  116. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  117. }
  118. });
  119. }
  120. /**
  121. * Called when a resource has been renamed
  122. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  123. */
  124. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  125. this.registeredServices.forEach((service) => {
  126. try {
  127. // Verify that the service contains the appropriate methods and that it can handle the rename
  128. if (service.rename) {
  129. service.rename(ev);
  130. }
  131. } catch (e) {
  132. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  133. }
  134. });
  135. }
  136. /**
  137. * Called when the editor is requesting to be configured for a particular file
  138. * @param {Editor.EditorEvents.EditorFileEvent} ev
  139. */
  140. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  141. this.registeredServices.forEach((service) => {
  142. try {
  143. // Notify services that the project has just been loaded
  144. if (service.configureEditor) {
  145. service.configureEditor(ev);
  146. }
  147. } catch (e) {
  148. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  149. }
  150. });
  151. }
  152. /**
  153. * Called when the project is unloaded
  154. */
  155. projectUnloaded() {
  156. this.registeredServices.forEach((service) => {
  157. // Notify services that the project has been unloaded
  158. try {
  159. if (service.projectUnloaded) {
  160. service.projectUnloaded();
  161. }
  162. } catch (e) {
  163. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  164. }
  165. });
  166. }
  167. }