dart-sdk/docs/newsletter/20171103/json3.dart
Florian Loitsch 4528eb32a0 Newsletter 2017-11-03.
Change-Id: I5adca1c6706d00ff074656b39d561a0497fb8717
Reviewed-on: https://dart-review.googlesource.com/18660
Reviewed-by: Florian Loitsch <floitsch@google.com>
2017-11-03 21:05:36 +00:00

52 lines
1.5 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
/// Decodes [input] in a chunked way and yields to the event loop
/// as soon as [maxMicroseconds] have elapsed.
Future<dynamic> decodeJsonChunked(String input, int maxMicroseconds) {
const chunkCount = 100; // Actually one more.
var result;
var outSink = new ChunkedConversionSink.withCallback((x) {
result = x[0];
});
var inSink = JSON.decoder.startChunkedConversion(outSink);
var chunkSize = input.length ~/ chunkCount;
int i = 0;
Future<dynamic> addChunks() {
var sw = new Stopwatch()..start();
while (i < 100) {
inSink.addSlice(input, i * chunkSize, (i + 1) * chunkSize, false);
i++;
if (sw.elapsedMicroseconds > maxMicroseconds) {
// Usually one has to pay attention not to chain too many futures,
// but here we know that there are at most chunkCount linked futures.
return new Future(addChunks);
}
}
inSink.addSlice(input, i * chunkSize, input.length, true);
return new Future.value(result);
}
return addChunks();
}
main() {
var input = new File("big.json").readAsStringSync();
var sw = new Stopwatch()..start();
bool done = false;
// Show that the event-loop is free to do something:
new Timer.periodic(const Duration(milliseconds: 2), (timer) {
print(".");
if (done) timer.cancel();
});
var future = decodeJsonChunked(input, 500);
future.then((result) {
print("done after ${sw.elapsedMicroseconds}us.");
done = true;
});
}