From ca4361248f1e4996ea5885d8071ee25397d2e778 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 26 Jun 2019 19:49:49 +0200 Subject: [PATCH] fallback for platforms missing AbortController --- src/matrix/hs-api.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/matrix/hs-api.js b/src/matrix/hs-api.js index 4c6ebf86..cc2e60f4 100644 --- a/src/matrix/hs-api.js +++ b/src/matrix/hs-api.js @@ -6,8 +6,21 @@ import { class RequestWrapper { constructor(promise, controller) { - this._promise = promise; - this._controller = controller; + if (!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() { @@ -47,13 +60,13 @@ export default class HomeServerApi { headers.append("Content-Type", "application/json"); 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 let promise = fetch(url, { method, headers, body: bodyString, - signal: controller.signal + signal: controller && controller.signal }); promise = promise.then(async (response) => { if (response.ok) {