Skip to content

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

License

Notifications You must be signed in to change notification settings

desktop-dart/channel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages