isoworker 1.2.0 copy "isoworker: ^1.2.0" to clipboard
isoworker: ^1.2.0 copied to clipboard

This is an easy to use wrapping of the Isolate class to make it easier to write parallel processes.

isoworker #

pub package

English, 日本語

isoworker is a wrapping of the Isolate class to make it easier to write parallel processes.

Features #

  • Unlike Flutter's compute, it doesn't create an Isolate object at task execution time, but uses it instead, so it has less overhead.
  • Tasks can be pipelined with Future.

Usage #

import 'package:isoworker/isoworker.dart';

/// Method to be called when `IsoWorker` is initialized.
/// Provide a top-level or static method with `Stream<WorkerData>` as an argument.
void workerMethod(Stream<WorkerData> message) {
  final _map = {
    'key_1': 'val_1',
    'key_2': 'val_2',
  };
  // Receive messages (WorkerData) to a worker
  message.listen((data) {
    // `WorkerData.value` to receive data from the `exec` runtime.
    final command = data.value['command'];
    switch (command) {
      case 'get':
        // Execute heavy processing, etc. and return the result as `WorkerData.callback`.
        data.callback(_map[data.value['key']]);
        break;
      default:
        // Be sure to call `callback` even if there is nothing there.
        data.callback(null);
    }
  });
}

void main() async {
  // Initialization
  final worker = await IsoWorker.init(workerMethod);
  // Execute the task
  final res = await worker.exec({
    'command': 'get',
    'key': 'key_1',
  });
  print(res);
  // Destroy the Worker
  await worker.dispose();
}

Note #

If an Exception is thrown in the Worker method, all unexecuted tasks will be notified of the Exception.

4
likes
160
points
211
downloads

Publisher

verified publisherzuvola.com

Weekly Downloads

This is an easy to use wrapping of the Isolate class to make it easier to write parallel processes.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on isoworker