-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive.ts
83 lines (74 loc) · 3.25 KB
/
receive.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
import { Payload, WindowClient } from './common';
import { GetOrigin } from './utils';
export interface Response {
send: (message: string) => void;
}
export class ResponseImpl implements Response {
messageId: string;
client: WindowClient;
//add constructor
constructor(messageId: string, client: WindowClient) {
this.messageId = messageId;
this.client = client;
}
send: (message: string) => void = (message: string) => {
const payload: Payload = {
id: this.messageId,
message: message,
fromOrigin: this.client.fromOrigin,
toOrigin: this.client.toOrigin,
nodeTypeTo: this.client.nodeTypeTo,
nodeTypeFrom: this.client.nodeTypeFrom,
subpath: this.client.subpath,
};
let fromType = this.client.nodeTypeFrom;
let toType = this.client.nodeTypeTo;
if (fromType == 'iframe' && toType == 'parent') {
let win: Window = this.client.node as Window;
win.postMessage(payload, this.client.toOrigin);
} else if (fromType == 'parent' && toType == 'iframe') {
let iframe: HTMLIFrameElement = this.client.node as HTMLIFrameElement;
iframe.contentWindow?.postMessage(payload, this.client.toOrigin);
} else if (fromType == 'window' && toType == 'parent') {
let win: Window = this.client.node as Window;
win.postMessage(payload, this.client.toOrigin);
} else if (fromType == 'parent' && toType == 'window') {
let win: Window = this.client.node as Window;
win.postMessage(payload, this.client.toOrigin);
}
};
}
export interface Receiver {
receive: (route: string, node: Window | HTMLIFrameElement, callback: (res: Response, message: string) => void) => void;
}
export class ReceiverImpl implements Receiver {
receive: (subpath: string, node: Window | HTMLIFrameElement, callback: (res: Response, message: string) => void) => void = (subpath: string, node: Window | HTMLIFrameElement, callback: (res: Response, message: string) => void) => {
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
window.addEventListener('message', (event) => {
if (event.origin !== event.data.fromOrigin) {
return;
}
if (event.data?.subpath !== subpath) {
return;
}
//if this message from parent to window check source also
if (event.data.nodeTypeFrom == 'parent' && event.data.nodeTypeTo == 'window') {
if (event.source != node) {
return;
}
}
const client: WindowClient = {
node: node,
nodeTypeTo: event.data.nodeTypeFrom,
nodeTypeFrom: event.data.nodeTypeTo,
toOrigin: event.data.fromOrigin,
fromOrigin: event.data.toOrigin,
subpath: subpath,
nodeReady: false,
};
const resp = new ResponseImpl(event.data.id, client);
callback(resp, event.data.message);
});
}
};
}