Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 49c1f1b

Browse files
authored
Merge pull request #1891 from turt2live/travis/widget-postmessage-patches
Send required properties when making requests to widgets over postMessage
2 parents 21ec34d + 98da8b3 commit 49c1f1b

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/FromWidgetPostMessageApi.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ export default class FromWidgetPostMessageApi {
116116
return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise
117117
}
118118

119+
// Although the requestId is required, we don't use it. We'll be nice and process the message
120+
// if the property is missing, but with a warning for widget developers.
121+
if (!event.data.requestId) {
122+
console.warn("fromWidget action '" + event.data.action + "' does not have a requestId");
123+
}
124+
119125
const action = event.data.action;
120126
const widgetId = event.data.widgetId;
121127
if (action === 'content_loaded') {

src/ToWidgetPostMessageApi.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ export default class ToWidgetPostMessageApi {
5151
if (payload.response === undefined) {
5252
return;
5353
}
54-
const promise = this._requestMap[payload._id];
54+
const promise = this._requestMap[payload.requestId];
5555
if (!promise) {
5656
return;
5757
}
58-
delete this._requestMap[payload._id];
58+
delete this._requestMap[payload.requestId];
5959
promise.resolve(payload);
6060
}
6161

@@ -64,21 +64,21 @@ export default class ToWidgetPostMessageApi {
6464
targetWindow = targetWindow || window.parent; // default to parent window
6565
targetOrigin = targetOrigin || "*";
6666
this._counter += 1;
67-
action._id = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter;
67+
action.requestId = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter;
6868

6969
return new Promise((resolve, reject) => {
70-
this._requestMap[action._id] = {resolve, reject};
70+
this._requestMap[action.requestId] = {resolve, reject};
7171
targetWindow.postMessage(action, targetOrigin);
7272

7373
if (this._timeoutMs > 0) {
7474
setTimeout(() => {
75-
if (!this._requestMap[action._id]) {
75+
if (!this._requestMap[action.requestId]) {
7676
return;
7777
}
7878
console.error("postMessage request timed out. Sent object: " + JSON.stringify(action),
7979
this._requestMap);
80-
this._requestMap[action._id].reject(new Error("Timed out"));
81-
delete this._requestMap[action._id];
80+
this._requestMap[action.requestId].reject(new Error("Timed out"));
81+
delete this._requestMap[action.requestId];
8282
}, this._timeoutMs);
8383
}
8484
});

src/WidgetMessaging.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export default class WidgetMessaging {
4444
}
4545

4646
messageToWidget(action) {
47+
action.widgetId = this.widgetId; // Required to be sent for all outbound requests
48+
4749
return this.toWidget.exec(action, this.target).then((data) => {
4850
// Check for errors and reject if found
4951
if (data.response === undefined) { // null is valid

0 commit comments

Comments
 (0)