jovial_misc_native 0.8.2 copy "jovial_misc_native: ^0.8.2" to clipboard
jovial_misc_native: ^0.8.2 copied to clipboard

Library split off from jovial_misc to make it clear that the rest of jovial_misc works on the JS runtime.

example/example.dart

import 'dart:async';
import 'dart:io';
import 'package:intl/intl.dart';
import 'package:jovial_misc/io_utils.dart';
import 'package:jovial_misc_native/io_utils_native.dart';
import 'package:jovial_misc_native/isolate_stream.dart';

///
/// Example of using [FlushingIOSink] with [DataOutputSink] and
/// [DataInputStream] to encode values that are compatible with
/// `java.io.DataInputStream` and `java.io.DataOutputStream`
///
Future<void> dataIoStreamExample() async {
  final file = File.fromUri(Directory.systemTemp.uri.resolve('test.dat'));
  final sink = FlushingIOSink(file.openWrite());
  final out = DataOutputSink(sink);
  out.writeUTF8('Hello, world.');
  unawaited(sink.close());
  await sink.done;

  final dis = DataInputStream(file.openRead());
  print(await dis.readUTF8());
  await dis.close();
  await file.delete();
}

///
/// Example of using [IsolateStream] to run a computationally-intensive
/// generator function in an isolate.  We use FizzBuzz as a
/// stand-in for a computationally intensive series of values.
///
Future<void> isolateStreamExample() async {
  const max = 25;
  final fmt = NumberFormat();
  const iterationPause = Duration(milliseconds: 250);
  print('Generating FizzBuzz sequence up to ${fmt.format(max)}');

  final stream = IsolateStream<String>(FizzBuzzGenerator(max));
  // Our stream will be limited to 11 strings in the buffer at a time.
  for (var iter = StreamIterator(stream); await iter.moveNext();) {
    print(iter.current);
    await Future<void>.delayed(iterationPause);
  }
  // Note that the producer doesn't run too far ahead of the consumer,
  // because the buffer is limited to 30 strings.
}

/// The generator that runs in a separate isolate.
class FizzBuzzGenerator extends IsolateStreamGenerator<String> {
  final int _max;

  FizzBuzzGenerator(this._max) {
    print('FizzBuzzGenerator constructor.  Note that this only runs once.');
    // This demonstrats that when FizzBuzzGenerator is sent to the other
    // isolate, the receiving isolate does not run the constructor.
  }

  @override
  Future<void> generate() async {
    for (var i = 1; i <= _max; i++) {
      var result = '';
      if (i % 3 == 0) {
        result = 'Fizz';
      }
      if (i % 5 == 0) {
        result += 'Buzz';
      }
      print('        Generator sending $i $result');
      if (result == '') {
        await sendValue(i.toString());
      } else {
        await sendValue(result);
      }
    }
  }

  @override
  int sizeOf(String value) => 1; // 1 entry

  @override
  int get bufferSize => 7; // Buffer up to 7 entries
}

///
/// Run the examples
///
void main() async {
  await dataIoStreamExample();
  print('');
  await isolateStreamExample();
}
0
likes
130
pub points
0%
popularity

Publisher

verified publisherjovial.com

Library split off from jovial_misc to make it clear that the rest of jovial_misc works on the JS runtime.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

async, collection, convert, intl, jovial_misc, pointycastle

More

Packages that depend on jovial_misc_native