iso 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

Iso #

pub package

An isolates runner that handles bidirectionnal communication. Run some code in an isolate and communicate with it.

Example #

   import 'package:iso/iso.dart';

   void run(IsoRunner iso) async {
     int counter = 0;
     // init the data in channel
     iso.receive();
     // listen to the data coming in
     iso.dataIn.listen((dynamic data) {
       counter = counter + int.parse("$data");
       // send into the main thread
       iso.send(counter);
     });
   }

   void main() async {
     final iso = Iso(run, onDataOut: (dynamic data) => print("Counter: $data"));
     iso.run();
     await iso.onCanReceive;
     // now we can send messages to the isolate
     while (true) {
       await Future<dynamic>.delayed(Duration(seconds: 1));
       iso.send(1);
     }
   }

Usage #

Initialize #

Define a function to be run in an isolate:

   void run(IsoRunner iso) {
      // do something here
   }

Important: this must be a top level function or a static method. The function can be async if needed

Initialize a runner:

   final iso = Iso(run);

Launch the function in the isolate:

   iso.run();
   // to terminate it:
   iso.dispose();

The function can be run with parameters:

   final params = <dynamic>["arg1", "arg2", 3];
   iso.run(params);

To grab the parameters in the isolate:

   void run(IsoRunner iso) {
      if (iso.hasArgs) {
        final List<dynamic> args = iso.args;
      }
   }

Communication channels #

Data coming from the isolate #

Handle data coming from the isolate using a handler function:

   void onDataOut(dynamic data) => print("Data coming from isolate: $data");

   final iso = Iso(run, onDataOut: onDataOut);

If onDataOutis not provided it will print the data to the terminal by default. To disable the default behavior set onDataOut to null:

   final iso = Iso(run, onDataOut: null)

Another option to handle this data is to listen to a channel:

   iso.dataOut.listen((dynamic payload) {
     if (payload == <String, dynamic>{"status": "finished"}) {
       print("Isolate declares it has finished");
       iso.kill();
     }
   });

Data coming into the isolate #

By default this data channel is not activated: you need to do it in the run function if needed:

   void run(IsoRunner iso) {
     iso.receive();
     iso.dataIn.listen((dynamic data) {
       // do something with the data
     });
   }

or:

   void run(IsoRunner iso) {
     iso.receive()
       ..listen((dynamic data) =>
         print("Data received in isolate -> $data / ${data.runtimeType}"));
   }

Send data to the isolate #

This has to be initialized in the isolate before sending as explained above.

   iso.run();
   // wait for the isolate to be ready to receive data
   await iso.onCanReceive;
   // send data
   iso.send("Some data");

Send data from the isolate to the main thread #

   void run(IsoRunner iso) {
      // ...
      iso.send("Some data");
   }

Examples #

Check the example folder to see examples for Dart and Flutter

Changelog #

0.3.0 #

  • Set the default for the onDataOut method to null
  • Update dependencies
  • Use more strict analysis options

0.2.3 #

  • Downgrade to meta 1.1.6 for Flutter stable compatibility
  • Use pedantic for static analysis

0.2.2 #

Prevent killing terminated isolate

0.2.1 #

  • Fix in isolate dispose
  • Make the kill method private

0.2.0 #

  • Minor fixes and some api changes
  • Improve examples
  • Add tests

0.1.0 #

Initial release

example/README.md

Iso examples #

Dart examples #

  • Simple: basic examples
  • Counter: a simple counter that runs in an isolate
  • Http server: an http server that runs in an isolate
  • Messaging: shows how to send messages of different types from the isolate
  • Error: shows error handling

Flutter examples #

  • Isolate worker: shows how to run a worker in an isolate to download files with a progress indicator
  • Geojson: examples of parsing geojson in the background (lib code)
  • Isohttpd: run an http server in an isolate

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  iso: ^0.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:iso/iso.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
65
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
extra_pedantic ^1.1.1+3 1.1.1+3
meta ^1.1.7 1.1.8
pedantic ^1.7.0 1.9.0
Dev dependencies
test ^1.6.5
test_coverage ^0.3.0+1