2021-12-10 02:37:01 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
import {Events, MessageBus, Parameters} from 'protocol';
|
2020-01-27 18:40:18 +00:00
|
|
|
|
|
|
|
|
type AnyEventCallback<Ev> = <E extends keyof Ev>(topic: E, args: Parameters<Ev[E]>) => void;
|
|
|
|
|
|
|
|
|
|
export class IFrameMessageBus extends MessageBus<Events> {
|
|
|
|
|
private _listeners: any[] = [];
|
|
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
constructor(
|
|
|
|
|
private _source: string, private _destination: string, private _docWindow: () => Window) {
|
2020-01-27 18:40:18 +00:00
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 18:18:52 +00:00
|
|
|
onAny(cb: AnyEventCallback<Events>): () => void {
|
2020-01-27 18:40:18 +00:00
|
|
|
const listener = (e: MessageEvent) => {
|
|
|
|
|
if (!e.data || !e.data.topic || e.data.source !== this._destination) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cb(e.data.topic, e.data.args);
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('message', listener);
|
|
|
|
|
this._listeners.push(listener);
|
|
|
|
|
return () => {
|
|
|
|
|
this._listeners.splice(this._listeners.indexOf(listener), 1);
|
|
|
|
|
window.removeEventListener('message', listener);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 18:18:52 +00:00
|
|
|
on<E extends keyof Events>(topic: E, cb: Events[E]): () => void {
|
2020-01-27 18:40:18 +00:00
|
|
|
const listener = (e: MessageEvent) => {
|
|
|
|
|
if (!e.data || e.data.source !== this._destination || !e.data.topic) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (e.data.topic === topic) {
|
|
|
|
|
cb.apply(null, e.data.args);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('message', listener);
|
|
|
|
|
this._listeners.push(listener);
|
|
|
|
|
return () => {
|
|
|
|
|
this._listeners.splice(this._listeners.indexOf(listener), 1);
|
|
|
|
|
window.removeEventListener('message', listener);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 18:18:52 +00:00
|
|
|
once<E extends keyof Events>(topic: E, cb: Events[E]): void {
|
2020-01-27 18:40:18 +00:00
|
|
|
const listener = (e: MessageEvent) => {
|
|
|
|
|
if (!e.data || e.data.source !== this._destination || !e.data.topic) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (e.data.topic === topic) {
|
|
|
|
|
cb.apply(null, e.data.args);
|
2020-02-03 23:22:12 +00:00
|
|
|
window.removeEventListener('message', listener);
|
2020-01-27 18:40:18 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('message', listener);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 23:35:02 +00:00
|
|
|
emit<E extends keyof Events>(topic: E, args?: Parameters<Events[E]>): boolean {
|
2020-03-18 00:48:29 +00:00
|
|
|
this._docWindow().postMessage(
|
2021-12-09 05:44:17 +00:00
|
|
|
{
|
|
|
|
|
source: this._source,
|
|
|
|
|
topic,
|
|
|
|
|
args,
|
|
|
|
|
},
|
|
|
|
|
'*');
|
2020-03-23 23:35:02 +00:00
|
|
|
return true;
|
2020-01-27 18:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-31 18:54:51 +00:00
|
|
|
destroy(): void {
|
2020-03-23 23:35:02 +00:00
|
|
|
this._listeners.forEach((l) => window.removeEventListener('message', l));
|
2020-01-27 18:40:18 +00:00
|
|
|
this._listeners = [];
|
|
|
|
|
}
|
|
|
|
|
}
|