pop method

Implementation

Future<ExpiringAddr> pop() async {
  return await _lock.synchronized(() async {
    if (_expiringHeap.isEmpty) {
      // Should not happen if called correctly, but defensive.
      throw StateError("Cannot pop from an empty heap");
    }
    final ExpiringAddr result = _expiringHeap[0];

    if (_expiringHeap.length == 1) {
      _expiringHeap.removeLast();
    } else {
      final ExpiringAddr last = _expiringHeap.removeLast();
      _expiringHeap[0] = last;
      last.heapIndex = 0;
      _siftDown(0); // _siftDown is safe for heap of size 1 (now at index 0)
    }

    result.heapIndex = -1; // Mark as removed from heap
    return result;
  });
}