async_phase 0.0.1+1 copy "async_phase: ^0.0.1+1" to clipboard
async_phase: ^0.0.1+1 copied to clipboard

A class and its subclasses representing phases of an asynchronous operation.

example/async_phase_example.dart

import 'dart:async';
import 'package:async_phase/async_phase.dart';
import 'utils.dart';

Future<void> main() async {
  final calc = Calculation(80.0, onCalc);
  await wait();

  for (final divisor in [2, 4, 0]) {
    print('Dividing by $divisor...');
    await wait();

    await calc.divideBy(divisor);
    await wait();
  }
}

void onCalc(AsyncPhase<double> phase) {
  final message = phase.when(
    initial: (data) => 'Initial value\n  $data',
    waiting: (data) => '  $data (waiting)',
    complete: (data) => '  $data (complete)',
    error: (data, e, s) => '  $data ($e)',
  );
  print(message);
}

class Calculation {
  Calculation(double initial, CalcCallback onCalc) {
    phase = AsyncInitial(initial);
    onCalc(phase);
  }

  late AsyncPhase<double> phase;

  Future<void> divideBy(num value) async {
    phase = phase.copyAsWaiting();
    onCalc(phase);

    phase = await AsyncPhase.from(
      () => phase.data!.divideBy(value),
      fallbackData: -1.0,
    );
    onCalc(phase);
  }
}
6
likes
0
pub points
16%
popularity

Publisher

verified publisherkaboc.cc

A class and its subclasses representing phases of an asynchronous operation.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

meta

More

Packages that depend on async_phase