mirror of https://github.com/vector-im/riot-web
Simplify request mapping.
parent
7e062090de
commit
46f46ee294
|
@ -16,36 +16,6 @@ limitations under the License.
|
||||||
|
|
||||||
import Promise from "bluebird";
|
import Promise from "bluebird";
|
||||||
|
|
||||||
function defer() {
|
|
||||||
let resolve;
|
|
||||||
let reject;
|
|
||||||
let isPending = true;
|
|
||||||
const promise = new Promise(function(...args) {
|
|
||||||
resolve = args[0];
|
|
||||||
reject = args[1];
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
resolve: function(...args) {
|
|
||||||
if (!isPending) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
isPending = false;
|
|
||||||
resolve(args[0]);
|
|
||||||
},
|
|
||||||
reject: function(...args) {
|
|
||||||
if (!isPending) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
isPending = false;
|
|
||||||
reject(args[0]);
|
|
||||||
},
|
|
||||||
isPending: function() {
|
|
||||||
return isPending;
|
|
||||||
},
|
|
||||||
promise: promise,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: PostMessageApi only handles message events with a data payload with a
|
// NOTE: PostMessageApi only handles message events with a data payload with a
|
||||||
// response field
|
// response field
|
||||||
export default class PostMessageApi {
|
export default class PostMessageApi {
|
||||||
|
@ -53,8 +23,8 @@ export default class PostMessageApi {
|
||||||
this._window = targetWindow || window.parent; // default to parent window
|
this._window = targetWindow || window.parent; // default to parent window
|
||||||
this._timeoutMs = timeoutMs || 5000; // default to 5s timer
|
this._timeoutMs = timeoutMs || 5000; // default to 5s timer
|
||||||
this._counter = 0;
|
this._counter = 0;
|
||||||
this._pending = {
|
this._requestMap = {
|
||||||
// $ID: Deferred
|
// $ID: {resolve, reject}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,15 +52,12 @@ export default class PostMessageApi {
|
||||||
if (payload.response === undefined) {
|
if (payload.response === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const deferred = self._pending[payload._id];
|
const promise = self._requestMap[payload._id];
|
||||||
if (!deferred) {
|
if (!promise) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!deferred.isPending()) {
|
delete self._requestMap[payload._id];
|
||||||
return;
|
promise.resolve(payload);
|
||||||
}
|
|
||||||
delete self._pending[payload._id];
|
|
||||||
deferred.resolve(payload);
|
|
||||||
};
|
};
|
||||||
return this._onMsgCallback;
|
return this._onMsgCallback;
|
||||||
}
|
}
|
||||||
|
@ -99,19 +66,20 @@ export default class PostMessageApi {
|
||||||
this._counter += 1;
|
this._counter += 1;
|
||||||
target = target || "*";
|
target = target || "*";
|
||||||
action._id = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter;
|
action._id = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter;
|
||||||
const d = defer();
|
|
||||||
this._pending[action._id] = d;
|
|
||||||
this._window.postMessage(action, target);
|
|
||||||
|
|
||||||
if (this._timeoutMs > 0) {
|
return new Promise((resolve, reject) => {
|
||||||
setTimeout(function() {
|
this._requestMap[action._id] = {resolve, reject};
|
||||||
if (!d.isPending()) {
|
this._window.postMessage(action, target);
|
||||||
return;
|
|
||||||
}
|
if (this._timeoutMs > 0) {
|
||||||
console.error("postMessage request timed out. Sent object: " + JSON.stringify(action));
|
setTimeout(() => {
|
||||||
d.reject(new Error("Timed out"));
|
if (!this._requestMap[action._id]) {
|
||||||
}, this._timeoutMs);
|
return;
|
||||||
}
|
}
|
||||||
return d.promise;
|
console.error("postMessage request timed out. Sent object: " + JSON.stringify(action));
|
||||||
|
this._requestMap[action._id].reject(new Error("Timed out"));
|
||||||
|
}, this._timeoutMs);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue