mirror of
https://github.com/element-hq/element-web
synced 2024-11-28 04:21:57 +03:00
Merge pull request #1891 from turt2live/travis/widget-postmessage-patches
Send required properties when making requests to widgets over postMessage
This commit is contained in:
commit
49c1f1b41f
3 changed files with 15 additions and 7 deletions
|
@ -116,6 +116,12 @@ export default class FromWidgetPostMessageApi {
|
||||||
return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise
|
return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Although the requestId is required, we don't use it. We'll be nice and process the message
|
||||||
|
// if the property is missing, but with a warning for widget developers.
|
||||||
|
if (!event.data.requestId) {
|
||||||
|
console.warn("fromWidget action '" + event.data.action + "' does not have a requestId");
|
||||||
|
}
|
||||||
|
|
||||||
const action = event.data.action;
|
const action = event.data.action;
|
||||||
const widgetId = event.data.widgetId;
|
const widgetId = event.data.widgetId;
|
||||||
if (action === 'content_loaded') {
|
if (action === 'content_loaded') {
|
||||||
|
|
|
@ -51,11 +51,11 @@ export default class ToWidgetPostMessageApi {
|
||||||
if (payload.response === undefined) {
|
if (payload.response === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const promise = this._requestMap[payload._id];
|
const promise = this._requestMap[payload.requestId];
|
||||||
if (!promise) {
|
if (!promise) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
delete this._requestMap[payload._id];
|
delete this._requestMap[payload.requestId];
|
||||||
promise.resolve(payload);
|
promise.resolve(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,21 +64,21 @@ export default class ToWidgetPostMessageApi {
|
||||||
targetWindow = targetWindow || window.parent; // default to parent window
|
targetWindow = targetWindow || window.parent; // default to parent window
|
||||||
targetOrigin = targetOrigin || "*";
|
targetOrigin = targetOrigin || "*";
|
||||||
this._counter += 1;
|
this._counter += 1;
|
||||||
action._id = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter;
|
action.requestId = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._requestMap[action._id] = {resolve, reject};
|
this._requestMap[action.requestId] = {resolve, reject};
|
||||||
targetWindow.postMessage(action, targetOrigin);
|
targetWindow.postMessage(action, targetOrigin);
|
||||||
|
|
||||||
if (this._timeoutMs > 0) {
|
if (this._timeoutMs > 0) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!this._requestMap[action._id]) {
|
if (!this._requestMap[action.requestId]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.error("postMessage request timed out. Sent object: " + JSON.stringify(action),
|
console.error("postMessage request timed out. Sent object: " + JSON.stringify(action),
|
||||||
this._requestMap);
|
this._requestMap);
|
||||||
this._requestMap[action._id].reject(new Error("Timed out"));
|
this._requestMap[action.requestId].reject(new Error("Timed out"));
|
||||||
delete this._requestMap[action._id];
|
delete this._requestMap[action.requestId];
|
||||||
}, this._timeoutMs);
|
}, this._timeoutMs);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -44,6 +44,8 @@ export default class WidgetMessaging {
|
||||||
}
|
}
|
||||||
|
|
||||||
messageToWidget(action) {
|
messageToWidget(action) {
|
||||||
|
action.widgetId = this.widgetId; // Required to be sent for all outbound requests
|
||||||
|
|
||||||
return this.toWidget.exec(action, this.target).then((data) => {
|
return this.toWidget.exec(action, this.target).then((data) => {
|
||||||
// Check for errors and reject if found
|
// Check for errors and reject if found
|
||||||
if (data.response === undefined) { // null is valid
|
if (data.response === undefined) { // null is valid
|
||||||
|
|
Loading…
Reference in a new issue