nextValue method

Future<T> nextValue()

Returns a Future that completes with the next value of this cell, when it is updated.

Implementation

Future<T> nextValue() async {
  var isFirst = true;
  final completer = Completer<T>();

  final watcher = ValueCell.watch(() {
    try {
      final value = this();

      if (!isFirst && !completer.isCompleted) {
        completer.complete(value);
      }
    }
    catch (e, trace) {
      if (!isFirst && !completer.isCompleted) {
        completer.completeError(e, trace);
      }
    }

    isFirst = false;
  });

  try {
    return await completer.future;
  }
  finally {
    watcher.stop();
  }
}