aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/models/base-pubsub.ts
diff options
context:
space:
mode:
authorIdan Amit <ia096e@intl.att.com>2018-02-13 10:38:16 +0200
committerIdan Amit <ia096e@intl.att.com>2018-02-13 16:43:30 +0200
commit71904f241cd3047054dc0a36c04120a3f53205ae (patch)
tree4bc2a7de56d7737131ddab4f2d206a655dbc0d6d /catalog-ui/src/app/models/base-pubsub.ts
parent332f0679fbd23532a058f0ff6a7cffaed3b5424e (diff)
Plugin pubsub implementation
Added implementation for sdc-hub and plugin pubsub classes Added timeout in the plugin head request health check Passing parentUrl in query params to each plugin Change-Id: Ie94aa4b398dd2fcfdf2d134f6c5785d3aa50d237 Issue-ID: SDC-1007 Signed-off-by: Idan Amit <ia096e@intl.att.com>
Diffstat (limited to 'catalog-ui/src/app/models/base-pubsub.ts')
-rw-r--r--catalog-ui/src/app/models/base-pubsub.ts95
1 files changed, 95 insertions, 0 deletions
diff --git a/catalog-ui/src/app/models/base-pubsub.ts b/catalog-ui/src/app/models/base-pubsub.ts
new file mode 100644
index 0000000000..ca500ca173
--- /dev/null
+++ b/catalog-ui/src/app/models/base-pubsub.ts
@@ -0,0 +1,95 @@
+declare const window: Window;
+
+export class BasePubSub {
+
+ subscribers: Map<string, ISubscriber>;
+ eventsCallbacks: Array<Function>;
+ clientId: string;
+
+ constructor(pluginId: string) {
+ this.subscribers = new Map<string, ISubscriber>();
+ this.eventsCallbacks = new Array<Function>();
+ this.clientId = pluginId;
+ this.onMessage = this.onMessage.bind(this);
+
+ window.addEventListener("message", this.onMessage);
+ }
+
+ public register(subscriberId: string, subscriberWindow: Window, subscriberUrl: string) {
+ const subscriber = {
+ window: subscriberWindow,
+ locationUrl: subscriberUrl || subscriberWindow.location.href
+ } as ISubscriber;
+
+ this.subscribers.set(subscriberId, subscriber);
+ }
+
+ public unregister(subscriberId: string) {
+ this.subscribers.delete(subscriberId);
+ }
+
+ public on(callback: Function) {
+ this.eventsCallbacks.push(callback);
+ }
+
+ public off(callback: Function) {
+ let index = this.eventsCallbacks.indexOf(callback);
+ this.eventsCallbacks.splice(index, 1)
+ }
+
+ public notify(eventType:string, eventData:any) {
+ let eventObj = {
+ type: eventType,
+ data: eventData,
+ originId: this.clientId
+ } as IPubSubEvent;
+
+ this.subscribers.forEach( (subscriber: ISubscriber, id: string) => {
+ subscriber.window.postMessage(eventObj, subscriber.locationUrl)
+ });
+ }
+
+ protected onMessage(event: any) {
+ if (this.subscribers.has(event.data.originId)) {
+ this.eventsCallbacks.forEach((callback: Function) => {
+ callback(event.data, event);
+ })
+ }
+ }
+}
+
+export class PluginPubSub extends BasePubSub {
+
+ constructor(pluginId: string, subscriberUrl: string) {
+ super(pluginId);
+ this.register('sdc-hub', window.parent, subscriberUrl);
+ this.subscribe();
+ }
+
+ public subscribe() {
+ const registerData = {
+ pluginId: this.clientId
+ };
+
+ this.notify('PLUGIN_REGISTER', registerData);
+ }
+
+ public unsubscribe() {
+ const unregisterData = {
+ pluginId: this.clientId
+ };
+
+ this.notify('PLUGIN_UNREGISTER', unregisterData);
+ }
+}
+
+export interface IPubSubEvent {
+ type: string;
+ originId: string;
+ data: any;
+}
+
+export interface ISubscriber {
+ window: Window;
+ locationUrl: string;
+}