ClientExtensionServices.ts 7.7 KB

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