forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiframe-message-bus.ts
87 lines (78 loc) · 2.73 KB
/
iframe-message-bus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @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
*/
import {Events, MessageBus, Parameters} from 'protocol';
type AnyEventCallback<Ev> = <E extends keyof Ev>(topic: E, args: Parameters<Ev[E]>) => void;
export class IFrameMessageBus extends MessageBus<Events> {
private _listeners: any[] = [];
constructor(
private _source: string, private _destination: string, private _docWindow: () => Window) {
super();
}
onAny(cb: AnyEventCallback<Events>): () => void {
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);
};
}
on<E extends keyof Events>(topic: E, cb: Events[E]): () => void {
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);
};
}
once<E extends keyof Events>(topic: E, cb: Events[E]): void {
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.removeEventListener('message', listener);
}
};
window.addEventListener('message', listener);
}
emit<E extends keyof Events>(topic: E, args?: Parameters<Events[E]>): boolean {
this._docWindow().postMessage(
{
source: this._source,
topic,
args,
// 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.
__ignore_ng_zone__: this._source === 'angular-devtools',
},
'*');
return true;
}
destroy(): void {
this._listeners.forEach((l) => window.removeEventListener('message', l));
this._listeners = [];
}
}