Skip to content

Commit bfff3c8

Browse files
authored
fix(pagination): count a page set with setItems as loaded, even when empty (#1995)
Ticket: https://linear.app/stream/issue/REACT-1475 ## CLA - [x] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required). - [x] Code changes are tested ## Description of the changes, What, Why and How? Found in the second side-by-side QA round (React `examples/vite`, v15 + v10). The v15 "Unread" channel list could open with no request and show only some unread channels. **Why:** a live event can route a channel into a list that was never queried (a message makes a channel unread before the "Unread" list was ever shown). The channel goes into the paginator's logical head interval, so `items` becomes defined although no page was loaded. stream-chat-react's `ChannelList` ran its first query only while `items === undefined`, so it skipped it. `isInitialized` can't replace that check: an empty `setItems` page, which the example uses to seed a catch-all list that must never query, leaves it `false` too. From the outside, both states looked the same. **What / How** (`BasePaginator`): - New getter `hasLoadedPage`: `true` after a query or any `setItems` page, including an empty one, and `false` for items that only events ingested. `isInitialized` is unchanged, so connection recovery (`ChannelManager.recover`) still skips a seeded empty list. - `setItems` sets the flag before it publishes, and `resetState` clears it (and the query shape) before it publishes, so state subscribers such as a UI selector read current values. Checks: `yarn lint`, `yarn types` and `yarn test` (4950 passed). The suite reports 2 unhandled errors from thread and poll tests, also reported before this change. Verified in the vite example against a local build: after a live message made a channel unread, opening "Unread" sends the query and lists all three unread channels, where it used to list only the event-routed one. The seeded catch-all list still sends no query. The stream-chat-react half is in GetStream/stream-chat-react#3364 (`fix(ChannelList)`, same ticket). It needs a release with this getter. ## Changelog - Added `BasePaginator.hasLoadedPage`: whether a page was loaded by a query or `setItems` (including an empty one), as opposed to items that only live events ingested into a never-queried list.
1 parent f5d1a2d commit bfff3c8

2 files changed

Lines changed: 98 additions & 3 deletions

File tree

‎src/pagination/paginators/BasePaginator.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ export abstract class BasePaginator<T, Q, R = PaginationQueryReturnValue<T>> {
529529
protected _executeQueryDebounced!: DebouncedExecQueryFunction<Q>;
530530
/** Last effective query shape produced by the subclass for the most recent request. */
531531
protected _lastQueryShape?: Q;
532+
/** Set by `setItems`, which loads a page (even an empty one) without a query. */
533+
private _pageSetWithoutQuery = false;
532534

533535
/**
534536
* Stable, purely data-driven comparison (age, timestamps, ...). Default for
@@ -645,6 +647,18 @@ export abstract class BasePaginator<T, Q, R = PaginationQueryReturnValue<T>> {
645647
return typeof this._lastQueryShape !== 'undefined';
646648
}
647649

650+
/**
651+
* True once a page was loaded: by a query, or by `setItems`, including an empty page (e.g. a
652+
* catch-all list seeded so it never runs its own query). Items that live events ingested into a
653+
* never-queried list are not a loaded page, although they make `items` defined: a UI deciding
654+
* whether to run the first query reads this, not `items === undefined`. Unlike `isInitialized`,
655+
* an empty `setItems` page counts, so connection recovery (which re-runs initialized lists'
656+
* queries) still skips such a list. Cleared by `resetState`.
657+
*/
658+
get hasLoadedPage() {
659+
return this._pageSetWithoutQuery || this.isInitialized;
660+
}
661+
648662
get isOfflineSupportEnabled() {
649663
return false;
650664
}
@@ -2682,6 +2696,8 @@ export abstract class BasePaginator<T, Q, R = PaginationQueryReturnValue<T>> {
26822696
newState.hasMoreTail = interval.hasMoreTail;
26832697
}
26842698

2699+
// Before the publish below: subscribers read `hasLoadedPage` from inside it.
2700+
this._pageSetWithoutQuery = true;
26852701
return newState;
26862702
});
26872703

@@ -3450,13 +3466,15 @@ export abstract class BasePaginator<T, Q, R = PaginationQueryReturnValue<T>> {
34503466
}
34513467

