channel 1.0.0
channel: ^1.0.0

Dart native js
Flutter Android iOS web

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

Duration #

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

Usage #

Send #

main() async {
  final channel = Channel<int>();
  channel.send(i);
}

Receive #

main() async {
  final channel = Channel<int>();
  print(await channel.tryReceive(i));
}
main() async {
  final channel = Channel<int>();
  final event = await channel.receive(i);
  if(!event.isClosed) {
    print(event.data);
  }
}

Close #

main() async {
  final channel = Channel<int>();
  channel.send(i);
  channel.close();
}

Stream #

main() async {
  final channel = Channel<int>();
  channel.asStream.listen((d) {
    print(d);
  });
}

Example #

import 'package:channel/channel.dart';
import 'package:pedantic/pedantic.dart';

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

  unawaited(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;
      }
    }
  }));

  unawaited(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);
  }

  channel.close();

  await Future.delayed(Duration(seconds: 5));
}
0
likes
90
pub points
38%
popularity

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

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

tejainece@gmail.com

License

BSD (LICENSE)

More

Packages that depend on channel