Plugin pubsub implementation
[sdc.git] / catalog-ui / src / app / ng2 / services / event-bus.service.ts
1 import { Injectable } from '@angular/core';
2 import {BasePubSub, IPubSubEvent} from "../../models/base-pubsub";
3
4 @Injectable()
5 export class EventBusService extends BasePubSub {
6
7     constructor() {
8         super("sdc-hub");
9     }
10
11     protected handlePluginRegistration(eventData: IPubSubEvent, event: any) {
12         if (eventData.type === 'PLUGIN_REGISTER') {
13             this.register(eventData.data.pluginId, event.source, event.origin);
14         } else if (eventData.type === 'PLUGIN_UNREGISTER') {
15             this.unregister(eventData.data.pluginId);
16         }
17     }
18
19     public unregister(pluginId: string) {
20         const unregisterData = {
21             pluginId: pluginId
22         };
23
24         this.notify('PLUGIN_CLOSE', unregisterData);
25         super.unregister(pluginId);
26     }
27
28     protected onMessage(event: any) {
29         if (event.data.type === 'PLUGIN_REGISTER') {
30             this.handlePluginRegistration(event.data, event);
31         }
32
33         super.onMessage(event);
34
35         if (event.data.type === 'PLUGIN_UNREGISTER') {
36             this.handlePluginRegistration(event.data, event);
37         }
38     }
39 }