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
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2021-12-10 02:37:01 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-05-08 13:20:37 +00:00
|
|
|
import {Events, MessageBus, Parameters} from '../projects/protocol';
|
2020-01-27 18:40:18 +00:00
|
|
|
|
|
|
|
|
export class IFrameMessageBus extends MessageBus<Events> {
|
2025-07-24 08:08:48 +00:00
|
|
|
private listeners: any[] = [];
|
2020-01-27 18:40:18 +00:00
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
constructor(
|
2025-07-24 08:08:48 +00:00
|
|
|
private readonly source: string,
|
|
|
|
|
private readonly destination: string,
|
|
|
|
|
private readonly docWindow: () => Window,
|
2021-12-09 05:44:17 +00:00
|
|
|
) {
|
2020-01-27 18:40:18 +00:00
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 19:44:53 +00:00
|
|
|
override on<E extends keyof Events>(topic: E, cb: Events[E]): () => void {
|
2020-01-27 18:40:18 +00:00
|
|
|
const listener = (e: MessageEvent) => {
|
2025-07-24 08:08:48 +00:00
|
|
|
if (!e.data || e.data.source !== this.destination || !e.data.topic) {
|
2020-01-27 18:40:18 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (e.data.topic === topic) {
|
2023-12-04 19:44:53 +00:00
|
|
|
(cb as () => void).apply(null, e.data.args);
|
2020-01-27 18:40:18 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('message', listener);
|
2025-07-24 08:08:48 +00:00
|
|
|
this.listeners.push(listener);
|
2020-01-27 18:40:18 +00:00
|
|
|
return () => {
|
2025-07-24 08:08:48 +00:00
|
|
|
this.listeners.splice(this.listeners.indexOf(listener), 1);
|
2020-01-27 18:40:18 +00:00
|
|
|
window.removeEventListener('message', listener);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 19:44:53 +00:00
|
|
|
override once<E extends keyof Events>(topic: E, cb: Events[E]): void {
|
2020-01-27 18:40:18 +00:00
|
|
|
const listener = (e: MessageEvent) => {
|
2025-07-24 08:08:48 +00:00
|
|
|
if (!e.data || e.data.source !== this.destination || !e.data.topic) {
|
2020-01-27 18:40:18 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (e.data.topic === topic) {
|
2023-12-04 19:44:53 +00:00
|
|
|
(cb as any).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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 19:44:53 +00:00
|
|
|
override emit<E extends keyof Events>(topic: E, args?: Parameters<Events[E]>): boolean {
|
2025-07-24 08:08:48 +00:00
|
|
|
this.docWindow().postMessage(
|
2021-12-09 05:44:17 +00:00
|
|
|
{
|
2025-07-24 08:08:48 +00:00
|
|
|
source: this.source,
|
2021-12-09 05:44:17 +00:00
|
|
|
topic,
|
|
|
|
|
args,
|
2023-08-28 01:22:04 +00:00
|
|
|
// Since both the devtools app and the demo app use IframeMessageBus,
|
|
|
|
|
// we want to only ignore the ngZone for the demo app. This will let us
|
|
|
|
|
// prevent infinite change detection loops triggered by message
|
|
|
|
|
// event listeners but also not prevent the NgZone in the devtools app
|
|
|
|
|
// from updating its UI.
|
2025-07-24 08:08:48 +00:00
|
|
|
__ignore_ng_zone__: this.source === 'angular-devtools',
|
2025-07-18 23:25:57 +00:00
|
|
|
__NG_DEVTOOLS_EVENT__: true,
|
2021-12-09 05:44:17 +00:00
|
|
|
},
|
|
|
|
|
'*',
|
|
|
|
|
);
|
2020-03-23 23:35:02 +00:00
|
|
|
return true;
|
2020-01-27 18:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-04 19:44:53 +00:00
|
|
|
override destroy(): void {
|
2025-07-24 08:08:48 +00:00
|
|
|
this.listeners.forEach((l) => window.removeEventListener('message', l));
|
|
|
|
|
this.listeners = [];
|
2020-01-27 18:40:18 +00:00
|
|
|
}
|
|
|
|
|
}
|