Skip to content

Commit 440ed1a

Browse files
committed
התאוששות מכשל קריאה במפרשים בצורת הדף (#5744)
1 parent 8401ee4 commit 440ed1a

5 files changed

Lines changed: 637 additions & 177 deletions

File tree

‎lib/data/data_providers/book_database_resolver.dart‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:io';
22

33
import 'package:otzaria/attached_libraries/repository/attached_library_registry.dart';
44
import 'package:otzaria/data/data_providers/book_composite_key.dart';
5+
import 'package:otzaria/data/data_providers/db_read_worker.dart';
56
import 'package:otzaria/data/data_providers/sqlite_data_provider.dart';
67
import 'package:otzaria/data/data_providers/user_books_database_holder.dart';
78
import 'package:otzaria/data/official_book_title_renames.dart';
@@ -70,13 +71,15 @@ class BookDatabaseResolver {
7071
/// מאתר ספר לפי מאפייניו במסדי הספרייה.
7172
///
7273
/// [preferSource] נבדק ראשון; [officialOnly] מונע fallback ל-`user_books.db`.
74+
/// [throwIfUnavailable] מדווח על מסד קיים שקריאה ממנו אינה זמינה.
7375
static Future<ResolvedDbBookRecord?> resolveBook({
7476
required String title,
7577
int? categoryId,
7678
String? fileType,
7779
String? filePath,
7880
BookSource preferSource = BookSource.official,
7981
bool officialOnly = false,
82+
bool throwIfUnavailable = false,
8083
}) async {
8184
final List<ResolvedBookRepositoryCandidate> candidates;
8285
if (officialOnly) {
@@ -93,6 +96,19 @@ class BookDatabaseResolver {
9396
candidates = await _loadRepositoryCandidates(preferSource: preferSource);
9497
}
9598

99+
if (throwIfUnavailable &&
100+
!preferSource.isAttached &&
101+
SqliteDataProvider.instance.isClosedForExternalWrite) {
102+
throw const DbReadWorkerSuspended();
103+
}
104+
if (throwIfUnavailable &&
105+
candidates.isEmpty &&
106+
preferSource is AttachedBookSource &&
107+
AttachedLibraryRegistry.instance.libraryFor(preferSource.slug) !=
108+
null) {
109+
throw StateError('Cannot read attached library ${preferSource.slug}');
110+
}
111+
96112
return resolveBookInCandidates(
97113
title: title,
98114
candidates: candidates,

‎lib/data/data_providers/database_library_provider.dart‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,22 +3328,32 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider {
33283328
preferSource: source,
33293329
);
33303330
if (record == null || record.source != source) return null;
3331-
final text = await BookTextReader.text(record.repository, record.book);
3332-
if (text != null) return text;
3333-
// ספר מבוסס-קובץ: filePath כבר נפתר בתוך תיקיית המסד (או null).
3334-
final file = record.book.filePath;
3335-
if (file == null || !await File(file).exists()) return null;
3336-
return await readFileBackedBookText(
3337-
File(file),
3338-
record.book.fileType,
3339-
title,
3340-
);
3331+
return await readResolvedBookText(record);
33413332
} catch (e) {
33423333
debugPrint('⚠️ Error reading attached book text: $e');
33433334
return null;
33443335
}
33453336
}
33463337

3338+
/// קורא ספר שכבר אותר, ומעביר כשל קריאה לקורא.
3339+
Future<String?> readResolvedBookText(ResolvedDbBookRecord record) async {
3340+
final book = record.book;
3341+
if (record.source.isAttached) {
3342+
final text = await BookTextReader.text(record.repository, book);
3343+
if (text != null) return text;
3344+
}
3345+
final path = book.filePath;
3346+
if (path != null && (book.isFileBacked || record.source.isAttached)) {
3347+
final file = File(path);
3348+
if (await file.exists()) {
3349+
return readFileBackedBookText(file, book.fileType, book.title);
3350+
}
3351+
}
3352+
return record.source.isAttached
3353+
? null
3354+
: BookTextReader.text(record.repository, book);
3355+
}
3356+
33473357
Future<List<TocEntry>?> _readAttachedBookToc(
33483358
String title,
33493359
int categoryId,

‎lib/data/data_providers/sqlite_data_provider.dart‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ class SqliteDataProvider {
223223
/// פתח מחדש את ה-RO.
224224
int _activeWriteSessions = 0;
225225

226+
/// סשן כתיבה חיצוני מונע זמנית פתיחה של חיבור הקריאה.
227+
bool get isClosedForExternalWrite => _activeWriteSessions > 0;
228+
226229
/// gate לכתיבה חיצונית (isolate סנכרון/generator) — נוצר ב-
227230
/// [closeForExternalWrite] ומושלם ב-[reopenAfterExternalWrite]. קוראים
228231
/// מקבילים ממתינים עליו ב-[initialize] במקום לקבל null, כדי שספרים/מפרשים
@@ -412,6 +415,7 @@ class SqliteDataProvider {
412415
}
413416
}
414417

418+
/// [throwReadErrors] מבדיל בין כשל קריאה לספר חסר או ריק.
415419
Future<({int startLine, int endLine, int totalLines, String text})?>
416420
getBookTextRangeFromDb(
417421
String title, {
@@ -420,11 +424,17 @@ class SqliteDataProvider {
420424
int? categoryId,
421425
String? fileType,
422426
BookSource preferSource = BookSource.official,
427+
bool throwReadErrors = false,
423428
}) async {
424429
if (!_isInitialized) {
425430
await initialize();
426431
}
427-
if (!_isInitialized) return null;
432+
if (!_isInitialized) {
433+
if (throwReadErrors && isClosedForExternalWrite) {
434+
throw const DbReadWorkerSuspended();
435+
}
436+
return null;
437+
}
428438

429439
try {
430440
final resolvedBook = await _resolveBookRecord(
@@ -449,6 +459,7 @@ class SqliteDataProvider {
449459
} on DbReadWorkerBusy {
450460
rethrow;
451461
} catch (e, st) {
462+
if (throwReadErrors) rethrow;
452463
debugPrint(
453464
'[SqliteDataProvider] getBookTextRangeFromDb failed for '
454465
'"$title": $e\n$st',

0 commit comments

Comments
 (0)