I'm having trouble understanding the flow of the following code. The code should process MERGE_SIZE lines (3 in this run), save the lines to a 'phase' file, and then process the next 3 lines and so on.
The call to savePhase has an await so I was expecting for the savePhase to complete before additional lines are processed.
As you can see in the output below every line is process and then the savePhase calls complete.
Future _sort() async {
var completer = Completer<void>();
var instance = 0;
var lineCount = MERGE_SIZE;
var phaseDirectory = Directory.systemTemp.createTempSync();
var list = <String>[];
var sentToPhase = false;
await File(filename)
.openRead()
.map(utf8.decode)
.transform(LineSplitter())
.forEach((l) async {
list.add(l);
print('$l linecount:$lineCount');
lineCount--;
if (lineCount == 0) {
lineCount = MERGE_SIZE;
instance++;
sentToPhase = true;
await savePhase(phaseDirectory, 1, instance, list, lineDelimiter);
list.clear();
print('savePhase completed');
}
});
which outputs
9 line linecount:3
8 line linecount:2
7 line linecount:1
6 line linecount:3
5 line linecount:2
4 line linecount:1
3 line linecount:3
2 line linecount:2
1 line linecount:1
savePhase completed
savePhase completed
savePhase completed
Is this something to do with the streams that openRead uses to deliver the read lines?
I thought I had await figured out, but apparently not :)