getAndRemove method

  1. @override
Future<T?> getAndRemove(
  1. String key
)
override

Removes the entry for a key only if currently mapped to some value.

  • key: key with which the specified value is associated

Returns the value if exists or null if no mapping existed for this key

Implementation

@override
Future<T?> getAndRemove(String key) {
  // #region Statistics
  Stopwatch? watch;
  Future<VaultEntry?> Function(VaultEntry? entry) posGet =
      (VaultEntry? entry) => Future.value(entry);
  Future<T?> Function(VaultEntry entry) posRemove =
      (VaultEntry entry) => Future<T?>.value(entry.value);
  if (statsEnabled) {
    watch = clock.stopwatch()..start();
    posGet = (VaultEntry? entry) {
      stats.increaseGets();

      if (watch != null) {
        stats.addGetTime(watch.elapsedMicroseconds);
      }

      return Future.value(entry);
    };
    posRemove = (VaultEntry entry) {
      stats.increaseRemovals();
      if (watch != null) {
        stats.addRemoveTime(watch.elapsedMicroseconds);
        watch.stop();
      }

      return Future<T?>.value(entry.value);
    };
  }
  // #endregion

  // Try to get the entry from the vault
  return _getStorageEntry(key).then(posGet).then((entry) {
    if (entry != null) {
      // The entry exists on vault
      return _removeStorageEntry(
              key, VaultEntryRemovedEvent<T>(this, entry.info))
          .then((_) => posRemove(entry));
    }

    return Future<T?>.value();
  });
}