cancellation_token_hoc081098 1.0.0-beta.1 copy "cancellation_token_hoc081098: ^1.0.0-beta.1" to clipboard
cancellation_token_hoc081098: ^1.0.0-beta.1 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);
}

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...');
}
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

meta, rxdart_ext

More

Packages that depend on cancellation_token_hoc081098