ClientExtensionServices.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Entry point for web view extensions -- extensions that live inside the web view
  2. interface EventSubscription {
  3. eventName: string;
  4. callback: (data: any) => any;
  5. }
  6. /**
  7. * Implements an event dispatcher for the client services
  8. */
  9. export class EventDispatcher implements Editor.Extensions.EventDispatcher {
  10. private subscriptions: EventSubscription[] = [];
  11. sendEvent(eventType: string, data: any) {
  12. this.subscriptions.forEach(sub => {
  13. if (sub.eventName == eventType) {
  14. sub.callback(data);
  15. }
  16. });
  17. }
  18. subscribeToEvent(eventType, callback) {
  19. this.subscriptions.push({
  20. eventName: eventType,
  21. callback: callback
  22. });
  23. }
  24. }
  25. /**
  26. * Generic registry for storing Editor Extension Services
  27. */
  28. class ServiceRegistry<T extends Editor.Extensions.EditorService> implements Editor.Extensions.ServiceRegistry<T> {
  29. registeredServices: T[] = [];
  30. /**
  31. * Adds a service to the registered services list for this type of service
  32. * @param {T} service the service to register
  33. */
  34. register(service: T) {
  35. this.registeredServices.push(service);
  36. }
  37. }
  38. export class ExtensionServiceRegistry extends ServiceRegistry<Editor.ClientExtensions.WebViewService> {
  39. /**
  40. * Allow this service registry to subscribe to events that it is interested in
  41. * @param {EventDispatcher} eventDispatcher The global event dispatcher
  42. */
  43. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  44. eventDispatcher.subscribeToEvent("CodeLoadedNotification", (ev) => this.codeLoaded(ev));
  45. eventDispatcher.subscribeToEvent("ConfigureEditor", (ev) => this.configureEditor(ev));
  46. }
  47. /**
  48. * Called when code is loaded
  49. * @param {Editor.EditorEvents.CodeLoadedEvent} ev Event info about the file that is being loaded
  50. */
  51. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  52. this.registeredServices.forEach((service) => {
  53. try {
  54. // Notify services that the project has just been loaded
  55. if (service.codeLoaded) {
  56. service.codeLoaded(ev);
  57. }
  58. } catch (e) {
  59. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  60. }
  61. });
  62. }
  63. /**
  64. * Called when the editor is requesting to be configured for a particular file
  65. * @param {Editor.EditorEvents.EditorFileEvent} ev
  66. */
  67. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  68. this.registeredServices.forEach((service) => {
  69. try {
  70. // Notify services that the project has just been loaded
  71. if (service.configureEditor) {
  72. service.configureEditor(ev);
  73. }
  74. } catch (e) {
  75. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  76. }
  77. });
  78. }
  79. }