Web version: databases read in a worker, the shelf's counts, Chrome and Edge remember chosen files

The page's reader: a Web Worker with a second instance of the program reads
a database (PGN, CBV, ZIP, 7Z) and gives back its text and index; the page
puts it together without reading it again (chess_core::PgnIndex,
PgnDatabase::into_index / from_index, serde behind a feature). A 13 MB CBV:
the longest frame 17 ms in Firefox, 67 ms in Chromium (1000 ms before).
platform::open_database is the same on both sides: away from the screen,
done when read. The shelf's cards count the games the same way (one file
at a time); the counts are kept through platform::fs.

Chrome, Edge (File System Access): a chosen file's handle is kept in the
browser's storage; at the next visit the file is read again by itself when
the browser allows it, else the card offers «Allow reading» (the browser's
own question). Files of different visits get folders of their own.

Checks: t.choose(files, open) for both browsers (in Chromium the handles of
the site's private file system stand for the dialog's); app.mjs checks the
draft, the question on leaving and the remembered file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-30 17:50:54 +03:00
parent 000821887f
commit f694bc768b
31 changed files with 1048 additions and 190 deletions

View file

@ -32,6 +32,18 @@ impl From<Cancelled> for OpenError {
}
}
/// What reading a database's text found: everything of a [`PgnDatabase`] but the text.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PgnIndex {
detected: EncodingGuess,
encoding: TextEncoding,
entries: Vec<GameEntry>,
content_hash: u64,
letter_repair: LetterRepairDetection,
repair_letters: bool,
}
/// Identity of the source content, for change detection and cache keys.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SourceFingerprint {
@ -113,6 +125,39 @@ impl PgnDatabase {
})
}
/// The database taken apart: its text and its index (to rebuild it elsewhere —
/// the web version reads a database in a worker and gives it to the page).
pub fn into_index(self) -> (Vec<u8>, PgnIndex) {
let index = PgnIndex {
detected: self.detected,
encoding: self.encoding,
entries: self.entries,
content_hash: self.fingerprint.content_hash,
letter_repair: self.letter_repair,
repair_letters: self.repair_letters,
};
(self.bytes, index)
}
/// The database again from its text and the index [`Self::into_index`] gave,
/// without reading the text once more.
pub fn from_index(bytes: Vec<u8>, index: PgnIndex) -> Self {
PgnDatabase {
path: None,
fingerprint: SourceFingerprint {
len: bytes.len() as u64,
modified: None,
content_hash: index.content_hash,
},
bytes,
detected: index.detected,
encoding: index.encoding,
entries: index.entries,
letter_repair: index.letter_repair,
repair_letters: index.repair_letters,
}
}
pub fn path(&self) -> Option<&Path> {
self.path.as_deref()
}
@ -223,6 +268,26 @@ impl PgnDatabase {
mod tests {
use super::*;
/// A database taken apart and put together again is the same database.
#[test]
fn index_round_trip() {
let text = "[White \"A\"]\n[Black \"B\"]\n\n1. e4 e5 *\n\n[White \"C\"]\n\n1. d4 {x} *\n";
let db = PgnDatabase::from_bytes(
text.as_bytes().to_vec(),
None,
&mut |_| {},
&AtomicBool::new(false),
)
.unwrap();
let (entries, hash) = (db.entries().to_vec(), db.fingerprint.content_hash);
let (bytes, index) = db.into_index();
let again = PgnDatabase::from_index(bytes, index);
assert_eq!(again.entries(), entries.as_slice());
assert_eq!(again.fingerprint.content_hash, hash);
assert_eq!(again.bytes(), text.as_bytes());
assert_eq!(again.load_game(1).unwrap().game.tag("White"), Some("C"));
}
fn assert_send_sync<T: Send + Sync>() {}
#[test]