Idan Amit | 71904f2 | 2018-02-13 10:38:16 +0200 | [diff] [blame] | 1 | declare const window: Window; |
| 2 | |
| 3 | export class BasePubSub { |
| 4 | |
| 5 | subscribers: Map<string, ISubscriber>; |
| 6 | eventsCallbacks: Array<Function>; |
| 7 | clientId: string; |
| 8 | |
| 9 | constructor(pluginId: string) { |
| 10 | this.subscribers = new Map<string, ISubscriber>(); |
| 11 | this.eventsCallbacks = new Array<Function>(); |
| 12 | this.clientId = pluginId; |
| 13 | this.onMessage = this.onMessage.bind(this); |
| 14 | |
| 15 | window.addEventListener("message", this.onMessage); |
| 16 | } |
| 17 | |
| 18 | public register(subscriberId: string, subscriberWindow: Window, subscriberUrl: string) { |
| 19 | const subscriber = { |
| 20 | window: subscriberWindow, |
| 21 | locationUrl: subscriberUrl || subscriberWindow.location.href |
| 22 | } as ISubscriber; |
| 23 | |
| 24 | this.subscribers.set(subscriberId, subscriber); |
| 25 | } |
| 26 | |
| 27 | public unregister(subscriberId: string) { |
| 28 | this.subscribers.delete(subscriberId); |
| 29 | } |
| 30 | |
| 31 | public on(callback: Function) { |
| 32 | this.eventsCallbacks.push(callback); |
| 33 | } |
| 34 | |
| 35 | public off(callback: Function) { |
| 36 | let index = this.eventsCallbacks.indexOf(callback); |
| 37 | this.eventsCallbacks.splice(index, 1) |
| 38 | } |
| 39 | |
Idan Amit | f97bae3 | 2018-03-06 13:52:58 +0200 | [diff] [blame^] | 40 | public notify(eventType:string, eventData?:any) { |
Idan Amit | 71904f2 | 2018-02-13 10:38:16 +0200 | [diff] [blame] | 41 | let eventObj = { |
| 42 | type: eventType, |
| 43 | data: eventData, |
| 44 | originId: this.clientId |
| 45 | } as IPubSubEvent; |
| 46 | |
| 47 | this.subscribers.forEach( (subscriber: ISubscriber, id: string) => { |
| 48 | subscriber.window.postMessage(eventObj, subscriber.locationUrl) |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | protected onMessage(event: any) { |
| 53 | if (this.subscribers.has(event.data.originId)) { |
| 54 | this.eventsCallbacks.forEach((callback: Function) => { |
| 55 | callback(event.data, event); |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export class PluginPubSub extends BasePubSub { |
| 62 | |
Idan Amit | f97bae3 | 2018-03-06 13:52:58 +0200 | [diff] [blame^] | 63 | constructor(pluginId: string, parentUrl: string) { |
Idan Amit | 71904f2 | 2018-02-13 10:38:16 +0200 | [diff] [blame] | 64 | super(pluginId); |
Idan Amit | f97bae3 | 2018-03-06 13:52:58 +0200 | [diff] [blame^] | 65 | this.register('sdc-hub', window.parent, parentUrl); |
Idan Amit | 71904f2 | 2018-02-13 10:38:16 +0200 | [diff] [blame] | 66 | this.subscribe(); |
| 67 | } |
| 68 | |
| 69 | public subscribe() { |
| 70 | const registerData = { |
| 71 | pluginId: this.clientId |
| 72 | }; |
| 73 | |
| 74 | this.notify('PLUGIN_REGISTER', registerData); |
| 75 | } |
| 76 | |
| 77 | public unsubscribe() { |
| 78 | const unregisterData = { |
| 79 | pluginId: this.clientId |
| 80 | }; |
| 81 | |
| 82 | this.notify('PLUGIN_UNREGISTER', unregisterData); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | export interface IPubSubEvent { |
| 87 | type: string; |
| 88 | originId: string; |
| 89 | data: any; |
| 90 | } |
| 91 | |
| 92 | export interface ISubscriber { |
| 93 | window: Window; |
| 94 | locationUrl: string; |
| 95 | } |