Giter Site home page Giter Site logo

iso's Introduction

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

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

iso's People

Contributors

drnorton avatar synw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

iso's Issues

Failure to fully run test coverage

Hi!

It looks like one of your tests is failing to complete -- running pub run test_coverage causes pub run to hang.

I'll see if I can debug further when I have time, although my primary interest right now in your package is that there are some metrics we're collecting across the pub.dev package list, and I ran into this with your package.

Thanks for supporting the Dart ecosystem and using Dart!

Should create sample for running on Flutter

This is cool library. i've checked source code in the lib. It's easy to understand. However, it will be perfect if you create sample for running on Flutter. Because as i thought, most of developers use dart to code in Flutter. They want to see visual application sample can run code in background. From my source (isolate_worker), i change to use your library for demo. Please check with the link: https://github.com/fluttervn/isolate_woker/tree/iso

Error: Unsupported operation: dart:isolate is not supported on dart4web

Hey there, thank you for this lib.
Can you imagine adding web support for it?

Error: Unsupported operation: dart:isolate is not supported on dart4web
    at Object.throw_ [as throw] (http://localhost:50470/dart_sdk.js:4339:11)
    at Object._unsupported (http://localhost:50470/dart_sdk.js:59179:15)
    at isolate$._ReceivePort.new.get sendPort [as sendPort] (http://localhost:50470/dart_sdk.js:58861:23)
    at iso.Iso.new.run (http://localhost:50470/packages/iso/src/iso.dart.lib.js:74:88)
    at run.next (<anonymous>)
    at runBody (http://localhost:50470/dart_sdk.js:37699:34)
    at Object._async [as async] (http://localhost:50470/dart_sdk.js:37730:7)
    at iso.Iso.new.run (http://localhost:50470/packages/iso/src/iso.dart.lib.js:72:20)
    at geojson.GeoJson.new._parse (http://localhost:50470/packages/geojson/src/geojson.dart.lib.js:390:32)
    at _parse.next (<anonymous>)
    at runBody (http://localhost:50470/dart_sdk.js:37699:34)
    at Object._async [as async] (http://localhost:50470/dart_sdk.js:37730:7)
    at geojson.GeoJson.new.[_parse] (http://localhost:50470/packages/geojson/src/geojson.dart.lib.js:375:20)
    at geojson.GeoJson.new.parse (http://localhost:50470/packages/geojson/src/geojson.dart.lib.js:285:26)
    at nearby_airports._NearbyAirportsPageState.new.loadAirports (http://localhost:50470/packages/flutter_map_demo/airports/nearby_airports.dart.lib.js:359:24)
    at loadAirports.next (<anonymous>)
    at http://localhost:50470/dart_sdk.js:37679:33
    at _RootZone.runUnary (http://localhost:50470/dart_sdk.js:37533:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:50470/dart_sdk.js:32507:29)
    at handleValueCallback (http://localhost:50470/dart_sdk.js:33054:49)
    at Function._propagateToListeners (http://localhost:50470/dart_sdk.js:33092:17)
    at async._AsyncCallbackEntry.new.callback (http://localhost:50470/dart_sdk.js:32818:27)
    at Object._microtaskLoop (http://localhost:50470/dart_sdk.js:37794:13)
    at _startMicrotaskLoop (http://localhost:50470/dart_sdk.js:37800:13)
    at http://localhost:50470/dart_sdk.js:33309:9
Flutter 1.23.0-18.1.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 198df796aa (4 weeks ago) • 2020-10-15 12:04:33 -0700
Engine • revision 1d12d82d9c
Tools • Dart 2.11.0 (build 2.11.0-213.1.beta)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.