nextAvailableAt method

  1. @override
  2. @server
  3. @client
Future<DateTime?> nextAvailableAt({
  1. DateTime? asOf,
})
override

Smallest non-null availableAt among entries that have not yet become available (availableAt > asOf, asOf defaulting to now), or null if there are none. Drives a TTB-sweep wake-up timer.

Note the strict-greater-than filter: keys that are already born are excluded. TTB doesn't have TTL's "becomes actionable when crossed → deleted soon after" lifecycle — a born key stays in the store, so only the not-yet-born subset is interesting for a wake-up timer.

Snapshot semantics: best-effort with respect to concurrent mutations, as for KeyValueStore.nextExpiresAt.

MUST be O(1)-or-better than a full-store scan on any backend bundled with this package.

Implementation

@override
@server
@client
Future<DateTime?> nextAvailableAt({DateTime? asOf}) async {
  final cutoff = asOf ?? DateTime.timestamp();
  DateTime? min;
  for (final fields in _expiryKeysCache.values) {
    final avail = fields[availableAt];
    // Strictly after the cutoff: already-born keys are excluded.
    if (avail == null || !avail.isAfter(cutoff)) continue;
    if (min == null || avail.isBefore(min)) min = avail;
  }
  return min;
}