transform_in_isolate 0.1.3 copy "transform_in_isolate: ^0.1.3" to clipboard
transform_in_isolate: ^0.1.3 copied to clipboard

Simple helper to run stream transformation through newly spawned isolate.

example/example.dart

import 'dart:async';
import 'dart:isolate';

import 'package:transform_in_isolate/transform_in_isolate.dart';

void main() async {
  final values = [1, 2, 3, 4];

  print("");
  print("No isolate:");
  print("------------------------");

  await Stream.fromIterable(values)
      .transform(SimpleTransform())
      .listen(log)
      .asFuture();

  print("");
  print("With isolate:");
  print("------------------------");

  await Stream.fromIterable(values)
      .transform(TransformInIsolate(SimpleTransform()))
      .listen(log)
      .asFuture();

  print("");
  print("Error from isolate:");
  print("------------------------");
  await Stream.fromIterable(values)
      .transform(TransformInIsolate(BrokenTransform()))
      .listen(log)
      .asFuture()
      .catchError(log);
}

class SimpleTransform extends StreamTransformerBase<int, int> {
  Stream<int> bind(Stream<int> stream) async* {
    await for (final val in stream) {
      log(val);
      await Future.delayed(Duration(milliseconds: 10));
      yield val;
    }
  }
}

class BrokenTransform extends StreamTransformerBase<int, int> {
  Stream<int> bind(stream) async* {
    yield await stream.first;
    await Future.delayed(Duration(milliseconds: 10));
    throw Exception("Error message from isolate: ${Isolate.current.debugName}");
  }
}

void log(dynamic value) {
  print("[${Isolate.current.debugName}] $value");
}
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Simple helper to run stream transformation through newly spawned isolate.

Repository (GitHub)
View/report issues

License

BSD-2-Clause (LICENSE)

More

Packages that depend on transform_in_isolate