write method

Future<void> write(
  1. T value
)

persist the state

Implementation

Future<void> write(T value) async {
  setPersistStateSingleton();
  // try {
  if (throttleDelay != null) {
    _valueForThrottle = value;
    if (_throttleTimer != null) {
      return;
    }
    _throttleTimer = Timer(Duration(milliseconds: throttleDelay!), () async {
      _throttleTimer = null;
      final r = await _persistStateSingleton!
          .write<String>(key, toJson!(_valueForThrottle!));
      if (debugPrintOperations) {
        StatesRebuilerLogger.log(
          'PersistState: write($key, $_valueForThrottle)',
        );
      }
      _valueForThrottle = null;
      return r;
    });
    return;
  }
  final json = toJson!(value);
  if (json == cachedJson) {
    return;
  }
  cachedJson = json;
  await _persistStateSingleton!.write<String>(key, json);
  if (debugPrintOperations) {
    StatesRebuilerLogger.log(
      'PersistState: write($key, $json)',
    );
  }
}