ClientExtensionServices.ts 6.9 KB

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