Skip to content

Commit

Permalink
Expose the requestId fully in the toWidget postMessage API
Browse files Browse the repository at this point in the history
This field is flagged as required in the proposal.

Addresses part of element-hq/element-web#6708

Signed-off-by: Travis Ralston
  • Loading branch information
turt2live committed May 12, 2018
1 parent f8fd90c commit 0522ab8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/ToWidgetPostMessageApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export default class ToWidgetPostMessageApi {
if (payload.response === undefined) {
return;
}
const promise = this._requestMap[payload._id];
const promise = this._requestMap[payload.requestId];
if (!promise) {
return;
}
delete this._requestMap[payload._id];
delete this._requestMap[payload.requestId];
promise.resolve(payload);
}

Expand All @@ -64,21 +64,21 @@ export default class ToWidgetPostMessageApi {
targetWindow = targetWindow || window.parent; // default to parent window
targetOrigin = targetOrigin || "*";
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) => {
this._requestMap[action._id] = {resolve, reject};
this._requestMap[action.requestId] = {resolve, reject};
targetWindow.postMessage(action, targetOrigin);

if (this._timeoutMs > 0) {
setTimeout(() => {
if (!this._requestMap[action._id]) {
if (!this._requestMap[action.requestId]) {
return;
}
console.error("postMessage request timed out. Sent object: " + JSON.stringify(action),
this._requestMap);
this._requestMap[action._id].reject(new Error("Timed out"));
delete this._requestMap[action._id];
this._requestMap[action.requestId].reject(new Error("Timed out"));
delete this._requestMap[action.requestId];
}, this._timeoutMs);
}
});
Expand Down

0 comments on commit 0522ab8

Please sign in to comment.