execute method

Future<void> execute({
  1. Duration delay = const Duration(milliseconds: 200),
  2. required int value,
})

Implementation

Future<void> execute({
  Duration delay = const Duration(milliseconds: 200),
  required int value,
}) async {
  if (value == 0) {
    throw Exception('[ERROR]: value must be greater than or equal to 1.');
  }

  if (_executing == true) {
    cancel();
  }

  _executing = true;
  executeStream.add(_executing);
  var calculated = value;
  while (calculated != 1 && _executing) {
    await Future.delayed(delay);

    if (_executing == true) {
      if (calculated % 2 == 1) {
        calculated = (calculated * 3) + 1;
      } else {
        calculated = calculated ~/ 2;
      }

      mathStream.add(calculated);
    }
  }

  cancel();
}