ClientExtensionServices.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.ServiceEventListener> implements Editor.Extensions.ServicesProvider<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. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.CodeSavedEvent, (ev) => this.saveCode(ev));
  78. }
  79. /**
  80. * Called when code is loaded
  81. * @param {Editor.EditorEvents.CodeLoadedEvent} ev Event info about the file that is being loaded
  82. */
  83. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  84. this.registeredServices.forEach((service) => {
  85. try {
  86. // Notify services that the project has just been loaded
  87. if (service.codeLoaded) {
  88. service.codeLoaded(ev);
  89. }
  90. } catch (e) {
  91. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  92. }
  93. });
  94. }
  95. /**
  96. * Called after code has been saved
  97. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  98. */
  99. saveCode(ev: Editor.EditorEvents.CodeSavedEvent) {
  100. // run through and find any services that can handle this.
  101. this.registeredServices.forEach((service) => {
  102. try {
  103. // Verify that the service contains the appropriate methods and that it can save
  104. if (service.save) {
  105. service.save(ev);
  106. }
  107. } catch (e) {
  108. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  109. }
  110. });
  111. }
  112. /**
  113. * Called when a resource has been deleted
  114. */
  115. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  116. this.registeredServices.forEach((service) => {
  117. try {
  118. // Verify that the service contains the appropriate methods and that it can delete
  119. if (service.delete) {
  120. service.delete(ev);
  121. }
  122. } catch (e) {
  123. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  124. }
  125. });
  126. }
  127. /**
  128. * Called when a resource has been renamed
  129. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  130. */
  131. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  132. this.registeredServices.forEach((service) => {
  133. try {
  134. // Verify that the service contains the appropriate methods and that it can handle the rename
  135. if (service.rename) {
  136. service.rename(ev);
  137. }
  138. } catch (e) {
  139. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  140. }
  141. });
  142. }
  143. /**
  144. * Called when the editor is requesting to be configured for a particular file
  145. * @param {Editor.EditorEvents.EditorFileEvent} ev
  146. */
  147. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  148. this.registeredServices.forEach((service) => {
  149. try {
  150. // Notify services that the project has just been loaded
  151. if (service.configureEditor) {
  152. service.configureEditor(ev);
  153. }
  154. } catch (e) {
  155. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  156. }
  157. });
  158. }
  159. /**
  160. * Called when the project is unloaded
  161. */
  162. projectUnloaded() {
  163. this.registeredServices.forEach((service) => {
  164. // Notify services that the project has been unloaded
  165. try {
  166. if (service.projectUnloaded) {
  167. service.projectUnloaded();
  168. }
  169. } catch (e) {
  170. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  171. }
  172. });
  173. }
  174. }