wuchuheng_isolate_channel 0.0.5 copy "wuchuheng_isolate_channel: ^0.0.5" to clipboard
wuchuheng_isolate_channel: ^0.0.5 copied to clipboard

This a library to simplify isolate thread communication.

example/wuchuheng_isolate_channel_example.dart

import 'package:wuchuheng_isolate_channel/src/service/task/index.dart';
import 'package:wuchuheng_isolate_channel/wuchuheng_isolate_channel.dart';
import 'package:wuchuheng_logger/wuchuheng_logger.dart';

void main() async {
  /// Isolate logic code.
  final Task task = await IsolateTask((message, channel) {
    Logger.info('isolate: receive $message');
    channel.send('task data');
    channel.onClose((name) => Logger.info('Channel is closed. channel: $name.'));
  });

  ///Main thread code.
  final channel = task.createChannel(name: 'channelName')
    ..listen((message, channel) => Logger.info('Receiving isolate messages')).cancel();
  channel.send('Send data to isolate');
  await Future.delayed(Duration(seconds: 1));

  ///Turn off the channel
  channel.close();

  /// listen to future
  final task2 = await IsolateTask((message, channel) {
    channel.send(message);
  });
  final channel2 = task2.createChannel();
  final result = channel2.listenToFuture();
  channel.send('OK');
  print(await result); // print 2
}
2
likes
0
pub points
0%
popularity

Publisher

unverified uploader

This a library to simplify isolate thread communication.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

json_annotation, wuchuheng_hooks

More

Packages that depend on wuchuheng_isolate_channel