multitasking 1.1.0 copy "multitasking: ^1.1.0" to clipboard
multitasking: ^1.1.0 copied to clipboard

Cooperative multitasking using asynchronous tasks, with support for forced task termination with `onExit` handlers.

example/example.dart

import 'dart:async';

import 'package:multitasking/multitasking.dart';

Future<void> main() async {
  final controller = StreamController<int>();
  final master = Task.run<void>(name: 'master', () async {
    Task.onExit((task) {
      print('Exit $task');
      if (!controller.isClosed) {
        print('$task closing controller');
        controller.close();
      }
    });

    var i = 0;
    Timer.periodic(Duration(seconds: 1), (timer) {
      controller.add(i++);
    });

    // Wait  forever
    await Completer<void>().future;
  });

  final stream = controller.stream;
  final slave = Task.run<void>(name: 'slave', () async {
    Task.onExit((task) {
      print('Exit $task');
    });

    await for (final value in stream) {
      print(value);
      await Task.sleep();
    }
  });

  Timer(Duration(seconds: 3), () {
    print('Stop $slave');
    slave.stop();
    print('Stop $master');
    master.stop();
  });

  try {
    await Task.waitAll([master, slave]);
  } catch (e) {
    print(e);
  }

  print('Tasks stopped');
}
0
likes
160
points
336
downloads

Publisher

unverified uploader

Weekly Downloads

Cooperative multitasking using asynchronous tasks, with support for forced task termination with `onExit` handlers.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

async, meta, stack_trace

More

Packages that depend on multitasking