Stateful Operation

Besides being treated as a Model, a function can also have state like a Unit. For example, Future<void> doSomething():

  • when doing nothing, it is in a state of stand-by.

  • when being executed, it is in a state of running.

That is why we created a convenient class for this type of function - StatefulOperation.

late final action = StatefulOperation<void, void>(
    (_) async {
      try {
        await Future.delayed(const Duration(seconds: 2));

        print('end');
      } catch (e) {

        // Handle exception

        print('end');
      }
    },
  );

void main() async {
    action(null);

    final running = action.runnings.join('-'); // null

    await Future.delayed(const Duration(seconds: 2));

    final running = action.runnings.join('-'); // nothing, indicates that action has completed.
}
  • StatefulOperation<I, O>: I is the type of parameter, O is the type of output.

  • UnmodifiableListView<I> get runnings: provides a list of running code blocks of the functions represented by the input parameters of each time the functions are triggered.