commit<T> function

Future<T> commit<T>(
  1. CommitCallback<T> callback, {
  2. String? debugName,
})

Perform synchronous change of state.

Use this method when changes in state should be synchronous and in strict order. It is valid to perform commits in nested order which mean next operation will not performed until nested done.

Example:

class SomeFragment extends Fragment {

  Future<void> performWithNested() {
    final StringBuffer output = StringBuffer();
    await commit(() async {
      output.write('u1');
      commit(() async {
        output.write('n1');
      });
      output.write('u2');
      store.commit(() async {
        output.write('n2');
        store.commit(() async {
          output.write('in1');
        });
        output.write('n3');
      });
      output.write('u3');
    });
    assert(output.toString() == 'u1n1u2n2in1n3u3');
  }
}

Implementation

Future<T> commit<T>(
  CommitCallback<T> callback, {
  String? debugName,
}) async {
  final maybeGuard = Zone.current[_CommitGuard];

  return maybeGuard != null
      ? await FlateConfiguration._commitRunner(callback, debugName: debugName)
      : await runZoned(
          () async {
            return await FlateConfiguration._commitLock.synchronized(
              () async {
                return await FlateConfiguration._commitRunner(
                  callback,
                  debugName: debugName,
                );
              },
            );
          },
          zoneValues: {
            _CommitGuard: const _CommitGuard(),
          },
        );
}