Skip to content

Commit

Permalink
retry fetchCell after generationidmismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
neindochoh committed Mar 19, 2024
1 parent 8f00e3e commit 3995cb4
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/hooks/useCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,24 +11,40 @@ interface CachedCell {
const cellCacheCapacity = 100;
const cellCache: Map<string, CachedCell> = 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<unknown> | 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 {
return response.value();
}
}

async function _sleep(ms: number) {
await new Promise((resolve) => setTimeout(resolve, ms));
}

function _getCell(
datasetId: string,
column: string,
Expand Down

0 comments on commit 3995cb4

Please sign in to comment.