34523468
resetState() {
3469+
// Nothing is loaded, so the next query is a first page again. A stale shape would make a
3470+
// re-query with an unchanged shape a continuation, ingested without `isHead`. Cleared before
3471+
// the publish below, whose subscribers read `isInitialized` / `hasLoadedPage`.
3472+
this._lastQueryShape = undefined;
3473+
this._pageSetWithoutQuery = false;
34533474
this.state.next(this.initialState);
34543475
this.setIntervals([]);
34553476
this.setActiveInterval(undefined);
34563477
this.clearIntervalViews();
3457-
// Nothing is loaded, so the next query is a first page again. A stale shape would make a
3458-
// re-query with an unchanged shape a continuation, ingested without `isHead`.
3459-
this._lastQueryShape = undefined;
34603478
}
34613479

34623480
/**

‎test/unit/pagination/paginators/BasePaginator.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4534,6 +4534,83 @@ describe('BasePaginator', () => {
45344534
// @ts-expect-error accessing protected property
45354535
expect(paginator._activeIntervalId).toBe(intervals[0].id);
45364536
});
4537+
4538+
describe('hasLoadedPage', () => {
4539+
it('is false until a page is loaded', () => {
4540+
const paginator = new Paginator();
4541+
expect(paginator.hasLoadedPage).toBe(false);
4542+
});
4543+
4544+
it('counts an empty page as loaded, without initializing the query shape', () => {
4545+
const paginator = new Paginator();
4546+
4547+
// e.g. a catch-all list seeded so it never runs its own query
4548+
paginator.setItems({ isLastPage: true, valueOrFactory: [] });
4549+
4550+
expect(paginator.hasLoadedPage).toBe(true);
4551+
// still skipped by connection recovery: there is no query to re-run
4552+
expect(paginator.isInitialized).toBe(false);
4553+
});
4554+
4555+
it('does not count items ingested into a list that was never queried', () => {
4556+
const paginator = new Paginator({ itemIndex });
4557+
paginator.sortComparator = makeComparator<TestItem>({
4558+
sort: [{ field: 'age', direction: -1 }],
4559+
});
4560+
4561+
paginator.ingestItem(v);
4562+
4563+
// the item is shown, but no page was loaded: the first query still has to run
4564+
expect(paginator.items).toEqual([v]);
4565+
expect(paginator.hasLoadedPage).toBe(false);
4566+
4567+
paginator.removeItem({ item: v });
4568+
4569+
expect(paginator.items).toEqual([]);
4570+
expect(paginator.hasLoadedPage).toBe(false);
4571+
});
4572+
4573+
it('is true after a query placed a page and false again after resetState', () => {
4574+
const paginator = new Paginator({ itemIndex });
4575+
paginator.sortComparator = makeComparator<TestItem>({
4576+
sort: [{ field: 'age', direction: -1 }],
4577+
});
4578+
4579+
paginator.placeQueryPage({
4580+
direction: 'tailward',
4581+
isFirstPage: true,
4582+
queryShape: defaultNextQueryShape,
4583+
requestedPageSize: 3,
4584+
results: { items: [] },
4585+
});
4586+
expect(paginator.hasLoadedPage).toBe(true);
4587+
4588+
paginator.resetState();
4589+
expect(paginator.hasLoadedPage).toBe(false);
4590+
4591+
paginator.setItems({ valueOrFactory: [] });
4592+
paginator.resetState();
4593+
expect(paginator.hasLoadedPage).toBe(false);
4594+
});
4595+
4596+
it('is already current when the state change is published', () => {
4597+
// a UI selector reads it from inside the state subscription
4598+
const paginator = new Paginator();
4599+
const seen: boolean[] = [];
4600+
const unsubscribe = paginator.state.subscribe(() => {
4601+
seen.push(paginator.hasLoadedPage);
4602+
});
4603+
4604+
paginator.setItems({ valueOrFactory: [] });
4605+
paginator.resetState();
4606+
paginator.setItems({ valueOrFactory: [a] });
4607+
paginator.resetState();
4608+
unsubscribe();
4609+
4610+
// initial call on subscribe, then each setItems / resetState
4611+
expect(seen).toEqual([false, true, false, true, false]);
4612+
});
4613+
});
45374614
});
45384615

45394616
describe('reload', () => {

0 commit comments

Comments
 (0)