Skip to content

Executor for dart, like the Java single thread pool based on Isolate.

License

Notifications You must be signed in to change notification settings

yrom/dart-executor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Executor for dart, like the Java single thread pool based on Isolate.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages