-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWindowMessageManager.ts
62 lines (57 loc) · 1.51 KB
/
WindowMessageManager.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
type Handler = (data: any) => void;
type StringToHandlers = Map<string, Handler[]>;
const nullHandlers : StringToHandlers = new Map();
const fromToHandlersMap : Map<Window | null, StringToHandlers> = new Map([[null, nullHandlers]]);
export function on(type: string, from: Window | null, fn: (data: any) => void) {
remove(type, from, fn);
let handlers = fromToHandlersMap.get(from);
if (!handlers) {
handlers = new Map();
fromToHandlersMap.set(from, handlers)
}
let fns = handlers.get(type);
if (!fns) {
fns = [];
handlers.set(type, fns);
}
fns.push(fn);
}
export function remove(type: string, from: Window | null, fn: (data: any) => void) {
let handlers = fromToHandlersMap.get(from);
if (!handlers) {
return;
}
const fns = handlers.get(type);
if (fns) {
const ndx = fns.indexOf(fn);
if (ndx >= 0) {
fns.splice(ndx, 1);
}
if (fns.length === 0) {
handlers.delete(type);
if (handlers.size === 0 && from) {
fromToHandlersMap.delete(from);
}
}
}
}
function callHandler(handlers: StringToHandlers, type: string, data: any) {
const fns = handlers.get(type);
if (fns) {
for (const fn of fns) {
fn(data);
}
return true;
}
return false;
}
window.addEventListener('message', (e: MessageEvent) => {
const {type, data} = e.data;
const handlers = fromToHandlersMap.get(e.source as Window);
if (handlers) {
if (callHandler(handlers, type, data)) {
return;
}
}
callHandler(nullHandlers, type, data);
});