@elliemae/microfe-common 2.0.0-next.41 → 2.0.0-next.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/remoting.js +279 -317
- package/dist/cjs/scriptingObject.js +47 -73
- package/dist/cjs/scriptingObjectManager.js +173 -201
- package/dist/esm/remoting.js +279 -318
- package/dist/esm/scriptingObject.js +47 -74
- package/dist/esm/scriptingObjectManager.js +173 -202
- package/package.json +4 -4
package/dist/cjs/remoting.js
CHANGED
|
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
6
|
var __export = (target, all) => {
|
|
8
7
|
for (var name in all)
|
|
9
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -17,28 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
16
|
return to;
|
|
18
17
|
};
|
|
19
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
var __publicField = (obj, key, value) => {
|
|
21
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
-
return value;
|
|
23
|
-
};
|
|
24
|
-
var __accessCheck = (obj, member, msg) => {
|
|
25
|
-
if (!member.has(obj))
|
|
26
|
-
throw TypeError("Cannot " + msg);
|
|
27
|
-
};
|
|
28
|
-
var __privateGet = (obj, member, getter) => {
|
|
29
|
-
__accessCheck(obj, member, "read from private field");
|
|
30
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
31
|
-
};
|
|
32
|
-
var __privateAdd = (obj, member, value) => {
|
|
33
|
-
if (member.has(obj))
|
|
34
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
35
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
36
|
-
};
|
|
37
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
38
|
-
__accessCheck(obj, member, "write to private field");
|
|
39
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
40
|
-
return value;
|
|
41
|
-
};
|
|
42
19
|
var remoting_exports = {};
|
|
43
20
|
__export(remoting_exports, {
|
|
44
21
|
Remoting: () => Remoting,
|
|
@@ -46,7 +23,6 @@ __export(remoting_exports, {
|
|
|
46
23
|
});
|
|
47
24
|
module.exports = __toCommonJS(remoting_exports);
|
|
48
25
|
var import_uuid = require("uuid");
|
|
49
|
-
var _correlationId, _logger, _listeners, _invocations, _timeoutMonitorHandle, _allowedSenders, _evaluateTimeouts, _startResponseMonitor, _stopResponseMonitor, _popInvocation, _processResponse, _processException, _receive, _processMessage;
|
|
50
26
|
const MESSAGE_SOURCE = "elli:remoting";
|
|
51
27
|
const RESPONSE_MESSAGE_TYPE = "elli:remoting:response";
|
|
52
28
|
const EXCEPTION_MESSAGE_TYPE = "elli:remoting:exception";
|
|
@@ -67,6 +43,24 @@ const sendMessage = (param) => {
|
|
|
67
43
|
targetWin.postMessage(msg, targetOrigin);
|
|
68
44
|
};
|
|
69
45
|
class Remoting {
|
|
46
|
+
#correlationId;
|
|
47
|
+
#logger;
|
|
48
|
+
/**
|
|
49
|
+
* set of listeners that are registered
|
|
50
|
+
*/
|
|
51
|
+
#listeners = /* @__PURE__ */ new Map();
|
|
52
|
+
/**
|
|
53
|
+
* Represents the set of invocations that are waiting for a response
|
|
54
|
+
*/
|
|
55
|
+
#invocations = /* @__PURE__ */ new Map();
|
|
56
|
+
/**
|
|
57
|
+
* The handle to the timeout monitor
|
|
58
|
+
*/
|
|
59
|
+
#timeoutMonitorHandle = null;
|
|
60
|
+
/**
|
|
61
|
+
* The set of windows that are allowed to send messages to this window
|
|
62
|
+
*/
|
|
63
|
+
#allowedSenders = /* @__PURE__ */ new Map();
|
|
70
64
|
/**
|
|
71
65
|
* Create a new instance of the Remoting class
|
|
72
66
|
*
|
|
@@ -74,313 +68,281 @@ class Remoting {
|
|
|
74
68
|
* @param correlationId unique id for the current session
|
|
75
69
|
*/
|
|
76
70
|
constructor(logger, correlationId) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
__privateAdd(this, _evaluateTimeouts, () => {
|
|
97
|
-
const ts = Date.now();
|
|
98
|
-
const canceledItems = [];
|
|
99
|
-
__privateGet(this, _invocations).forEach((eventData, key) => {
|
|
100
|
-
const { requestId, cancelTime } = eventData;
|
|
101
|
-
__privateGet(this, _logger).debug(
|
|
102
|
-
`Checking response timeout for requestId: ${requestId}) @ ${cancelTime ?? ""}`
|
|
71
|
+
if (!logger)
|
|
72
|
+
throw new Error("logger is required");
|
|
73
|
+
if (!correlationId)
|
|
74
|
+
throw new Error("correlationId is required");
|
|
75
|
+
this.#correlationId = correlationId;
|
|
76
|
+
this.#logger = logger;
|
|
77
|
+
}
|
|
78
|
+
// Evaluates the timeouts on any waiting invocations
|
|
79
|
+
#evaluateTimeouts = () => {
|
|
80
|
+
const ts = Date.now();
|
|
81
|
+
const canceledItems = [];
|
|
82
|
+
this.#invocations.forEach((eventData, key) => {
|
|
83
|
+
const { requestId, cancelTime } = eventData;
|
|
84
|
+
this.#logger.debug(
|
|
85
|
+
`Checking response timeout for requestId: ${requestId}) @ ${cancelTime ?? ""}`
|
|
86
|
+
);
|
|
87
|
+
if (eventData.cancelTime && eventData.cancelTime < ts) {
|
|
88
|
+
this.#logger.debug(
|
|
89
|
+
`Detected response timeout for requestId: ${requestId}...`
|
|
103
90
|
);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)
|
|
108
|
-
canceledItems.push(key);
|
|
109
|
-
eventData.resolve();
|
|
110
|
-
__privateGet(this, _logger).debug(
|
|
111
|
-
`Aborted waiting for response to requestid: ${requestId})`
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
canceledItems.forEach((key) => {
|
|
116
|
-
__privateGet(this, _logger).debug(
|
|
117
|
-
`removing invocations with requestId ${key} from cache since response time has expired`
|
|
91
|
+
canceledItems.push(key);
|
|
92
|
+
eventData.resolve();
|
|
93
|
+
this.#logger.debug(
|
|
94
|
+
`Aborted waiting for response to requestid: ${requestId})`
|
|
118
95
|
);
|
|
119
|
-
__privateGet(this, _invocations).delete(key);
|
|
120
|
-
});
|
|
121
|
-
if (__privateGet(this, _invocations).size === 0) {
|
|
122
|
-
__privateGet(this, _logger).debug(`stopping response monitor`);
|
|
123
|
-
__privateGet(this, _stopResponseMonitor).call(this);
|
|
124
96
|
}
|
|
125
97
|
});
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
__privateGet(this, _logger).debug("Staring response timeout evaluator");
|
|
130
|
-
__privateSet(this, _timeoutMonitorHandle, window.setInterval(
|
|
131
|
-
__privateGet(this, _evaluateTimeouts),
|
|
132
|
-
200
|
|
133
|
-
));
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
// Stops the timeout monitor interval
|
|
137
|
-
__privateAdd(this, _stopResponseMonitor, () => {
|
|
138
|
-
if (__privateGet(this, _timeoutMonitorHandle) !== null) {
|
|
139
|
-
window.clearInterval(__privateGet(this, _timeoutMonitorHandle));
|
|
140
|
-
__privateSet(this, _timeoutMonitorHandle, null);
|
|
141
|
-
__privateGet(this, _logger).debug("Stopped response timeout evaluator");
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
// Pops an invocation from the incovation list
|
|
145
|
-
__privateAdd(this, _popInvocation, (requestId) => {
|
|
146
|
-
const e = __privateGet(this, _invocations).get(requestId);
|
|
147
|
-
__privateGet(this, _logger).debug(`serving requestId: ${requestId}`);
|
|
148
|
-
__privateGet(this, _invocations).delete(requestId);
|
|
149
|
-
return e;
|
|
150
|
-
});
|
|
151
|
-
// Handles a response to a prior cross-frame invocation
|
|
152
|
-
__privateAdd(this, _processResponse, (message) => {
|
|
153
|
-
const { requestId } = message;
|
|
154
|
-
__privateGet(this, _logger).debug(
|
|
155
|
-
`Response received for invocation requestId: ${requestId}`
|
|
98
|
+
canceledItems.forEach((key) => {
|
|
99
|
+
this.#logger.debug(
|
|
100
|
+
`removing invocations with requestId ${key} from cache since response time has expired`
|
|
156
101
|
);
|
|
157
|
-
|
|
158
|
-
if (!eventData) {
|
|
159
|
-
__privateGet(this, _logger).warn(
|
|
160
|
-
`Received response to stale/invalid request with requestId: ${requestId}`
|
|
161
|
-
);
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
eventData.resolve(message.body);
|
|
165
|
-
return true;
|
|
102
|
+
this.#invocations.delete(key);
|
|
166
103
|
});
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
104
|
+
if (this.#invocations.size === 0) {
|
|
105
|
+
this.#logger.debug(`stopping response monitor`);
|
|
106
|
+
this.#stopResponseMonitor();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
// Set a timer interval to catch any invocations that didn't respond in a timely manner
|
|
110
|
+
#startResponseMonitor = () => {
|
|
111
|
+
if (this.#timeoutMonitorHandle === null) {
|
|
112
|
+
this.#logger.debug("Staring response timeout evaluator");
|
|
113
|
+
this.#timeoutMonitorHandle = window.setInterval(
|
|
114
|
+
this.#evaluateTimeouts,
|
|
115
|
+
200
|
|
171
116
|
);
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
body: message.body
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
return true;
|
|
203
|
-
});
|
|
204
|
-
// Processes a message received thru the window's message event
|
|
205
|
-
__privateAdd(this, _processMessage, (message) => {
|
|
206
|
-
__privateGet(this, _logger).debug(
|
|
207
|
-
`Remoting: Received message ${JSON.stringify(message.data)}`
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
// Stops the timeout monitor interval
|
|
120
|
+
#stopResponseMonitor = () => {
|
|
121
|
+
if (this.#timeoutMonitorHandle !== null) {
|
|
122
|
+
window.clearInterval(this.#timeoutMonitorHandle);
|
|
123
|
+
this.#timeoutMonitorHandle = null;
|
|
124
|
+
this.#logger.debug("Stopped response timeout evaluator");
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
// Pops an invocation from the incovation list
|
|
128
|
+
#popInvocation = (requestId) => {
|
|
129
|
+
const e = this.#invocations.get(requestId);
|
|
130
|
+
this.#logger.debug(`serving requestId: ${requestId}`);
|
|
131
|
+
this.#invocations.delete(requestId);
|
|
132
|
+
return e;
|
|
133
|
+
};
|
|
134
|
+
// Handles a response to a prior cross-frame invocation
|
|
135
|
+
#processResponse = (message) => {
|
|
136
|
+
const { requestId } = message;
|
|
137
|
+
this.#logger.debug(
|
|
138
|
+
`Response received for invocation requestId: ${requestId}`
|
|
139
|
+
);
|
|
140
|
+
const eventData = this.#popInvocation(requestId);
|
|
141
|
+
if (!eventData) {
|
|
142
|
+
this.#logger.warn(
|
|
143
|
+
`Received response to stale/invalid request with requestId: ${requestId}`
|
|
208
144
|
);
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
*/
|
|
248
|
-
__publicField(this, "initialize", (win) => {
|
|
249
|
-
win.removeEventListener("message", __privateGet(this, _processMessage));
|
|
250
|
-
win.addEventListener("message", __privateGet(this, _processMessage));
|
|
251
|
-
__privateGet(this, _logger).debug(`initialized remoting id: ${__privateGet(this, _correlationId)}`);
|
|
252
|
-
});
|
|
253
|
-
/**
|
|
254
|
-
* Closes the remoting service for a window
|
|
255
|
-
*/
|
|
256
|
-
__publicField(this, "close", () => {
|
|
257
|
-
window.removeEventListener("message", __privateGet(this, _processMessage));
|
|
258
|
-
__privateGet(this, _logger).debug(`closed remoting id: ${__privateGet(this, _correlationId)}`);
|
|
259
|
-
});
|
|
260
|
-
/**
|
|
261
|
-
* Sends an invocation which generates a Promise to be used to get a response
|
|
262
|
-
*
|
|
263
|
-
* @param {InvokeParam} param The parameters for the invocation
|
|
264
|
-
* @returns promisifyed response
|
|
265
|
-
*/
|
|
266
|
-
__publicField(this, "invoke", (param) => {
|
|
267
|
-
const {
|
|
268
|
-
targetWin,
|
|
269
|
-
targetOrigin,
|
|
270
|
-
messageType,
|
|
271
|
-
messageBody,
|
|
272
|
-
responseTimeoutMs
|
|
273
|
-
} = param;
|
|
274
|
-
return new Promise((resolve, reject) => {
|
|
275
|
-
const msg = createMessage({ messageType, messageBody });
|
|
276
|
-
__privateGet(this, _invocations).set(msg.requestId, {
|
|
277
|
-
requestId: msg.requestId,
|
|
278
|
-
resolve,
|
|
279
|
-
reject,
|
|
280
|
-
cancelTime: responseTimeoutMs ? Date.now() + Number.parseInt(responseTimeoutMs, 10) : null
|
|
281
|
-
});
|
|
282
|
-
targetWin.postMessage(msg, targetOrigin);
|
|
283
|
-
const { requestId } = msg;
|
|
284
|
-
__privateGet(this, _logger).debug(
|
|
285
|
-
`Posted invocation message of type ${messageType} requestId: ${requestId || ""}`
|
|
286
|
-
);
|
|
287
|
-
if (responseTimeoutMs) {
|
|
288
|
-
__privateGet(this, _logger).debug(
|
|
289
|
-
`starting response monitor for requestId: ${requestId || ""} for ${responseTimeoutMs} ms`
|
|
290
|
-
);
|
|
291
|
-
__privateGet(this, _startResponseMonitor).call(this);
|
|
292
|
-
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
eventData.resolve(message.body);
|
|
148
|
+
return true;
|
|
149
|
+
};
|
|
150
|
+
// Handles a response to a prior cross-frame invocation
|
|
151
|
+
#processException = (message) => {
|
|
152
|
+
this.#logger.debug(
|
|
153
|
+
`Exception received for invocation (requestId = ${message.requestId})`
|
|
154
|
+
);
|
|
155
|
+
const eventData = this.#popInvocation(message.requestId);
|
|
156
|
+
if (!eventData) {
|
|
157
|
+
this.#logger.warn(
|
|
158
|
+
`Received exception for stale/invalid request (requestId = ${message.requestId})`
|
|
159
|
+
);
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
eventData.reject(new Error(message.body));
|
|
163
|
+
return true;
|
|
164
|
+
};
|
|
165
|
+
// Receives a message from another window and invokes any event handlers
|
|
166
|
+
#receive = ({
|
|
167
|
+
sourceWin,
|
|
168
|
+
sourceOrigin,
|
|
169
|
+
message
|
|
170
|
+
}) => {
|
|
171
|
+
this.#logger.debug(`Received message of type "${message.type}"`);
|
|
172
|
+
const callbacks = this.#listeners.get(message.type);
|
|
173
|
+
if (!callbacks)
|
|
174
|
+
return false;
|
|
175
|
+
callbacks.forEach((callback) => {
|
|
176
|
+
this.#logger.debug(`Invoking message handler ${callback.name}`);
|
|
177
|
+
callback({
|
|
178
|
+
sourceWin,
|
|
179
|
+
sourceOrigin,
|
|
180
|
+
requestId: message.requestId,
|
|
181
|
+
type: message.type,
|
|
182
|
+
body: message.body
|
|
293
183
|
});
|
|
294
184
|
});
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
185
|
+
return true;
|
|
186
|
+
};
|
|
187
|
+
// Processes a message received thru the window's message event
|
|
188
|
+
#processMessage = (message) => {
|
|
189
|
+
this.#logger.debug(
|
|
190
|
+
`Remoting: Received message ${JSON.stringify(message.data)}`
|
|
191
|
+
);
|
|
192
|
+
if (this.#allowedSenders.size === 0)
|
|
193
|
+
return false;
|
|
194
|
+
if (!message.source)
|
|
195
|
+
return false;
|
|
196
|
+
const senderOrigin = this.#allowedSenders.get(message.source);
|
|
197
|
+
if (!senderOrigin)
|
|
198
|
+
return false;
|
|
199
|
+
if (message?.data?.source !== MESSAGE_SOURCE)
|
|
200
|
+
return false;
|
|
201
|
+
if (message.data.type === RESPONSE_MESSAGE_TYPE)
|
|
202
|
+
this.#processResponse(message.data);
|
|
203
|
+
else if (message.data.type === EXCEPTION_MESSAGE_TYPE)
|
|
204
|
+
this.#processException(message.data);
|
|
205
|
+
else
|
|
206
|
+
this.#receive({
|
|
207
|
+
sourceWin: message.source,
|
|
208
|
+
sourceOrigin: senderOrigin,
|
|
209
|
+
message: message.data
|
|
317
210
|
});
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
211
|
+
return true;
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Adds window and its origin list of allowed senders
|
|
215
|
+
*
|
|
216
|
+
* @param {AddSenderParam} param - The sender to add
|
|
217
|
+
*/
|
|
218
|
+
addSender = (param) => {
|
|
219
|
+
const { origin, window: window2 } = param;
|
|
220
|
+
if (!origin)
|
|
221
|
+
throw new Error("origin is required");
|
|
222
|
+
if (!window2)
|
|
223
|
+
throw new Error("window is required");
|
|
224
|
+
this.#allowedSenders.set(window2, origin);
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Initializes the remoting service for a window
|
|
228
|
+
*
|
|
229
|
+
* @param win The window to initialize remoting for
|
|
230
|
+
*/
|
|
231
|
+
initialize = (win) => {
|
|
232
|
+
win.removeEventListener("message", this.#processMessage);
|
|
233
|
+
win.addEventListener("message", this.#processMessage);
|
|
234
|
+
this.#logger.debug(`initialized remoting id: ${this.#correlationId}`);
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Closes the remoting service for a window
|
|
238
|
+
*/
|
|
239
|
+
close = () => {
|
|
240
|
+
window.removeEventListener("message", this.#processMessage);
|
|
241
|
+
this.#logger.debug(`closed remoting id: ${this.#correlationId}`);
|
|
242
|
+
};
|
|
243
|
+
/**
|
|
244
|
+
* Sends an invocation which generates a Promise to be used to get a response
|
|
245
|
+
*
|
|
246
|
+
* @param {InvokeParam} param The parameters for the invocation
|
|
247
|
+
* @returns promisifyed response
|
|
248
|
+
*/
|
|
249
|
+
invoke = (param) => {
|
|
250
|
+
const {
|
|
251
|
+
targetWin,
|
|
252
|
+
targetOrigin,
|
|
253
|
+
messageType,
|
|
254
|
+
messageBody,
|
|
255
|
+
responseTimeoutMs
|
|
256
|
+
} = param;
|
|
257
|
+
return new Promise((resolve, reject) => {
|
|
258
|
+
const msg = createMessage({ messageType, messageBody });
|
|
259
|
+
this.#invocations.set(msg.requestId, {
|
|
260
|
+
requestId: msg.requestId,
|
|
261
|
+
resolve,
|
|
262
|
+
reject,
|
|
263
|
+
cancelTime: responseTimeoutMs ? Date.now() + Number.parseInt(responseTimeoutMs, 10) : null
|
|
341
264
|
});
|
|
342
|
-
msg.requestId = requestId;
|
|
343
265
|
targetWin.postMessage(msg, targetOrigin);
|
|
344
|
-
|
|
345
|
-
|
|
266
|
+
const { requestId } = msg;
|
|
267
|
+
this.#logger.debug(
|
|
268
|
+
`Posted invocation message of type ${messageType} requestId: ${requestId || ""}`
|
|
346
269
|
);
|
|
270
|
+
if (responseTimeoutMs) {
|
|
271
|
+
this.#logger.debug(
|
|
272
|
+
`starting response monitor for requestId: ${requestId || ""} for ${responseTimeoutMs} ms`
|
|
273
|
+
);
|
|
274
|
+
this.#startResponseMonitor();
|
|
275
|
+
}
|
|
347
276
|
});
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
277
|
+
};
|
|
278
|
+
/**
|
|
279
|
+
* Setup callback for a specific message type
|
|
280
|
+
*
|
|
281
|
+
* @param {ListenParam<T>} param The parameters for the listener
|
|
282
|
+
*/
|
|
283
|
+
listen = (param) => {
|
|
284
|
+
const { messageType, callback } = param;
|
|
285
|
+
const items = this.#listeners.get(messageType) || [];
|
|
286
|
+
items.push(callback);
|
|
287
|
+
this.#listeners.set(messageType, items);
|
|
288
|
+
};
|
|
289
|
+
/**
|
|
290
|
+
* Send a message without any form of response. Fire and forget
|
|
291
|
+
*
|
|
292
|
+
* @param {SendParam} param The parameters for the send
|
|
293
|
+
*/
|
|
294
|
+
send = (param) => {
|
|
295
|
+
const { targetWin, targetOrigin, messageType, messageBody } = param;
|
|
296
|
+
const msg = createMessage({
|
|
297
|
+
messageType,
|
|
298
|
+
messageBody,
|
|
299
|
+
onewayMsg: true
|
|
364
300
|
});
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
301
|
+
targetWin.postMessage(msg, targetOrigin);
|
|
302
|
+
this.#logger.debug(`Posted one-way message of type "${messageType}"`);
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* Removes a window from the list of allowed senders
|
|
306
|
+
*
|
|
307
|
+
* @param {AddSenderParam} param - The sender to remove
|
|
308
|
+
*/
|
|
309
|
+
removeSender = (param) => {
|
|
310
|
+
const { window: window2 } = param;
|
|
311
|
+
if (window2)
|
|
312
|
+
this.#allowedSenders.delete(window2);
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* Send a response message to a window
|
|
316
|
+
*
|
|
317
|
+
* @param {RespondParam} param The parameters for the response
|
|
318
|
+
*/
|
|
319
|
+
respond = (param) => {
|
|
320
|
+
const { targetWin, targetOrigin, requestId, response } = param;
|
|
321
|
+
const msg = createMessage({
|
|
322
|
+
messageType: RESPONSE_MESSAGE_TYPE,
|
|
323
|
+
messageBody: response
|
|
324
|
+
});
|
|
325
|
+
msg.requestId = requestId;
|
|
326
|
+
targetWin.postMessage(msg, targetOrigin);
|
|
327
|
+
this.#logger.debug(
|
|
328
|
+
`Response sent to caller for invocation requestId: ${requestId}`
|
|
329
|
+
);
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* Send an exception message to a window
|
|
333
|
+
*
|
|
334
|
+
* @param {RaiseExceptionParam} param The parameters for the exception
|
|
335
|
+
*/
|
|
336
|
+
raiseException = (param) => {
|
|
337
|
+
const { targetWin, targetOrigin, requestId, ex } = param;
|
|
338
|
+
const msg = createMessage({
|
|
339
|
+
messageType: EXCEPTION_MESSAGE_TYPE,
|
|
340
|
+
messageBody: ex
|
|
341
|
+
});
|
|
342
|
+
msg.requestId = requestId;
|
|
343
|
+
targetWin.postMessage(msg, targetOrigin);
|
|
344
|
+
this.#logger.debug(
|
|
345
|
+
`Exception sent to caller for invocation. requestId: ${requestId}`
|
|
346
|
+
);
|
|
347
|
+
};
|
|
372
348
|
}
|
|
373
|
-
_correlationId = new WeakMap();
|
|
374
|
-
_logger = new WeakMap();
|
|
375
|
-
_listeners = new WeakMap();
|
|
376
|
-
_invocations = new WeakMap();
|
|
377
|
-
_timeoutMonitorHandle = new WeakMap();
|
|
378
|
-
_allowedSenders = new WeakMap();
|
|
379
|
-
_evaluateTimeouts = new WeakMap();
|
|
380
|
-
_startResponseMonitor = new WeakMap();
|
|
381
|
-
_stopResponseMonitor = new WeakMap();
|
|
382
|
-
_popInvocation = new WeakMap();
|
|
383
|
-
_processResponse = new WeakMap();
|
|
384
|
-
_processException = new WeakMap();
|
|
385
|
-
_receive = new WeakMap();
|
|
386
|
-
_processMessage = new WeakMap();
|