mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
fix(timeline): keep the pagination gate open after a failed backfill
This commit is contained in:
4 files changed
+94
-16
No files matched your search
@@ -52,6 +52,8 @@ const {
|
||||
forwardStatus: 'idle',
|
||||
canPaginateBack: false,
|
||||
canPaginateForward: false,
|
||||
backwardError: false,
|
||||
forwardError: false,
|
||||
jumpFailed: false,
|
||||
focusItem: undefined as { eventId: string; scrollTo: boolean; highlight: boolean } | undefined,
|
||||
setFocusItem: vi.fn<() => void>(),
|
||||
@@ -408,6 +410,8 @@ beforeEach(() => {
|
||||
timelineSync.liveTimelineLinked = true;
|
||||
timelineSync.jumpFailed = false;
|
||||
timelineSync.backwardStatus = 'idle';
|
||||
timelineSync.backwardError = false;
|
||||
timelineSync.forwardError = false;
|
||||
timelineSync.forwardStatus = 'idle';
|
||||
(timelineSync.handleTimelinePagination as ReturnType<typeof vi.fn>).mockReset();
|
||||
(timelineSync.cancelEventTimelineLoad as ReturnType<typeof vi.fn>).mockReset();
|
||||
@@ -859,7 +863,7 @@ describe('failed backfill on an empty timeline', () => {
|
||||
it('surfaces the error with a working Retry instead of endless placeholders', () => {
|
||||
timelineSync.eventsLength = 0;
|
||||
timelineSync.canPaginateBack = true;
|
||||
timelineSync.backwardStatus = 'error';
|
||||
timelineSync.backwardError = true;
|
||||
|
||||
const { getByText } = renderTimeline();
|
||||
|
||||
|
||||
@@ -826,12 +826,21 @@ export function RoomTimeline({
|
||||
prevBackwardStatusRef.current = timelineSync.backwardStatus;
|
||||
if (timelineSync.backwardStatus === 'loading') {
|
||||
wasAtBottomBeforePaginationRef.current = atBottomRef.current;
|
||||
} else if (prev === 'loading' && timelineSync.backwardStatus === 'idle') {
|
||||
} else if (
|
||||
prev === 'loading' &&
|
||||
timelineSync.backwardStatus === 'idle' &&
|
||||
!timelineSync.backwardError
|
||||
) {
|
||||
if (scrollOwnerRef.current === 'event' && scrollAnchorRef.current !== undefined) {
|
||||
restoreScrollAnchor();
|
||||
} else if (wasAtBottomBeforePaginationRef.current) scrollToBottom();
|
||||
}
|
||||
}, [timelineSync.backwardStatus, restoreScrollAnchor, scrollToBottom]);
|
||||
}, [
|
||||
timelineSync.backwardStatus,
|
||||
timelineSync.backwardError,
|
||||
restoreScrollAnchor,
|
||||
scrollToBottom,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!timelineSync.focusItem?.scrollTo || !vListRef.current) return;
|
||||
@@ -1247,8 +1256,7 @@ export function RoomTimeline({
|
||||
|
||||
// A failed backfill keeps its pagination token, so the placeholder condition
|
||||
// would otherwise hold forever and never reach the error and its Retry.
|
||||
const showEmptyPaginationError =
|
||||
timelineSync.eventsLength === 0 && timelineSync.backwardStatus === 'error';
|
||||
const showEmptyPaginationError = timelineSync.eventsLength === 0 && timelineSync.backwardError;
|
||||
|
||||
const showLoadingPlaceholders =
|
||||
timelineSync.eventsLength === 0 &&
|
||||
@@ -1256,8 +1264,12 @@ export function RoomTimeline({
|
||||
(!isReady || timelineSync.canPaginateBack || timelineSync.backwardStatus === 'loading');
|
||||
|
||||
let backPaginationJSX: ReactNode | undefined;
|
||||
if (timelineSync.canPaginateBack || timelineSync.backwardStatus !== 'idle') {
|
||||
if (timelineSync.backwardStatus === 'error') {
|
||||
if (
|
||||
timelineSync.canPaginateBack ||
|
||||
timelineSync.backwardStatus !== 'idle' ||
|
||||
timelineSync.backwardError
|
||||
) {
|
||||
if (timelineSync.backwardError) {
|
||||
backPaginationJSX = (
|
||||
<Box
|
||||
justifyContent="Center"
|
||||
@@ -1282,8 +1294,12 @@ export function RoomTimeline({
|
||||
}
|
||||
|
||||
let frontPaginationJSX: ReactNode | undefined;
|
||||
if (!timelineSync.liveTimelineLinked || timelineSync.forwardStatus !== 'idle') {
|
||||
if (timelineSync.forwardStatus === 'error') {
|
||||
if (
|
||||
!timelineSync.liveTimelineLinked ||
|
||||
timelineSync.forwardStatus !== 'idle' ||
|
||||
timelineSync.forwardError
|
||||
) {
|
||||
if (timelineSync.forwardError) {
|
||||
frontPaginationJSX = (
|
||||
<Box
|
||||
justifyContent="Center"
|
||||
|
||||
@@ -715,6 +715,45 @@ const syncOpts = (
|
||||
});
|
||||
|
||||
describe('back-pagination', () => {
|
||||
describe('a failed backfill leaves the gate open', () => {
|
||||
it('keeps the status idle so a later attempt is not blocked', async () => {
|
||||
const { room } = createPaginableRoom();
|
||||
const paginate = vi.fn<() => Promise<boolean>>(async () => {
|
||||
throw new Error('net::ERR_INTERNET_DISCONNECTED');
|
||||
});
|
||||
const { result } = renderHook(() => useTimelineSync(syncOpts(room, paginate, () => false)));
|
||||
|
||||
await act(async () => {
|
||||
await result.current.handleTimelinePagination(true, true);
|
||||
});
|
||||
|
||||
expect(result.current.backwardError).toBe(true);
|
||||
expect(result.current.backwardStatus).toBe('idle');
|
||||
});
|
||||
|
||||
it('clears the error as soon as the next attempt starts', async () => {
|
||||
const { room, events } = createPaginableRoom();
|
||||
const paginate = vi
|
||||
.fn<() => Promise<boolean>>()
|
||||
.mockRejectedValueOnce(new Error('net::ERR_INTERNET_DISCONNECTED'))
|
||||
.mockImplementation(async () => {
|
||||
events.push({}, {}, {}, {}, {});
|
||||
return true;
|
||||
});
|
||||
const { result } = renderHook(() => useTimelineSync(syncOpts(room, paginate, () => true)));
|
||||
|
||||
await act(async () => {
|
||||
await result.current.handleTimelinePagination(true);
|
||||
});
|
||||
expect(result.current.backwardError).toBe(true);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.handleTimelinePagination(true);
|
||||
});
|
||||
expect(result.current.backwardError).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normal sync', () => {
|
||||
it('auto-continues over hidden-only pages, then settles back to idle', async () => {
|
||||
const { room, events } = createPaginableRoom();
|
||||
@@ -873,11 +912,14 @@ describe('back-pagination', () => {
|
||||
await act(async () => {
|
||||
await result.current.handleTimelinePagination(true);
|
||||
});
|
||||
expect(result.current.backwardStatus).toBe('error');
|
||||
expect(result.current.backwardError).toBe(true);
|
||||
// The gate must not latch, or neither retry path can ever fire again.
|
||||
expect(result.current.backwardStatus).toBe('idle');
|
||||
|
||||
await act(async () => {
|
||||
await result.current.handleTimelinePagination(true);
|
||||
});
|
||||
expect(result.current.backwardError).toBe(false);
|
||||
expect(result.current.backwardStatus).toBe('idle');
|
||||
expect(paginateEventTimeline).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
@@ -48,7 +48,7 @@ const EVENT_TIMELINE_LOAD_TIMEOUT_MS = 12000;
|
||||
|
||||
const JUMP_CONTEXT_LIMIT = 20;
|
||||
|
||||
type PaginationStatus = 'idle' | 'loading' | 'error';
|
||||
type PaginationStatus = 'idle' | 'loading';
|
||||
|
||||
type TimelineState = {
|
||||
linkedTimelines: EventTimeline[];
|
||||
@@ -193,6 +193,11 @@ const useTimelinePagination = (
|
||||
const alive = useAlive();
|
||||
const [backwardStatus, setBackwardStatus] = useState<PaginationStatus>('idle');
|
||||
const [forwardStatus, setForwardStatus] = useState<PaginationStatus>('idle');
|
||||
// Kept apart from the status so a failure surfaces without latching the gate: both
|
||||
// retry paths only fire from 'idle', so folding failure into the status strands the
|
||||
// timeline behind Retry for the rest of the room visit.
|
||||
const [backwardError, setBackwardError] = useState(false);
|
||||
const [forwardError, setForwardError] = useState(false);
|
||||
|
||||
const fetchingRef = useRef({ backward: false, forward: false });
|
||||
const paginate = useMemo(() => {
|
||||
@@ -226,9 +231,13 @@ const useTimelinePagination = (
|
||||
|
||||
fetchingRef.current[directionKey] = true;
|
||||
const setStatus = backwards ? setBackwardStatus : setForwardStatus;
|
||||
if (alive()) setStatus('loading');
|
||||
const setFailed = backwards ? setBackwardError : setForwardError;
|
||||
if (alive()) {
|
||||
setStatus('loading');
|
||||
setFailed(false);
|
||||
}
|
||||
|
||||
let settledStatus: PaginationStatus = 'idle';
|
||||
let failed = false;
|
||||
|
||||
try {
|
||||
const maxAttempts = autoContinue ? MAX_AUTO_CONTINUATIONS : 0;
|
||||
@@ -245,7 +254,7 @@ const useTimelinePagination = (
|
||||
);
|
||||
|
||||
if (err) {
|
||||
settledStatus = 'error';
|
||||
failed = true;
|
||||
return;
|
||||
}
|
||||
if (!alive()) return;
|
||||
@@ -271,7 +280,10 @@ const useTimelinePagination = (
|
||||
}
|
||||
} finally {
|
||||
fetchingRef.current[directionKey] = false;
|
||||
if (alive()) setStatus(settledStatus);
|
||||
if (alive()) {
|
||||
setStatus('idle');
|
||||
if (failed) setFailed(true);
|
||||
}
|
||||
}
|
||||
};
|
||||
}, [
|
||||
@@ -286,7 +298,7 @@ const useTimelinePagination = (
|
||||
onFocusedForwardExhausted,
|
||||
]);
|
||||
|
||||
return { paginate, backwardStatus, forwardStatus };
|
||||
return { paginate, backwardStatus, forwardStatus, backwardError, forwardError };
|
||||
};
|
||||
|
||||
const useLiveEventArrive = (
|
||||
@@ -591,6 +603,8 @@ export function useTimelineSync({
|
||||
paginate: handleTimelinePagination,
|
||||
backwardStatus,
|
||||
forwardStatus,
|
||||
backwardError,
|
||||
forwardError,
|
||||
} = useTimelinePagination(
|
||||
mx,
|
||||
room,
|
||||
@@ -800,6 +814,8 @@ export function useTimelineSync({
|
||||
canPaginateForward,
|
||||
backwardStatus,
|
||||
forwardStatus,
|
||||
backwardError,
|
||||
forwardError,
|
||||
handleTimelinePagination,
|
||||
loadEventTimeline,
|
||||
cancelEventTimelineLoad,
|
||||
|
||||
Reference in new issue
Block a user