updateValue method

Future<void> updateValue(
  1. V? changeValue(
    1. V? value
    ), {
  2. bool notifyOnNull = false,
})

Update value directly in storage.

Implementation

Future<void> updateValue(V? Function(V? value) changeValue,
    {bool notifyOnNull = false}) async {
  _lock.synchronized(() async {
    final cached = await _storage.get(_resourceKey);
    final newValue = changeValue.call(cached?.data);

    if (newValue != null) {
      await _storage.put(
        _resourceKey,
        newValue,
        storeTime: cached?.storeTime ?? 0,
      );

      _subject.add(Resource.success(newValue));
    } else if (cached != null) {
      await _storage.delete(_resourceKey);
      if (notifyOnNull) _subject.add(Resource.success(null));
    }
  });
}