-
Notifications
You must be signed in to change notification settings - Fork 584
/
iframeDriver.ts
186 lines (154 loc) · 5.65 KB
/
iframeDriver.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/// <reference path="../localtypings/pxteditor.d.ts" />
import { IframeClientMessage } from "./iframeEmbeddedClient";
type MessageHandler = (response: any) => void;
const MessageReceivedEvent = "message";
const MessageSentEvent = "sent";
interface PendingMessage {
original: pxt.editor.EditorMessageRequest;
resolve: MessageHandler;
reject: MessageHandler;
}
/**
* Abstract class for driving communication with an embedded iframe
*/
export abstract class IframeDriver {
protected readyForMessages = false;
protected messageQueue: pxt.editor.EditorMessageRequest[] = [];
protected nextId = 0;
protected pendingMessages: { [index: string]: PendingMessage } = {};
protected editorEventListeners: { [index: string]: MessageHandler[] } = {};
protected port: MessagePort;
protected portRequestPending: boolean;
protected frameId: string;
constructor(public iframe: HTMLIFrameElement) {
const queryParams = new URLSearchParams(new URL(iframe.src).search);
if (queryParams.has("frameid")) {
this.frameId = queryParams.get("frameid")!;
}
window.addEventListener("message", this.onMessageReceived);
// In case this driver was created after the iframe was loaded and we missed
// the ready event, send a ready request for it to parrot back
if (iframe.contentWindow) {
iframe.contentWindow.postMessage({
type: "iframeclientready"
} as IframeClientMessage, "*");
}
}
protected abstract handleMessage(message: MessageEvent): void;
dispose() {
window.removeEventListener("message", this.onMessageReceived);
if (this.port) {
this.port.close();
}
}
addEventListener(event: string, handler: (ev: any) => void): void {
if (!this.editorEventListeners[event]) this.editorEventListeners[event] = [];
this.editorEventListeners[event].push(handler);
}
removeEventListener(event: string, handler: (ev: any) => void) {
if (this.editorEventListeners[event]) {
const filtered = this.editorEventListeners[event].filter(h => h !== handler);
if (filtered.length === 0) {
delete this.editorEventListeners[event];
}
else {
this.editorEventListeners[event] = filtered;
}
}
}
protected onMessageReceived = (event: MessageEvent) => {
const data = event.data;
if (data) {
if (this.frameId && data.frameId !== this.frameId) {
return;
}
if (data.type === "iframeclientready") {
if (this.frameId && !this.port) {
this.createMessagePort();
}
else {
this.readyForMessages = true;
this.sendMessageCore();
}
return;
}
else if (data.type === "iframeclientsetmessageport") {
return;
}
}
this.handleMessage(event);
}
protected resolvePendingMessage(event: MessageEvent) {
const data = event.data as pxt.editor.EditorMessageResponse;
if (data.id && this.pendingMessages[data.id]) {
const resp = event.data as pxt.editor.EditorMessageResponse;
const pending = this.pendingMessages[resp.id!];
delete this.pendingMessages[resp.id!];
if (resp.success) {
pending.resolve(resp);
}
else {
pending.reject(resp.error || new Error("Unknown error: iFrame returned failure"));
}
}
}
protected fireEvent(event: string, data: any) {
const listeners = this.editorEventListeners[event];
if (!listeners) return;
for (const handler of listeners) {
try {
handler(data);
}
catch (e) {
pxt.error(e);
}
}
}
protected sendRequest(message: any) {
return new Promise((resolve, reject) => {
message.response = true;
message.id = this.nextId++ + "";
this.pendingMessages[message.id] = {
original: message,
resolve,
reject
};
this.sendMessageCore(message);
});
}
protected sendMessageCore(message?: any) {
if (message) {
this.messageQueue.push(message);
}
if (this.readyForMessages) {
while (this.messageQueue.length) {
const toSend = this.messageQueue.shift();
if (this.port) {
this.port.postMessage(toSend);
}
else {
this.iframe.contentWindow?.postMessage(toSend, "*");
}
this.fireEvent(MessageSentEvent, toSend);
}
}
}
protected createMessagePort() {
if (this.port || this.portRequestPending) return;
this.portRequestPending = true
const channel = new MessageChannel();
channel.port1.onmessage = (message: MessageEvent) => {
if (!this.port) {
this.port = channel.port1;
this.frameId = undefined;
window.removeEventListener("message", this.onMessageReceived);
this.readyForMessages = true;
this.sendMessageCore();
}
this.onMessageReceived(message);
};
this.iframe.contentWindow!.postMessage({
type: "iframeclientsetmessageport",
} as IframeClientMessage, "*", [channel.port2]);
}
}