channel 2.0.2 copy "channel: ^2.0.2" to clipboard
channel: ^2.0.2 copied to clipboard

Golang like send/receive communication channel with multi-listen capability

example/channel_example.dart

import 'package:channel/channel.dart';

void main() async {
  final channel = Channel<int>();

  final futures = <Future>[];
  futures.add(Future.microtask(() async {
    while (true) {
      final data = await channel.receive();
      if (!data.isClosed) {
        print('In first task: ${data.data}');
      } else {
        print('First task closed');
        break;
      }
    }
  }));

  futures.add(Future.microtask(() async {
    while (true) {
      final data = await channel.receive();
      if (!data.isClosed) {
        print('In second task: ${data.data}');
      } else {
        print('Second task closed');
        break;
      }
    }
  }));

  for (int i = 0; i < 10; i++) {
    channel.send(i);
    if(i % 3 == 0) {
      await Future.delayed(Duration(microseconds: 1));
    }
  }
  print('all values sent');

  channel.close();

  await Future.wait(futures);
}
4
likes
140
pub points
76%
popularity

Publisher

unverified uploader

Golang like send/receive communication channel with multi-listen capability

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on channel