mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 11:35:04 +01:00
fallback for platforms missing AbortController
This commit is contained in:
parent
b3095112cd
commit
ca4361248f
@ -6,8 +6,21 @@ import {
|
|||||||
|
|
||||||
class RequestWrapper {
|
class RequestWrapper {
|
||||||
constructor(promise, controller) {
|
constructor(promise, controller) {
|
||||||
this._promise = promise;
|
if (!controller) {
|
||||||
this._controller = controller;
|
const abortPromise = new Promise((_, reject) => {
|
||||||
|
this._controller = {
|
||||||
|
abort() {
|
||||||
|
const err = new Error("fetch request aborted");
|
||||||
|
err.name = "AbortError";
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
this._promise = Promise.race([promise, abortPromise]);
|
||||||
|
} else {
|
||||||
|
this._promise = promise;
|
||||||
|
this._controller = controller;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abort() {
|
abort() {
|
||||||
@ -47,13 +60,13 @@ export default class HomeServerApi {
|
|||||||
headers.append("Content-Type", "application/json");
|
headers.append("Content-Type", "application/json");
|
||||||
bodyString = JSON.stringify(body);
|
bodyString = JSON.stringify(body);
|
||||||
}
|
}
|
||||||
const controller = new AbortController();
|
const controller = typeof AbortController === "function" ? new AbortController() : null;
|
||||||
// TODO: set authenticated headers with second arguments, cache them
|
// TODO: set authenticated headers with second arguments, cache them
|
||||||
let promise = fetch(url, {
|
let promise = fetch(url, {
|
||||||
method,
|
method,
|
||||||
headers,
|
headers,
|
||||||
body: bodyString,
|
body: bodyString,
|
||||||
signal: controller.signal
|
signal: controller && controller.signal
|
||||||
});
|
});
|
||||||
promise = promise.then(async (response) => {
|
promise = promise.then(async (response) => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
Loading…
Reference in New Issue
Block a user