Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/printing/print_layout_queue.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:async';

import 'pdf_export_cancellation.dart';

/// פריסת ספר מלא ל-PDF אחת בכל התהליך: כל פריסה היא isolate עם הספר כולו בזיכרון.
class PrintLayoutQueue {
PrintLayoutQueue._();

static Future<void>? _tail;

/// מסתיים כשהפריסות שבתור הסתיימו.
static Future<void>? get idle => _tail;

/// מריץ את [layout] אחרי הפריסות הקודמות; אם [isCancelled] נכון כשהגיע תורו,
/// הפריסה אינה מתחילה ונזרק [PdfExportCancelledException].
static Future<T> run<T>(
Future<T> Function() layout, {
required bool Function() isCancelled,
}) async {
final completer = Completer<void>();
final previous = _tail;
_tail = completer.future;
try {
if (previous != null) await previous;
if (isCancelled()) throw const PdfExportCancelledException();
return await layout();
} finally {
if (identical(_tail, completer.future)) _tail = null;
completer.complete();
}
}
}
8 changes: 7 additions & 1 deletion lib/printing/view/printing_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import 'package:otzaria/printing/print_pdf_source.dart';
import 'package:otzaria/printing/pdf_file_stream.dart';
import 'package:otzaria/printing/pdf_file_cleanup.dart';
import 'package:otzaria/printing/print_pdf_file_cache.dart';
import 'package:otzaria/printing/print_layout_queue.dart';
import 'package:otzaria/printing/pdf_export_cancellation.dart';
import 'package:otzaria/printing/print_preview_controller.dart';
import 'package:opentype_shaper/opentype_shaper.dart';
Expand Down Expand Up @@ -247,6 +248,7 @@ class PrintingScreen extends StatefulWidget {
static Future<void> get debugRasterIdle async {
await Future.wait<void>([
?_PrintingScreenState._rasterLock,
?PrintLayoutQueue.idle,
..._PrintingScreenState._fileCleanups,
]);
}
Expand Down Expand Up @@ -1447,7 +1449,7 @@ class _PrintingScreenState extends State<PrintingScreen> {
final fullBackFont = pw.Font.ttf(fallbackBytes.buffer.asByteData());

// הגופנים נרשמים ומשתחררים ב-worker; אותם בתים משמשים גם להטמעה ב-PDF.
return Isolate.run(
Future<Uint8List?> layout() => Isolate.run(
() => withShaperFontPair(
createPrimary: () => ShaperFont.register(primaryBytes),
createFallback: () => ShaperFont.register(fallbackBytes),
Expand Down Expand Up @@ -1615,8 +1617,12 @@ class _PrintingScreenState extends State<PrintingScreen> {
},
),
);
// tear-off: closure שלוכד את this היה נשלח ל-isolate יחד עם layout.
return PrintLayoutQueue.run(layout, isCancelled: _isLayoutCancelled);
}

bool _isLayoutCancelled() => _preview.isStopped;

Future<List<Link>> _loadLinksForPrintRange(
int selectedStart,
int selectedEnd,
Expand Down
60 changes: 60 additions & 0 deletions test/printing/print_layout_queue_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'dart:async';

import 'package:flutter_test/flutter_test.dart';
import 'package:otzaria/printing/pdf_export_cancellation.dart';
import 'package:otzaria/printing/print_layout_queue.dart';

void main() {
test('פריסות ממסכים שונים רצות אחת אחרי השנייה', () async {
var active = 0;
var maxActive = 0;
final gates = List.generate(3, (_) => Completer<void>());
final runs = [
for (final gate in gates)
PrintLayoutQueue.run(() async {
active++;
if (active > maxActive) maxActive = active;
await gate.future;
active--;
return gates.indexOf(gate);
}, isCancelled: () => false),
];
await pumpEventQueue();
expect(active, 1);
for (final gate in gates) {
gate.complete();
await pumpEventQueue();
}
expect(await Future.wait(runs), [0, 1, 2]);
expect(maxActive, 1);
expect(PrintLayoutQueue.idle, isNull);
});

test('מסך שנסגר בזמן ההמתנה אינו מתחיל פריסה', () async {
final gate = Completer<void>();
final first = PrintLayoutQueue.run(
() => gate.future,
isCancelled: () => false,
);
var closed = false;
var started = false;
final queued = PrintLayoutQueue.run(() async {
started = true;
}, isCancelled: () => closed);
closed = true;
gate.complete();
await first;
await expectLater(queued, throwsA(isA<PdfExportCancelledException>()));
expect(started, isFalse);
});

test('כשל בפריסה אינו חוסם את הבאה', () async {
final failed = PrintLayoutQueue.run<int>(
() async => throw StateError('layout'),
isCancelled: () => false,
);
final next = PrintLayoutQueue.run(() async => 7, isCancelled: () => false);
await expectLater(failed, throwsStateError);
expect(await next, 7);
});
}
Loading