From 6187c942bedebeb2f452ed0856652f90cd5c5772 Mon Sep 17 00:00:00 2001 From: Idan Amit Date: Sun, 15 Apr 2018 19:19:08 +0300 Subject: New observable notify design in pubsub Implemented the new subscription mechanism for the pub sub notify function Change-Id: I5e6484adf1a0413d48b54b55048cda1a59b387ee Issue-ID: SDC-1178 Signed-off-by: Idan Amit --- catalog-ui/src/app/models/base-pubsub.ts | 81 +++++++++++++++++++----------- catalog-ui/src/app/models/plugin-pubsub.ts | 29 +++++++++++ 2 files changed, 81 insertions(+), 29 deletions(-) create mode 100644 catalog-ui/src/app/models/plugin-pubsub.ts (limited to 'catalog-ui/src/app/models') diff --git a/catalog-ui/src/app/models/base-pubsub.ts b/catalog-ui/src/app/models/base-pubsub.ts index ca313b15f5..b9ff788a57 100644 --- a/catalog-ui/src/app/models/base-pubsub.ts +++ b/catalog-ui/src/app/models/base-pubsub.ts @@ -5,10 +5,12 @@ export class BasePubSub { subscribers: Map; eventsCallbacks: Array; clientId: string; + eventsToWait: Map>; constructor(pluginId: string) { this.subscribers = new Map(); - this.eventsCallbacks = new Array(); + this.eventsCallbacks = []; + this.eventsToWait = new Map>(); this.clientId = pluginId; this.onMessage = this.onMessage.bind(this); @@ -29,7 +31,13 @@ export class BasePubSub { } public on(callback: Function) { - this.eventsCallbacks.push(callback); + let functionExists = this.eventsCallbacks.find((func: Function) => { + return callback.toString() == func.toString() + }); + + if (!functionExists) { + this.eventsCallbacks.push(callback); + } } public off(callback: Function) { @@ -44,9 +52,49 @@ export class BasePubSub { originId: this.clientId } as IPubSubEvent; - this.subscribers.forEach( (subscriber: ISubscriber, id: string) => { - subscriber.window.postMessage(eventObj, subscriber.locationUrl) + this.subscribers.forEach( (subscriber: ISubscriber, subscriberId: string) => { + subscriber.window.postMessage(eventObj, subscriber.locationUrl); + }); + + return { + subscribe: function(callbackFn) { + + if(this.subscribers.size !== 0) { + let subscribersToNotify = Array.from(this.subscribers.keys()); + + const checkNotifyComplete = (subscriberId: string) => { + + let index = subscribersToNotify.indexOf(subscriberId); + subscribersToNotify.splice(index, 1); + + if (subscribersToNotify.length === 0) { + callbackFn(); + } + }; + + this.subscribers.forEach((subscriber: ISubscriber, subscriberId: string) => { + if (this.eventsToWait.has(subscriberId) && this.eventsToWait.get(subscriberId).indexOf(eventType) !== -1) { + + const actionCompletedFunction = (eventData, subId = subscriberId) => { + if (eventData.type == "ACTION_COMPLETED") { + checkNotifyComplete(subId); + } + this.off(actionCompletedFunction); + + }; + this.on(actionCompletedFunction); + } + else { + checkNotifyComplete(subscriberId); + } + }); + } + else { + callbackFn(); + } + }.bind(this) + } } protected onMessage(event: any) { @@ -58,31 +106,6 @@ export class BasePubSub { } } -export class PluginPubSub extends BasePubSub { - - constructor(pluginId: string, parentUrl: string) { - super(pluginId); - this.register('sdc-hub', window.parent, parentUrl); - 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; diff --git a/catalog-ui/src/app/models/plugin-pubsub.ts b/catalog-ui/src/app/models/plugin-pubsub.ts new file mode 100644 index 0000000000..3a34de99cc --- /dev/null +++ b/catalog-ui/src/app/models/plugin-pubsub.ts @@ -0,0 +1,29 @@ +import {BasePubSub} from "./base-pubsub"; + +declare const window: Window; + +export class PluginPubSub extends BasePubSub { + + constructor(pluginId: string, parentUrl: string, eventsToWait?: Array) { + super(pluginId); + this.register('sdc-hub', window.parent, parentUrl); + this.subscribe(eventsToWait); + } + + public subscribe(eventsToWait?: Array) { + const registerData = { + pluginId: this.clientId, + eventsToWait: eventsToWait || [] + }; + + this.notify('PLUGIN_REGISTER', registerData); + } + + public unsubscribe() { + const unregisterData = { + pluginId: this.clientId + }; + + this.notify('PLUGIN_UNREGISTER', unregisterData); + } +} -- cgit 1.2.3-korg