Skip to content

Commit

Permalink
Merge pull request #446 from Renumics/feature/retry-lens-update-on-ge…
Browse files Browse the repository at this point in the history
…nid-mismatch

retry fetchCell after generationidmismatch
  • Loading branch information
neindochoh authored Mar 20, 2024
2 parents 8f00e3e + 3995cb4 commit 0146e2a
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 0146e2a

Please sign in to comment.