ClientExtensionServices.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. unregister(service: T) {
  60. var index = this.registeredServices.indexOf(service, 0);
  61. if (index > -1) {
  62. this.registeredServices.splice(index, 1);
  63. }
  64. }
  65. }
  66. export class ExtensionServiceRegistry extends ServiceRegistry<Editor.ClientExtensions.WebViewService> {
  67. /**
  68. * Allow this service registry to subscribe to events that it is interested in
  69. * @param {EventDispatcher} eventDispatcher The global event dispatcher
  70. */
  71. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  72. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.CodeLoadedEvent, (ev) => this.codeLoaded(ev));
  73. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ConfigureEditorEvent, (ev) => this.configureEditor(ev));
  74. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ResourceRenamedEvent, (ev) => this.renameResource(ev));
  75. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ProjectUnloadedEvent, (ev) => this.projectUnloaded());
  76. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ResourceDeletedEvent, (ev) => this.deleteResource(ev));
  77. }
  78. /**
  79. * Called when code is loaded
  80. * @param {Editor.EditorEvents.CodeLoadedEvent} ev Event info about the file that is being loaded
  81. */
  82. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  83. this.registeredServices.forEach((service) => {
  84. try {
  85. // Notify services that the project has just been loaded
  86. if (service.codeLoaded) {
  87. service.codeLoaded(ev);
  88. }
  89. } catch (e) {
  90. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  91. }
  92. });
  93. }
  94. /**
  95. * Called after a resource has been saved
  96. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  97. */
  98. saveResource(ev: Editor.EditorEvents.SaveResourceEvent) {
  99. // run through and find any services that can handle this.
  100. this.registeredServices.forEach((service) => {
  101. try {
  102. // Verify that the service contains the appropriate methods and that it can save
  103. if (service.save) {
  104. service.save(ev);
  105. }
  106. } catch (e) {
  107. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  108. }
  109. });
  110. }
  111. /**
  112. * Called when a resource has been deleted
  113. */
  114. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  115. this.registeredServices.forEach((service) => {
  116. try {
  117. // Verify that the service contains the appropriate methods and that it can delete
  118. if (service.delete) {
  119. service.delete(ev);
  120. }
  121. } catch (e) {
  122. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  123. }
  124. });
  125. }
  126. /**
  127. * Called when a resource has been renamed
  128. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  129. */
  130. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  131. this.registeredServices.forEach((service) => {
  132. try {
  133. // Verify that the service contains the appropriate methods and that it can handle the rename
  134. if (service.rename) {
  135. service.rename(ev);
  136. }
  137. } catch (e) {
  138. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  139. }
  140. });
  141. }
  142. /**
  143. * Called when the editor is requesting to be configured for a particular file
  144. * @param {Editor.EditorEvents.EditorFileEvent} ev
  145. */
  146. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  147. this.registeredServices.forEach((service) => {
  148. try {
  149. // Notify services that the project has just been loaded
  150. if (service.configureEditor) {
  151. service.configureEditor(ev);
  152. }
  153. } catch (e) {
  154. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  155. }
  156. });
  157. }
  158. /**
  159. * Called when the project is unloaded
  160. */
  161. projectUnloaded() {
  162. this.registeredServices.forEach((service) => {
  163. // Notify services that the project has been unloaded
  164. try {
  165. if (service.projectUnloaded) {
  166. service.projectUnloaded();
  167. }
  168. } catch (e) {
  169. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  170. }
  171. });
  172. }
  173. }