Skip to content

Latest commit

 

History

History
59 lines (44 loc) · 1.03 KB

README.md

File metadata and controls

59 lines (44 loc) · 1.03 KB

executor

Like the Java single thread pool based on Isolate.

Install

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

dependencies:
  executor: 
    git: https://github.com/yrom/dart-executor.git

Get it:

$ pub get

Import it:

import 'package:executor/executor.dart';

Example

Use the global cachedExecutor:

int _max_bytes = 10 * 1024;

/// decoding fat-json
Future<dynamic> decodeJson(Uint8List bytes) async {
  if (bytes == null) return null;
  if (bytes.lengthInBytes <= _max_bytes) {
    return _decodeJsonBytes(bytes);
  }
  // decode large bytes in different isolate.
  return await cachedExecutor.run(_decodeJsonBytes, bytes);
}

dynamic _decodeJsonBytes(Uint8List bytes) {
  var str = utf8.decoder.convert(bytes);
  return json.decode(str);
}

Use the Executor:

// spawn an new Executor instance
var exec = await Executor.spawn(debugName: 'MyExecutor');

// compute in Executor
var result = await exec.run(_decodeJsonBytes, bytes);

// close when done.
await exec.close();