ReactivePagedManager<Q, T, C extends Object> class

A manager for paginated lists: one accumulated list per query, with concurrent-safe loadNext/loadPrevious/refresh, dedupe by item id, cross-query item patching, and automatic eviction of unwatched queries.

Type parameters:

  • Q: the query (filters, sort, ...). Needs value equality and must not contain the page, offset or cursor.
  • T: the item type.
  • C: the opaque cursor type (page number, offset, token, ...). The package never interprets it.

Loading futures never throw and never hang: they resolve to the resulting PagedState. Errors are exposed in the state and through onError.

final events = ReactivePagedManager<EventQuery, Event, int>(
  idOf: (e) => e.id,
  fetchPage: (req) async {
    final page = req.cursor ?? 1;
    final r = await api.events(req.query, page: page);
    return PageResult(r.items, next: r.hasNext ? page + 1 : null);
  },
);

Constructors

ReactivePagedManager({required PageFetcher<Q, T, C> fetchPage, required Object? idOf(T item), Duration? maxAge, int? maxEntries, Duration? evictAfterUnwatched, Retention retainWhenUnwatched = Retention.all, RefreshMode refreshMode = RefreshMode.firstPage, int maxRefreshPages = 10, int? maxItems, int maxUnproductivePages = 3, ReactiveStorage<Q, PagedSnapshot<T>>? storage, bool persistWhen(Q query)?, int storageVersion = 0, Duration? storageReadTimeout, Object errorMapper(Object error, StackTrace stackTrace)?, void onError(Q query, PageDirection direction, Object error, StackTrace stackTrace)?, DateTime clock() = DateTime.now})
Creates a manager.

Properties

cachedQueries → Iterable<Q>
The queries currently held in memory.
no setter
clock → DateTime Function()
Time source.
final
errorMapper → Object Function(Object error, StackTrace stackTrace)?
Maps errors thrown by fetchPage before they are stored and reported.
final
evictAfterUnwatched → Duration?
How long a query may stay unwatched before it is evicted.
final
fetchPage → PageFetcher<Q, T, C>
Loads one page.
final
hashCode → int
The hash code for this object.
no setterinherited
idOf → Object? Function(T item)
Item identity.
final
maxAge → Duration?
Age of the head after which a state is stale.
final
maxEntries → int?
Maximum number of unwatched queries kept in memory.
final
maxItems → int?
Windowing limit.
final
maxRefreshPages → int
Cap for RefreshMode.allPages.
final
maxUnproductivePages → int
Pages that add nothing new followed per call.
final
onError → void Function(Q query, PageDirection direction, Object error, StackTrace stackTrace)?
Failure callback.
final
persistWhen → bool Function(Q query)?
Which queries are persisted (default: all, when storage is set).
final
refreshMode → RefreshMode
Default refresh mode.
final
retainWhenUnwatched → Retention
What is kept for unwatched queries.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
storage → ReactiveStorage<Q, PagedSnapshot<T>>?
Optional persistence layer.
final
storageReadTimeout → Duration?
The longest a storage read may take before it counts as missing.
final
storageVersion → int
Entries persisted with a different version are ignored and deleted.
final

Methods

clearCache() → void
evict for every query (e.g. on logout), including persisted entries.
debugCorruptPageCounts(Q query) → void
Breaks the page bookkeeping of query on purpose, to test that the debug-build invariant check is loud. For tests only.
debugCursors(Q query) → ({C? next, int pages, C? previous})?
The cursors of query, for tests and debugging only (cursors are not part of PagedState on purpose): next and previous are the cursors the next loadNext/loadPrevious would send (null at an end, and for a snapshot restored from storage), pages how many pages are loaded. null when query is not cached.
dispose() → Future<void>
Aborts everything, cancels timers, closes streams. Pending futures resolve. Later calls do nothing.
evict(Q query) → void
Resets query: in-flight loads are discarded, the cache and the persisted entry are dropped. Listeners stay subscribed and receive an empty idle state; nothing is refetched automatically.
invalidate(Q query) → void
Marks query stale and discards its in-flight loads. A watched query refetches immediately; an unwatched one is only marked and revalidates on the next watch.
invalidateAll() → void
invalidate for every cached query.
invalidateWhere(bool test(Q query)) → void
invalidate for every cached query matching test.
loadNext(Q query) → Future<PagedState<T>>
Loads the page after the tail. Joins an in-flight loadNext, and joins a running head load instead of appending to a list being replaced. Starts the first load when nothing is loaded (or only a restored snapshot is); a no-op without more pages.
loadPrevious(Q query) → Future<PagedState<T>>
Loads the page before the head. Same rules as loadNext, including the first-page load (and debug warning) for a query that is not cached.
mutate<R>(Future<R> action(), {void optimistic(PagedEdits<Q, T> edits)?, bool invalidateAfter(Q query)?}) → Future<R>
Runs action with optimistic edits.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patchItem(Object id, T transform(T current), {bool where(Q query)?}) → void
Applies transform to the item with id in every cached query (or those matching where). Emits only for queries that contained the id. The transform must be pure: it may be replayed onto a response that arrives later.
peek(Q query) → PagedState<T>?
The current state of query, or null when it is not cached.
prefetch(Q query) → void
Fire-and-forget: loads the first page of query when nothing is loaded yet, or when what is loaded is stale (invalidated, older than maxAge, or only restored from storage). Otherwise, and while a first-page load or refresh is already running, it does nothing. Never throws; failures go to the state and onError as usual. The query is cached unwatched, so it counts towards maxEntries and evictAfterUnwatched.
refresh(Q query, {C? anchor, RefreshMode? mode}) → Future<PagedState<T>>
Refetches the head, keeping the previous items visible (isRefreshing). On failure the previous items stay and the error is in the state. Joins a running refresh; a different anchor or mode supersedes it. Aborts running loadNext/loadPrevious. See RefreshMode.
removeItem(Object id, {bool where(Q query)?}) → void
Removes the item with id from every cached query (or those matching where) and decrements totalCount. Emits only for queries that changed.
toString() → String
A string representation of this object.
inherited
upsertItem(Q query, T item, {PagedEnd at = PagedEnd.head, bool move = false}) → void
Inserts or replaces item in query.
watch(Q query, {bool fetch = true}) → Stream<PagedState<T>>
The state of query as a stream. Emits the current state first, then every change. With fetch (default) it loads the first page when the query has nothing loaded yet or is stale; that load starts before the first emission, so the first state already has isRefreshing: true (a new query never emits an idle empty state first). An idle state with nothing loaded therefore means "not fetching" (fetch: false) or "reset" (after evict / clearCache). Listening is ref-counted: it drives evictAfterUnwatched and maxEntries.

Operators

operator ==(Object other) → bool
The equality operator.
inherited