Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry fetchCell after generationidmismatch #446

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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