From f27e32e54c3e6a6f7d156cf6c23c11778a7dd316 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Mon, 5 Dec 2022 13:25:04 -0600 Subject: Ensure the UI always renders, even in bad network conditions (close #6) --- src/state/lib/api.ts | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/state/lib/api.ts') diff --git a/src/state/lib/api.ts b/src/state/lib/api.ts index 842905d1d..f17d0337c 100644 --- a/src/state/lib/api.ts +++ b/src/state/lib/api.ts @@ -12,6 +12,8 @@ import {APP_BSKY_GRAPH} from '../../third-party/api' import {RootStoreModel} from '../models/root-store' import {extractEntities} from '../../lib/strings' +const TIMEOUT = 10e3 // 10s + export function doPolyfill() { AtpApi.xrpc.fetch = fetchHandler } @@ -175,10 +177,14 @@ async function fetchHandler( reqBody = JSON.stringify(reqBody) } + const controller = new AbortController() + const to = setTimeout(() => controller.abort(), TIMEOUT) + const res = await fetch(reqUri, { method: reqMethod, headers: reqHeaders, body: reqBody, + signal: controller.signal, }) const resStatus = res.status @@ -197,6 +203,9 @@ async function fetchHandler( throw new Error('TODO: non-textual response body') } } + + clearTimeout(to) + return { status: resStatus, headers: resHeaders, -- cgit 1.4.1