From 3995cb4f5f2a2073e50529f98d2f2529b9f849b7 Mon Sep 17 00:00:00 2001 From: Dominik Haentsch Date: Tue, 19 Mar 2024 14:20:39 +0100 Subject: [PATCH] retry fetchCell after generationidmismatch --- src/hooks/useCell.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/hooks/useCell.ts b/src/hooks/useCell.ts index 1f7f186b..50a68702 100644 --- a/src/hooks/useCell.ts +++ b/src/hooks/useCell.ts @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react'; import api from '../api'; import { Dataset, useDataset } from '../stores/dataset'; import { Problem, isProblem } from '../types'; +import { ApiResponse } from '../client'; interface CachedCell { generationId: number; @@ -10,13 +11,33 @@ interface CachedCell { const cellCacheCapacity = 100; const cellCache: Map = new Map(); +async function _sleep(ms: number) { + await new Promise((resolve) => setTimeout(resolve, ms)); +} + async function _fetchCell( column: string, row: number, generationId: number, asBuffer: boolean ) { - const response = await api.table.getCellRaw({ column, row, generationId }); + const max_tries = 3; + let tries = 0; + let response: ApiResponse | undefined = undefined; + + while (!response) { + try { + response = await api.table.getCellRaw({ column, row, generationId }); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (error: any) { + const problem = await error.response.json?.(); + if (++tries > max_tries || problem?.type !== 'GenerationIDMismatch') { + throw error; + } + await _sleep(100); + } + } + if (asBuffer) { return response.raw.arrayBuffer(); } else { @@ -24,10 +45,6 @@ async function _fetchCell( } } -async function _sleep(ms: number) { - await new Promise((resolve) => setTimeout(resolve, ms)); -} - function _getCell( datasetId: string, column: string,