cancellation_token_hoc081098 1.0.0-beta.2 copy "cancellation_token_hoc081098: ^1.0.0-beta.2" to clipboard
cancellation_token_hoc081098: ^1.0.0-beta.2 copied to clipboard

outdated

Dart Cancellation Token. Inspired by CancellationToken in C#. A Dart utility package for easy async task cancellation.

example/lib/cancellation_token_hoc081098_example.dart

import 'package:cancellation_token_hoc081098/cancellation_token_hoc081098.dart';
import 'package:rxdart_ext/rxdart_ext.dart';

void main() async {
  await guardFutureExample();
  print('-' * 80);
  await guardStreamExample();
  print('-' * 80);
  await reuseTokenExample();
}

Future<void> guardStreamExample() async {
  final token = CancellationToken();
  final stream = token.guardStream(Rx.fromCallable(() async {
    print('start...');

    token.guard();
    await delay(100);
    token.guard();

    print('Step 1');

    token.guard();
    await delay(100);
    token.guard();

    print('Step 2');

    token.guard();
    await delay(100);
    token.guard();

    print('done...');
    return 42;
  }));

  stream.listen(print, onError: print);

  await delay(120);
  token.cancel();
  await delay(800);
  print('exit...');
}

Future<void> guardFutureExample() async {
  final token = CancellationToken();

  final future = token.guardFuture(() async {
    print('start...');

    token.guard();
    await delay(100);
    token.guard();

    print('Step 1');

    token.guard();
    await delay(100);
    token.guard();

    print('Step 2');

    token.guard();
    await delay(100);
    token.guard();

    print('done...');
    return 42;
  });

  // ignore: unawaited_futures
  future.then(print, onError: print);

  await delay(120);
  token.cancel();
  await delay(800);
  print('exit...');
}

Future<void> reuseTokenExample() async {
  final token = CancellationToken();
  final future1 = token.guardFuture(() async {
    for (var i = 0; i < 10; i++) {
      token.guard();

      print('future1: start $i');
      await delay(100);
      print('future1: end $i');

      token.guard();
    }
  });
  final future2 = token.guardFuture(() async {
    for (var i = 0; i < 10; i++) {
      token.guard();

      print('future2: start $i');
      await delay(100);
      print('future2: end $i');

      token.guard();
    }
  });

  // ignore: unawaited_futures
  Future.wait([future1, future2]).then(
    (result) => print('result: $result'),
    onError: (Object e, StackTrace s) => print('error: $e, $s'),
  );

  await delay(250);
  token.cancel();
  await delay(800);
  print('exit...');
}
2
likes
0
pub points
56%
popularity

Publisher

unverified uploader

Dart Cancellation Token. Inspired by CancellationToken in C#. A Dart utility package for easy async task cancellation.

Repository (GitHub)
View/report issues

Funding

Consider supporting this project:

www.buymeacoffee.com

License

unknown (license)

Dependencies

rxdart_ext

More

Packages that depend on cancellation_token_hoc081098