channel

Creator: coderz1093

Last updated:

Add to Cart

Description:

channel

Duration #
Golang like send/receive communication channel with multi-listen capability.
Usage #
Send #
main() async {
final channel = Channel<int>();
channel.send(i);
}
copied to clipboard
Receive #
main() async {
final channel = Channel<int>();
print(await channel.tryReceive(i));
}
copied to clipboard
Pipe #
pipe method offers a way to pipe the contents of the given stream into the channel.
main() async {
final channel = Channel<int>();
await channel.pipe(Stream.fromIterable(Iterable.generate(10, (i) => i)));
channel.close();
}
copied to clipboard
main() async {
final channel = Channel<int>();
final event = await channel.receive(i);
if(!event.isClosed) {
print(event.data);
}
}
copied to clipboard
Close #
main() async {
final channel = Channel<int>();
channel.send(i);
channel.close();
}
copied to clipboard
Stream #
main() async {
final channel = Channel<int>();
channel.asStream.listen((d) {
print(d);
});
}
copied to clipboard
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));
}
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files:

Customer Reviews

There are no reviews.