aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/models/base-pubsub.ts
diff options
context:
space:
mode:
authorIdan Amit <ia096e@intl.att.com>2018-04-15 19:19:08 +0300
committerIdan Amit <ia096e@intl.att.com>2018-04-15 19:19:08 +0300
commit6187c942bedebeb2f452ed0856652f90cd5c5772 (patch)
tree9c3463873e34d9097f2119b9ed35900f6ecab733 /catalog-ui/src/app/models/base-pubsub.ts
parent821b76c6b81ebf96e8fd8340ac5f6ed79f8ed22a (diff)
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 <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.ts81
1 files changed, 52 insertions, 29 deletions
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<string, ISubscriber>;
eventsCallbacks: Array<Function>;
clientId: string;
+ eventsToWait: Map<string, Array<string>>;
constructor(pluginId: string) {
this.subscribers = new Map<string, ISubscriber>();
- this.eventsCallbacks = new Array<Function>();
+ this.eventsCallbacks = [];
+ this.eventsToWait = new Map<string, Array<string>>();
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;