Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Engine and Drive speed pass
Yashwanth Kamireddi4e60fbef55bc
diff --git a/.uiverify/contacts-quiet.mjs b/.uiverify/contacts-quiet.mjsnew file mode 100644index 0000000..5066827--- /dev/null+++ b/.uiverify/contacts-quiet.mjs@@ -0,0 +1,70 @@+// Contacts reads its book again only when there is something new in it. A pass that landed+// with nothing new, or with something new only for another app, leaves it alone; one that+// changed contacts — or one that cannot say what it changed — is read. A card sent while+// another app is in front is read once on coming back, not once per send out of sight.+import { engine } from "./engine.mjs";+import { SERVICES, installStub, openScreen } from "./stub.mjs";+const b = await engine.launch();+const ctx = await b.newContext({ viewport: { width: 1280, height: 800 } });+await ctx.addInitScript(installStub, { screen: "library", services: SERVICES, twoFactor: null, contactsFixture: true });+await ctx.addInitScript(() => {+ const internals = window.__TAURI_INTERNALS__;+ const invoke = internals.invoke;+ internals.invoke = async (cmd, args) => {+ if (cmd === "contacts_list") window.__reads = (window.__reads ?? 0) + 1;+ return invoke(cmd, args);+ };+});+const page = await ctx.newPage();+page.on("pageerror", (e) => { console.log(`FAIL page error: ${e.message}`); process.exitCode = 1; });+await page.goto(process.env.BASE ?? process.env.URL ?? "http://localhost:4173", { waitUntil: "networkidle" });+await openScreen(page, "Contacts");+await page.waitForTimeout(500);++let failures = 0;+const ok = (p, m) => { console.log(`${p ? "ok " : "FAIL"} ${m}`); if (!p) failures++; };+const reads = () => page.evaluate(() => window.__reads ?? 0);+const status = { services: [], asOf: null, tookMs: 0 };+const synced = (changed) => page.evaluate(([s, c]) => window.__emit("quince://synced", c === undefined ? s : { ...s, changed: c }), [status, changed]);+const sent = (services) => page.evaluate((s) => window.__emit("quince://outbox-changed", { services: s }), services);++ok((await page.locator(".contact-row").count()) > 0, "the book is on screen");++let before = await reads();+await synced([]);+await page.waitForTimeout(300);+ok((await reads()) === before, "a pass that stored nothing new is not read again");++await synced(["notes", "photos"]);+await page.waitForTimeout(300);+ok((await reads()) === before, "nor one that changed only other apps");++await synced(["contacts"]);+await page.waitForTimeout(300);+ok((await reads()) === before + 1, "one that changed contacts is read, once");++before = await reads();+await synced(undefined);+await page.waitForTimeout(300);+ok((await reads()) === before + 1, "one that cannot say what it changed is read, as always");++// Another app in front: a contact sent meanwhile waits for the screen to come back.+await openScreen(page, "Notes");+await page.waitForTimeout(400);+before = await reads();+await sent(["contacts"]);+await sent(["contacts"]);+await page.waitForTimeout(300);+ok((await reads()) === before, "out of sight, a card sent is not read at once");+await openScreen(page, "Contacts");+await page.waitForTimeout(400);+ok((await reads()) === before + 1, "and is read once on coming back");++// In front, a card sent is read at once, as before.+before = await reads();+await sent(["contacts"]);+await page.waitForTimeout(300);+ok((await reads()) === before + 1, "in front, a card sent is read at once");++await b.close();+if (failures) process.exitCode = 1;diff --git a/.uiverify/drive-thumbs.mjs b/.uiverify/drive-thumbs.mjsnew file mode 100644index 0000000..efbfc07--- /dev/null+++ b/.uiverify/drive-thumbs.mjs@@ -0,0 +1,68 @@+// The open folder's pictures are asked for when it opens and when something in it changes —+// not on every ten-second look that found nothing new, which sent and redrew every picture in+// the folder each time. A folder whose pictures never came is asked again.+import { engine } from "./engine.mjs";+import { SERVICES, installStub } from "./stub.mjs";++const b = await engine.launch();+const ctx = await b.newContext({ viewport: { width: 1280, height: 800 } });+await ctx.addInitScript(installStub, { screen: "library", services: SERVICES, twoFactor: null,+ calendarFixture: false, notesFixture: false, contactsFixture: false, driveFixture: true, remindersFixture: false });+await ctx.addInitScript(() => {+ const internals = window.__TAURI_INTERNALS__;+ const invoke = internals.invoke;+ internals.invoke = async (cmd, args) => {+ if (cmd === "drive_sync_folder") window.__checks = (window.__checks ?? 0) + 1;+ if (cmd === "drive_thumbnails") {+ window.__thumbs = (window.__thumbs ?? 0) + 1;+ if (window.__thumbsFail) throw new Error("offline");+ }+ const answer = await invoke(cmd, args);+ if (cmd === "drive_children" && window.__phone && Array.isArray(answer)) {+ return [...answer, { drivewsid: "FILE::phone", parentId: null, name: "Scan from phone", extension: "pdf",+ kind: "FILE", size: 2048, childCount: null, createdAt: 1792500000, modifiedAt: 1792500000, etag: "p1" }];+ }+ return answer;+ };+});+const page = await ctx.newPage();+page.on("pageerror", (e) => { console.log(`FAIL page error: ${e.message}`); process.exitCode = 1; });+await page.goto(process.env.BASE ?? process.env.URL ?? "http://localhost:4173", { waitUntil: "networkidle" });+await page.locator(".nav-item", { hasText: "Drive" }).click();+await page.waitForTimeout(800);+let failures = 0;+const ok = (p, m) => { console.log(`${p ? "ok " : "FAIL"} ${m}`); if (!p) failures++; };+const thumbs = () => page.evaluate(() => window.__thumbs ?? 0);+const checks = () => page.evaluate(() => window.__checks ?? 0);++ok((await thumbs()) === 1, "opening the folder asks for its pictures");+ok((await page.locator("img.drive-thumb").count()) > 0, "and draws them");++// Nothing new in ten seconds: the folder is looked at, its pictures are not asked for again.+let before = await thumbs();+let looked = await checks();+await page.waitForTimeout(10_600);+ok((await checks()) > looked, "the open folder is checked again after ten seconds");+ok((await thumbs()) === before, "and a check that found nothing new asks for no pictures");+ok((await page.locator("img.drive-thumb").count()) > 0, "which stay on screen");++// Something new: its picture is asked for.+await page.evaluate(() => { window.__phone = true; });+await page.waitForTimeout(10_600);+ok((await thumbs()) === before + 1, "a file arriving asks for the pictures again");++// Pictures that never came (offline) are asked for at the next look, changed or not.+await page.locator(".nav-item", { hasText: "Drive" }).click();+await page.evaluate(() => { window.__thumbsFail = true; window.__phone = false; });+await page.locator(".drive-row-hit", { hasText: "Downloads" }).dblclick();+await page.waitForTimeout(800);+await page.evaluate(() => { window.__thumbsFail = false; });+before = await thumbs();+await page.waitForTimeout(10_600);+ok((await thumbs()) === before + 1, "a folder whose pictures failed is asked again");+before = await thumbs();+await page.waitForTimeout(10_600);+ok((await thumbs()) === before, "and, once they came, not again while nothing changes");++await b.close();+if (failures) process.exitCode = 1;diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rsindex c5273e2..89df23d 100644--- a/crates/quince-auth/src/lib.rs+++ b/crates/quince-auth/src/lib.rs@@ -268,6 +268,23 @@ pub struct Client {/// The keyring would not answer when this started — locked, or not up yet. Not the same/// as nothing stored: it is asked again before anything else happens.keyring_error: Option<String>,+ /// What was last written, as a hash of the session and one of the cookies: a heartbeat that+ /// changed neither writes neither (see [`Client::persist`]).+ persisted: std::sync::Mutex<Persisted>,+}++/// Hashes of the session and the cookie jar as last written; `None` for not written yet.+#[derive(Default)]+struct Persisted {+ session: Option<u64>,+ cookies: Option<u64>,+}++fn hash_of(bytes: &[u8]) -> u64 {+ use std::hash::{Hash, Hasher};+ let mut h = std::collections::hash_map::DefaultHasher::new();+ bytes.hash(&mut h);+ h.finish()}impl Client {@@ -334,6 +351,7 @@ impl Client {pending: None,awaiting_code: false,keyring_error,+ persisted: std::sync::Mutex::new(Persisted::default()),})}@@ -367,8 +385,41 @@ impl Client {}}+ /// Keep the session and its cookies where the next start finds them — each only when it+ /// differs from what was last written.+ ///+ /// Every heartbeat, so every sync and every return to the window, wrote the session to the+ /// keyring: several D-Bus writes, deletes of old names and a read-back, all while holding+ /// the sign-in lock, for a session that had not changed. Only a write that went through is+ /// remembered, so one that failed is made again next time.fn persist(&self) -> Result<()> {- self.store.save(&self.session)?;+ let session = serde_json::to_vec(&self.session)+ .map_err(|e| Error::Protocol(format!("couldn't save the session: {e}")))?;+ let mut cookies = Vec::new();+ {+ let jar = self.jar.lock().expect("cookie jar mutex poisoned");+ cookie_store::serde::json::save(&jar, &mut cookies)+ .map_err(|e| Error::Protocol(format!("couldn't save cookies: {e}")))?;+ }+ let (session, cookies) = (hash_of(&session), (hash_of(&cookies), cookies));+ let mut last = self+ .persisted+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if last.session != Some(session) {+ self.store.save(&self.session)?;+ last.session = Some(session);+ }+ if last.cookies != Some(cookies.0) {+ self.write_cookies(&cookies.1)?;+ last.cookies = Some(cookies.0);+ }+ Ok(())+ }++ /// The cookie jar, as `cookie_store` wrote it, to its file.+ fn write_cookies(&self, cookies: &[u8]) -> Result<()> {+ use std::io::Write;// The session's cookies are a credential: readable by this user alone, whatever folder// they end up in (a backup, a copy), not only by the private data folder around them.let mut options = std::fs::OpenOptions::new();@@ -386,9 +437,9 @@ impl Client {file.set_permissions(std::fs::Permissions::from_mode(0o600))?;}let mut w = std::io::BufWriter::new(file);- let jar = self.jar.lock().expect("cookie jar mutex poisoned");- cookie_store::serde::json::save(&jar, &mut w)- .map_err(|e| Error::Protocol(format!("couldn't save cookies: {e}")))+ w.write_all(cookies)?;+ w.flush()?;+ Ok(())}/// Forget this account entirely.@@ -397,6 +448,11 @@ impl Client {if let Ok(mut jar) = self.jar.lock() {jar.clear();}+ // Nothing is stored any more: the next session is written whatever it is.+ *self+ .persisted+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Persisted::default();self.store.clear()}diff --git a/crates/quince-auth/tests/auth_flow.rs b/crates/quince-auth/tests/auth_flow.rsindex 221c85c..cf06464 100644--- a/crates/quince-auth/tests/auth_flow.rs+++ b/crates/quince-auth/tests/auth_flow.rs@@ -833,3 +833,55 @@ async fn a_sign_in_that_needs_a_code_asks_apple_to_send_it() {"from the icloud.com origin, not idmsa's");}++/// A heartbeat that changes nothing writes nothing: the keyring is not rewritten on every sync+/// and every return to the window. One that brings something new is written as before.+#[tokio::test]+async fn a_heartbeat_writes_the_session_only_when_it_changed() {+ let server = MockServer::start().await;+ mount_init(&server, 200).await;+ mount_complete(&server, 409).await;+ Mock::given(method("POST"))+ .and(path("/appleauth/auth/verify/trusteddevice/securitycode"))+ .respond_with(ResponseTemplate::new(204))+ .mount(&server)+ .await;+ mount_trust_and_login(&server).await;+ Mock::given(method("POST"))+ .and(path("/setup/ws/1/validate"))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({})))+ .up_to_n_times(2)+ .with_priority(1)+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path("/setup/ws/1/validate"))+ .respond_with(+ ResponseTemplate::new(200)+ .insert_header("scnt", "scnt-renewed")+ .set_body_json(json!({})),+ )+ .mount(&server)+ .await;++ let dir = tempfile::tempdir().unwrap();+ let mut client = client_for(&server, dir.path());+ client+ .sign_in("[email protected]", "hunter2")+ .await+ .unwrap();+ client.submit_two_factor("123456").await.unwrap();+ let stored = dir.path().join("session.json");+ assert!(stored.exists());++ // The first heartbeat of a run writes once; the next, with nothing new, does not.+ assert!(client.heartbeat().await.unwrap());+ std::fs::remove_file(&stored).unwrap();+ assert!(client.heartbeat().await.unwrap());+ assert!(!stored.exists(), "an unchanged session was written again");++ // Apple hands over something new: written.+ assert!(client.heartbeat().await.unwrap());+ let kept = std::fs::read_to_string(&stored).expect("a changed session is written");+ assert!(kept.contains("scnt-renewed"));+}diff --git a/crates/quince-core/Cargo.toml b/crates/quince-core/Cargo.tomlindex b21831e..bf8dca2 100644--- a/crates/quince-core/Cargo.toml+++ b/crates/quince-core/Cargo.toml@@ -6,7 +6,7 @@ rust-version.workspace = truelicense.workspace = true[dependencies]-rusqlite = { version = "0.32", features = ["bundled"] }+rusqlite = { version = "0.32", features = ["bundled", "hooks"] }jiff = "0.2.37"serde.workspace = truethiserror = "2"diff --git a/crates/quince-core/src/contacts.rs b/crates/quince-core/src/contacts.rsindex c41d3c8..07ceb14 100644--- a/crates/quince-core/src/contacts.rs+++ b/crates/quince-core/src/contacts.rs@@ -713,6 +713,17 @@ impl Library {////// The picture is served per row as a list scrolls, so this stays a single-row lookup/// rather than loading everyone and their phone numbers to answer it.+ /// Every contact with a picture at Apple and the signature naming it, `(guid, signature)`:+ /// what warming the picture cache needs to know, without building each card whole.+ pub fn contact_photo_signatures(&self) -> Result<Vec<(String, String)>> {+ let mut stmt = self.conn.prepare(+ "SELECT guid, photo_signature FROM contacts+ WHERE photo_url IS NOT NULL AND photo_signature IS NOT NULL",+ )?;+ let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;+ Ok(rows.collect::<rusqlite::Result<_>>()?)+ }+pub fn contact_photo_source(&self, guid: &str) -> Result<Option<ContactPhoto>> {let mut stmt = self.conn.prepare("SELECT photo_url, photo_signature, photo_crop FROM contacts WHERE guid = ?1",@@ -967,6 +978,34 @@ mod tests {}}+ /// Warming the picture cache asks only who has a picture and which: the same people the+ /// whole cards say have one, with the same signatures.+ #[test]+ fn picture_signatures_are_read_without_building_every_card() {+ let mut lib = Library::in_memory().unwrap();+ let mut alex = contact("1", Some("Alex"), Some("Rivera"));+ alex.photo_url = Some("https://example.invalid/a".into());+ alex.photo_signature = Some("sig-a".into());+ // A picture with no signature is fetched as it is drawn, never warmed.+ let mut bea = contact("2", Some("Bea"), Some("Moreno"));+ bea.photo_url = Some("https://example.invalid/b".into());+ let mut cy = contact("3", Some("Cy"), Some("Lane"));+ cy.photo_signature = Some("stale".into());+ lib.put_contacts(&[alex, bea, cy]).unwrap();+ let mut whole: Vec<(String, String)> = lib+ .contacts(None)+ .unwrap()+ .into_iter()+ .filter(|c| c.photo_url.is_some())+ .filter_map(|c| c.photo_signature.map(|s| (c.guid, s)))+ .collect();+ let mut quick = lib.contact_photo_signatures().unwrap();+ whole.sort();+ quick.sort();+ assert_eq!(quick, whole);+ assert_eq!(quick, [("1".to_string(), "sig-a".to_string())]);+ }+/// Calendar's read: the people with a birthday, in the address book's order, with what/// a birthday's title is made from — and whether there are any at all.#[test]diff --git a/crates/quince-core/src/lib.rs b/crates/quince-core/src/lib.rsindex 91012c8..6c28e52 100644--- a/crates/quince-core/src/lib.rs+++ b/crates/quince-core/src/lib.rs@@ -159,6 +159,51 @@ impl Library {}}+ /// Run `apply`, and say whether it changed any row outside the tables in `besides` — the+ /// bookkeeping a pass writes every time (where it got to, and when), whatever came back.+ ///+ /// A pass that brought nothing new still stores its position, so "rows were written" is+ /// true of every pass; "rows other than the position were written" is what tells a screen+ /// it has something to read again. Errs one way only: a change it cannot place is counted+ /// as a change.+ pub fn changing<T>(+ &mut self,+ besides: &'static [&'static str],+ apply: impl FnOnce(&mut Self) -> T,+ ) -> (T, bool) {+ use std::sync::atomic::{AtomicU64, Ordering};+ /// The hook taken off again however `apply` ends, a panic included.+ struct Unhook<'a>(&'a Connection);+ impl Drop for Unhook<'_> {+ fn drop(&mut self) {+ self.0+ .update_hook(None::<fn(rusqlite::hooks::Action, &str, &str, i64)>);+ }+ }+ let aside = std::sync::Arc::new(AtomicU64::new(0));+ let counted = std::sync::Arc::clone(&aside);+ self.conn.update_hook(Some(+ move |_: rusqlite::hooks::Action, _: &str, table: &str, _: i64| {+ if besides.contains(&table) {+ counted.fetch_add(1, Ordering::Relaxed);+ }+ },+ ));+ let before = self.conn.total_changes();+ let out = {+ // Not borrowed across `apply`: it takes the library whole. The hook is removed+ // just after, and on unwinding by the guard below.+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| apply(self)));+ let _unhook = Unhook(&self.conn);+ match out {+ Ok(out) => out,+ Err(panic) => std::panic::resume_unwind(panic),+ }+ };+ let written = self.conn.total_changes().saturating_sub(before);+ (out, written > aside.load(Ordering::Relaxed))+ }+pub fn schema_version(&self) -> Result<u32> {Ok(self.conn@@ -527,4 +572,28 @@ mod tests {assert_eq!(count, 1, "a re-sync must not duplicate rows");assert_eq!(title, "Standup (moved)");}++ /// A pass that only moved its position changed nothing a screen shows; one that stored a+ /// record did. Rolled back or not, a change it cannot place counts as one.+ #[test]+ fn only_rows_beyond_the_bookkeeping_count_as_a_change() {+ let mut library = Library::in_memory().unwrap();+ let ((), changed) = library.changing(&["sync_state"], |l| {+ l.mark_synced("notes", Some("token-2")).unwrap();+ l.mark_full_sync("notes").unwrap();+ });+ assert!(!changed, "a new position alone is not a change");++ let ((), changed) = library.changing(&["sync_state"], |l| {+ l.mark_synced("notes", Some("token-3")).unwrap();+ l.set_state("k", "v").unwrap();+ });+ assert!(changed, "a row stored is a change");++ // The hook is gone afterwards: nothing it counted leaks into the next call.+ let ((), changed) = library.changing(&[], |l| l.set_state("k", "w").unwrap());+ assert!(changed);+ let ((), changed) = library.changing(&["sync_state"], |_| {});+ assert!(!changed);+ }}diff --git a/crates/quince-icloud/src/photos.rs b/crates/quince-icloud/src/photos.rsindex a46be3c..3d1275b 100644--- a/crates/quince-icloud/src/photos.rs+++ b/crates/quince-icloud/src/photos.rs@@ -32,9 +32,15 @@ pub struct PhotosFetch {pub complete: bool,}-/// How many pages of the change feed one pass reads (500 records each). A library larger-/// than this is read over several passes, each carrying on where the last stopped.-const PASS_PAGES: usize = 100;+/// How many pages of the change feed one fetch reads (500 records each). A library larger+/// than this is read over several, each carrying on where the last stopped and each stored+/// before the next is asked for: a hundred pages at once held some fifty thousand records in+/// memory together through a whole walk.+const PASS_PAGES: usize = 10;++/// How many fetches one refresh makes before it leaves the rest of a walk to the next: the+/// hundred pages a refresh has always gone through, ten at a time.+pub const PASS_ROUNDS: usize = 10;/// One pass of the change feed from `since` (or from the start).///@@ -1920,4 +1926,61 @@ mod tests {.unwrap();assert!(again.incremental && again.from.as_deref() == Some("further"));}++ /// A whole walk read the way a refresh reads it — a few pages at a time, each stored+ /// before the next is asked for — ends with every photo, as one long fetch did, and never+ /// holds more than a few pages of records at once.+ #[tokio::test]+ async fn a_walk_read_a_few_pages_at_a_time_ends_whole() {+ use wiremock::matchers::{body_string_contains, method, path_regex};+ use wiremock::{Mock, MockServer};+ const PAGES: usize = 25;+ let server = MockServer::start().await;+ // Page `k` is asked for with the token page `k - 1` gave; the first with none.+ for k in (0..PAGES).rev() {+ let reply = zone_reply(json!({+ "records": [+ master_named(&format!("M{k}")),+ asset_named(&format!("A{k}"), &format!("M{k}"))+ ],+ "syncToken": format!("page-{}", k + 1),+ "moreComing": k + 1 < PAGES+ }));+ let mock = Mock::given(method("POST")).and(path_regex(r".*/changes/zone"));+ if k == 0 {+ mock.respond_with(reply).mount(&server).await;+ } else {+ mock.and(body_string_contains(format!("\"page-{k}\"")))+ .respond_with(reply)+ .with_priority(1)+ .mount(&server)+ .await;+ }+ }+ let http = reqwest::Client::new();+ let mut lib = quince_core::Library::in_memory().unwrap();+ let mut rounds = 0;+ loop {+ rounds += 1;+ let since = lib.sync_token(SERVICE).unwrap();+ let fetched = fetch(&http, &server.uri(), vec![], since.as_deref())+ .await+ .unwrap();+ assert!(+ fetched.records.len() <= 2 * PASS_PAGES,+ "one fetch holds a few pages"+ );+ let complete = fetched.complete;+ apply(&mut lib, fetched).unwrap();+ if complete || rounds >= PASS_ROUNDS {+ break;+ }+ }+ assert_eq!(rounds, PAGES.div_ceil(PASS_PAGES));+ assert_eq!(ids(&lib).len(), PAGES, "every photo, from every page");+ assert_eq!(+ lib.sync_token(SERVICE).unwrap(),+ Some(format!("page-{PAGES}"))+ );+ }}diff --git a/crates/quince-icloud/src/reminders.rs b/crates/quince-icloud/src/reminders.rsindex 00a8817..d964a8d 100644--- a/crates/quince-icloud/src/reminders.rs+++ b/crates/quince-icloud/src/reminders.rs@@ -737,37 +737,43 @@ pub fn memberships_from(bytes: &[u8]) -> Vec<(String, String)> {////// A file that will not download is skipped rather than failing the sync: the reminders are/// still right, and their sections simply stay as they were until the next pass.+///+/// Eight at a time, as Notes fetches its long notes: one after another, a walk over many lists+/// waited on each list's file in turn.async fn fetch_memberships(http: &reqwest::Client,records: &[Value],) -> std::collections::HashMap<String, Vec<(String, String)>> {- let mut out = std::collections::HashMap::new();- for record in records {- if field::record_type(record).as_deref() != Some("List") || field::flag(record, "Deleted") {- continue;- }- let Some(name) = field::record_name(record) else {- continue;- };- let Some(url) = record- .pointer("/fields/MembershipsOfRemindersInSectionsAsData/value/downloadURL")- .and_then(Value::as_str)- else {- continue;- };- // The URL is a template; Apple fills the file name in with whatever it is given.- let Ok(response) = http.get(url.replace("${f}", "memberships")).send().await else {- continue;- };- if !response.status().is_success() {- continue;- }- let Ok(bytes) = response.bytes().await else {- continue;- };- out.insert(name, memberships_from(&bytes));- }- out+ use futures_util::StreamExt;+ let wanted: Vec<(String, String)> = records+ .iter()+ .filter(|r| field::record_type(r).as_deref() == Some("List") && !field::flag(r, "Deleted"))+ .filter_map(|r| {+ let name = field::record_name(r)?;+ let url = r+ .pointer("/fields/MembershipsOfRemindersInSectionsAsData/value/downloadURL")+ .and_then(Value::as_str)?;+ Some((name, url.to_string()))+ })+ .collect();+ futures_util::stream::iter(wanted)+ .map(|(name, url)| async move {+ // The URL is a template; Apple fills the file name in with whatever it is given.+ let response = http+ .get(url.replace("${f}", "memberships"))+ .send()+ .await+ .ok()?;+ if !response.status().is_success() {+ return None;+ }+ let bytes = response.bytes().await.ok()?;+ Some((name, memberships_from(&bytes)))+ })+ .buffer_unordered(8)+ .filter_map(|found| async move { found })+ .collect()+ .await}/// Ask Apple what changed. Touches no local state.@@ -4930,6 +4936,64 @@ mod membership_tests {assert!(memberships_from(br#"{"memberships":"wrong"}"#).is_empty());assert!(memberships_from(br#"{"other":[]}"#).is_empty());}++ /// Every list's file is fetched, side by side rather than in turn; a deleted list and a+ /// file that will not come are left out, as before.+ #[tokio::test]+ async fn every_lists_file_is_fetched_together() {+ use serde_json::json;+ use wiremock::matchers::{method, path_regex};+ use wiremock::{Mock, MockServer, ResponseTemplate};+ let server = MockServer::start().await;+ let slow = std::time::Duration::from_millis(150);+ Mock::given(method("GET"))+ .and(path_regex(r"^/m/missing/"))+ .respond_with(ResponseTemplate::new(404))+ .mount(&server)+ .await;+ Mock::given(method("GET"))+ .and(path_regex(r"^/m/"))+ .respond_with(+ ResponseTemplate::new(200)+ .set_body_string(r#"{"memberships":[{"memberID":"R1","groupID":"S1"}]}"#)+ .set_delay(slow),+ )+ .mount(&server)+ .await;+ let list = |name: &str, deleted: bool| {+ json!({+ "recordType": "List",+ "recordName": name,+ "fields": {+ "Deleted": { "value": i64::from(deleted) },+ "MembershipsOfRemindersInSectionsAsData": {+ "value": { "downloadURL": format!("{}/m/{name}/${{f}}", server.uri()) }+ }+ }+ })+ };+ let mut records: Vec<Value> = (0..12).map(|i| list(&format!("L{i}"), false)).collect();+ records.push(list("gone", true));+ records.push(list("missing", false));+ let http = reqwest::Client::new();+ let started = std::time::Instant::now();+ let found = fetch_memberships(&http, &records).await;+ let took = started.elapsed();+ assert_eq!(found.len(), 12);+ assert!(+ found.contains_key("L0")+ && !found.contains_key("gone")+ && !found.contains_key("missing")+ );+ assert_eq!(+ found["L3"],+ [("Reminder/R1".to_string(), "ListSection/S1".to_string())]+ );+ assert!(+ took < slow * 6,+ "twelve files {slow:?} each took {took:?}: fetched in turn"+ );+ }}/// Where lists and groups sit, as the phone has them.diff --git a/crates/quince-icloud/src/sync.rs b/crates/quince-icloud/src/sync.rsindex be1cbc1..1fe9365 100644--- a/crates/quince-icloud/src/sync.rs+++ b/crates/quince-icloud/src/sync.rs@@ -14,6 +14,10 @@ use quince_core::Library;/// two months is not.const MAX_WINDOW_DAYS: i32 = 28;+/// How many of those windows are asked about at once: enough that a refresh is a couple of+/// round trips rather than a dozen, few enough that Apple is not flooded.+const CALENDAR_WINDOWS_AT_ONCE: usize = 4;+fn parse_date(value: &str) -> Result<Date> {value.parse::<Date>()@@ -81,16 +85,30 @@ pub async fn fetch_calendar(let mut events = Vec::new();let mut seen = std::collections::HashSet::new();- for (start, end) in &windows {- // `/ca/startup` reliably carries the collections but not always the events — on some- // accounts it returns collections alone. Asking `/ca/events` for the same window is- // what guarantees them, so do both and merge.- let startup = client.startup(start, end, timezone).await?;+ // `/ca/startup` reliably carries the collections but not always the events — on some+ // accounts it returns collections alone. Asking `/ca/events` for the same window is what+ // guarantees them, so do both and merge.+ //+ // Every window's two questions are asked together, a few windows at a time, rather than+ // one after another: five months was a dozen round trips in a row on every change Apple+ // reported. The answers are taken in the windows' order, so what is kept from two that+ // overlap is what it always was.+ use futures_util::StreamExt;+ let client = &client;+ let mut answers = futures_util::stream::iter(windows)+ .map(|(start, end)| async move {+ futures_util::future::try_join(+ client.startup(&start, &end, timezone),+ client.events(&start, &end, timezone),+ )+ .await+ })+ .buffered(CALENDAR_WINDOWS_AT_ONCE);+ while let Some(answer) = answers.next().await {+ let (startup, windowed) = answer?;if calendars.is_empty() {calendars = startup.calendars;}- let windowed = client.events(start, end, timezone).await?;-for event in startup.events.into_iter().chain(windowed.events) {if seen.insert(event.guid.clone()) {events.push(event);@@ -427,6 +445,66 @@ mod tests {}}+ /// Five months is six windows of two questions each. Asked side by side a few windows at+ /// a time, the whole takes a couple of Apple's answers, not twelve in a row — and gives+ /// the same calendars and events, each once.+ #[tokio::test]+ async fn the_windows_are_asked_about_together() {+ let server = MockServer::start().await;+ let slow = std::time::Duration::from_millis(150);+ Mock::given(method("GET"))+ .and(path("/ca/startup"))+ .respond_with(+ ResponseTemplate::new(200)+ .set_body_json(body())+ .set_delay(slow),+ )+ .mount(&server)+ .await;+ Mock::given(method("GET"))+ .and(path("/ca/events"))+ .respond_with(+ ResponseTemplate::new(200)+ .set_body_json(json!({ "Event": body()["Event"].clone() }))+ .set_delay(slow),+ )+ .mount(&server)+ .await;+ let http = reqwest::Client::new();+ let started = std::time::Instant::now();+ let snapshot = fetch_calendar(+ &http,+ &server.uri(),+ vec![],+ "2026-08-01",+ "2026-12-31",+ "Asia/Kolkata",+ )+ .await+ .unwrap();+ let took = started.elapsed();+ let asked = server.received_requests().await.unwrap().len();+ assert_eq!(+ asked,+ 2 * chunks(+ parse_date("2026-08-01").unwrap(),+ parse_date("2026-12-31").unwrap()+ )+ .len()+ );+ assert!(+ took < slow * 6,+ "{asked} questions answered {slow:?} apart took {took:?}: they waited on each other"+ );+ assert_eq!(snapshot.calendars.len(), 1);+ let guids: Vec<&str> = snapshot.events.iter().map(|e| e.guid.as_str()).collect();+ assert_eq!(+ guids,+ ["ev-1", "ev-2"],+ "each event once, in the order first seen"+ );+ }+/// A save answers with nothing (measured on the account); what is stored is Apple's own/// copy, read back, with the etag the next edit or delete needs.#[tokio::test]diff --git a/crates/quince-vfs/src/fuse.rs b/crates/quince-vfs/src/fuse.rsindex c11ed47..010a8f0 100644--- a/crates/quince-vfs/src/fuse.rs+++ b/crates/quince-vfs/src/fuse.rs@@ -67,6 +67,9 @@ struct Inner {looked: Mutex<std::collections::HashMap<String, std::time::Instant>>,/// The account's storage as last asked, `(used, total)`.space: Mutex<Option<((u64, u64), std::time::Instant)>>,+ /// Each folder's listing as last worked out, by the folder's inode, kept while nothing it+ /// was worked out from has moved (see `Listing`).+ listings: Mutex<std::collections::HashMap<u64, Arc<Listing>>>,/// Nothing in it can be changed.read_only: bool,/// New files can be added and anything deleted, nothing else (the Photos folder: a photo is@@ -76,6 +79,45 @@ struct Inner {pub struct DriveFs(Arc<Inner>);+/// What a pending file in a folder brings to its listing: its inode, its name, and the file of+/// Apple's it takes the place of.+type LocalKey = (u64, String, Option<DriveItem>);++/// A folder's listing — names, inodes and items — and everything it was worked out from: the+/// store's version, the inode table's generation and the files being written into it. While all+/// three stand, working it out again gives the very same listing, so it is used as it is.+///+/// A file manager listing a folder looks every name in it up after, and each look-up worked the+/// whole folder out again — read from the library, named, placed — to find one name: a folder of+/// two thousand cost four million rows, every second the kernel's answer lasted.+struct Listing {+ version: u64,+ generation: u64,+ local: Vec<LocalKey>,+ entries: Vec<(String, u64, DriveItem)>,+ /// Where in `entries` each name is first.+ by_name: std::collections::HashMap<String, usize>,+}++impl Listing {+ fn of(entries: Vec<(String, u64, DriveItem)>) -> Self {+ let mut by_name = std::collections::HashMap::with_capacity(entries.len());+ for (at, (name, ..)) in entries.iter().enumerate() {+ by_name.entry(name.clone()).or_insert(at);+ }+ Self {+ version: 0,+ generation: 0,+ local: Vec::new(),+ entries,+ by_name,+ }+ }+}++/// The most folders whose listings are kept at once.+const KEPT_LISTINGS: usize = 256;+/// The attribute that keeps a file or folder on this computer: `setfattr -n user.quince.keep/// -v 1 <path>` keeps it, `-x` lets it go — what the window's "Keep on This Computer" does.const KEEP_ATTR: &str = "user.quince.keep";@@ -129,6 +171,7 @@ impl DriveFs {writes: None,space: Mutex::new(None),looked: Mutex::new(std::collections::HashMap::new()),+ listings: Mutex::new(std::collections::HashMap::new()),keeping: std::sync::atomic::AtomicBool::new(false),again: std::sync::atomic::AtomicBool::new(false),read_only: false,@@ -566,13 +609,60 @@ impl Inner {/// A folder's items with their names and inodes: from the library — or, never opened/// before, from Apple once — with the files being written here among them.fn children(&self, ino: u64) -> Result<Vec<(String, u64, DriveItem)>, Errno> {+ Ok(self.listing(ino)?.entries.clone())+ }++ /// The folder's listing: the one worked out last while nothing it came from has moved,+ /// else worked out again (see `Listing`).+ fn listing(&self, ino: u64) -> Result<Arc<Listing>, Errno> {+ // Asked first: a change landing while this works the listing out moves it past what is+ // recorded here, and the next look works it out again.+ let version = self.store.version();let folder = self.item(ino).ok_or(Errno::ENOENT)?;if !folder.is_container() {return Err(Errno::ENOTDIR);}+ // Files being written here: a new one is shown beside Apple's; one taking another's+ // place is shown in it, under its own inode.+ let mut local: Vec<(u64, Pending)> = Vec::new();+ if let Some(w) = &self.writes {+ let pending = w+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ local = pending+ .iter()+ .filter(|(_, p)| p.folder == folder.drivewsid)+ .map(|(i, p)| (*i, p.clone()))+ .collect();+ }+ local.sort_by_key(|(i, _)| *i);+ let local_key: Vec<LocalKey> = local+ .iter()+ .map(|(i, p)| (*i, p.name.clone(), p.replaces.clone()))+ .collect();+ if let Some(version) = version {+ let generation = self+ .inodes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .generation();+ let kept = self+ .listings+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .get(&ino)+ .cloned();+ if let Some(kept) = kept.filter(|l| {+ l.version == version && l.generation == generation && l.local == local_key+ }) {+ return Ok(kept);+ }+ }let stored = self.store.children(&folder.drivewsid);// A folder Apple says has things in it, with nothing stored, was never listed.let listed = stored.filter(|s| !s.is_empty() || folder.child_count == Some(0));+ let from_store = listed.is_some();let mut items = match listed {Some(items) => items,None => {@@ -586,20 +676,6 @@ impl Inner {.map_err(|_| Errno::EIO)?}};- // Files being written here: a new one is shown beside Apple's; one taking another's- // place is shown in it, under its own inode.- let mut local: Vec<(u64, Pending)> = Vec::new();- if let Some(w) = &self.writes {- let pending = w- .items- .lock()- .unwrap_or_else(std::sync::PoisonError::into_inner);- local = pending- .iter()- .filter(|(_, p)| p.folder == folder.drivewsid)- .map(|(i, p)| (*i, p.clone()))- .collect();- }let mut inodes = self.inodes.lock()@@ -634,16 +710,38 @@ impl Inner {inodes.place(pino, ino, &p.name);out.push((p.name.clone(), pino, it));}- Ok(out)+ let generation = inodes.generation();+ drop(inodes);+ let mut listing = Listing::of(out);+ // Kept only when it came from the store: one read from Apple was stored on the way, which+ // moves the store's version, and is worked out again from there next time.+ if let (Some(version), true) = (version, from_store) {+ listing.version = version;+ listing.generation = generation;+ listing.local = local_key;+ let listing = Arc::new(listing);+ let mut kept = self+ .listings+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if kept.len() >= KEPT_LISTINGS && !kept.contains_key(&ino) {+ kept.clear();+ }+ kept.insert(ino, Arc::clone(&listing));+ return Ok(listing);+ }+ Ok(Arc::new(listing))}/// The item called `name` in folder `parent`.fn child(&self, parent: u64, name: &OsStr) -> Result<(u64, DriveItem), Errno> {- self.children(parent)?- .into_iter()- .find(|(n, ..)| OsStr::new(n) == name)- .map(|(_, ino, item)| (ino, item))- .ok_or(Errno::ENOENT)+ let listing = self.listing(parent)?;+ let at = *name+ .to_str()+ .and_then(|n| listing.by_name.get(n))+ .ok_or(Errno::ENOENT)?;+ let (_, ino, item) = &listing.entries[at];+ Ok((*ino, item.clone()))}/// A file of Apple's made writable: its bytes copied here (unless they are about to be@@ -1791,3 +1889,122 @@ pub fn mount(fs: DriveFs, at: &Path) -> std::io::Result<fuser::BackgroundSessionInner::settle_kept(&inner);Ok(session)}++#[cfg(test)]+mod listing_tests {+ use super::*;+ use crate::{Fut, ROOT};+ use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};++ struct Apple;+ impl Remote for Apple {+ fn list(&self, _folder: &str) -> Fut<Vec<DriveItem>> {+ Box::pin(async { Ok(Vec::new()) })+ }+ fn read(&self, _item: &DriveItem, _offset: u64, _len: u64) -> Fut<Vec<u8>> {+ Box::pin(async { Ok(Vec::new()) })+ }+ }++ /// A library that counts how often it is read, and whose version a test moves.+ struct Library {+ items: Mutex<Vec<DriveItem>>,+ reads: AtomicUsize,+ version: AtomicU64,+ }+ impl Store for Library {+ fn children(&self, folder: &str) -> Option<Vec<DriveItem>> {+ self.reads.fetch_add(1, Ordering::SeqCst);+ (folder == ROOT).then(|| self.items.lock().unwrap().clone())+ }+ fn version(&self) -> Option<u64> {+ Some(self.version.load(Ordering::SeqCst))+ }+ }++ fn file(n: usize) -> DriveItem {+ DriveItem {+ drivewsid: format!("FILE::{n}"),+ parent_id: Some(ROOT.into()),+ name: format!("Scan {n}"),+ extension: Some("pdf".into()),+ kind: "FILE".into(),+ size: Some(10),+ child_count: None,+ created_at: Some(1_700_000_000),+ modified_at: Some(1_700_000_000),+ etag: Some("e1".into()),+ }+ }++ /// A file manager listing a folder of many files looks each name up after: the folder is+ /// read from the library once for all of them, while nothing in it changes — and again,+ /// with the change, once something does.+ #[test]+ fn a_folders_names_are_looked_up_without_reading_it_again() {+ let rt = tokio::runtime::Runtime::new().unwrap();+ let cache = tempfile::tempdir().unwrap();+ let library = Arc::new(Library {+ items: Mutex::new((0..2000).map(file).collect()),+ reads: AtomicUsize::new(0),+ version: AtomicU64::new(1),+ });+ let fs = DriveFs::new(+ Arc::new(Apple),+ Arc::clone(&library) as Arc<dyn Store>,+ Arc::new(Cache::new(cache.path()).unwrap()),+ rt.handle().clone(),+ );+ let inner = &fs.0;+ let listed = inner.children(1).unwrap();+ assert_eq!(listed.len(), 2000);+ for (name, ino, item) in &listed {+ let (found, it) = inner.child(1, OsStr::new(name)).unwrap();+ assert_eq!((found, &it), (*ino, item));+ }+ assert!(matches!(+ inner.child(1, OsStr::new("desktop.ini")),+ Err(Errno::ENOENT)+ ));+ assert_eq!(+ library.reads.load(Ordering::SeqCst),+ 1,+ "two thousand look-ups, one read"+ );++ // A file saved elsewhere lands in the library: the next look sees it.+ library.items.lock().unwrap().push(file(2000));+ library.version.fetch_add(1, Ordering::SeqCst);+ assert!(inner.child(1, OsStr::new("Scan 2000.pdf")).is_ok());+ assert_eq!(library.reads.load(Ordering::SeqCst), 2);+ // The same answers as working it out from nothing.+ let again = inner.children(1).unwrap();+ assert_eq!(again.len(), 2001);+ assert_eq!(&again[..2000], &listed[..]);+ }++ /// Without a version to go by, every look reads the library, as it always did.+ #[test]+ fn a_store_without_a_version_is_read_every_time() {+ struct Plain(Vec<DriveItem>, AtomicUsize);+ impl Store for Plain {+ fn children(&self, _folder: &str) -> Option<Vec<DriveItem>> {+ self.1.fetch_add(1, Ordering::SeqCst);+ Some(self.0.clone())+ }+ }+ let rt = tokio::runtime::Runtime::new().unwrap();+ let cache = tempfile::tempdir().unwrap();+ let store = Arc::new(Plain(vec![file(1)], AtomicUsize::new(0)));+ let fs = DriveFs::new(+ Arc::new(Apple),+ Arc::clone(&store) as Arc<dyn Store>,+ Arc::new(Cache::new(cache.path()).unwrap()),+ rt.handle().clone(),+ );+ for _ in 0..3 {+ assert!(fs.0.child(1, OsStr::new("Scan 1.pdf")).is_ok());+ }+ assert_eq!(store.1.load(Ordering::SeqCst), 3);+ }+}diff --git a/crates/quince-vfs/src/lib.rs b/crates/quince-vfs/src/lib.rsindex 670c310..468af7a 100644--- a/crates/quince-vfs/src/lib.rs+++ b/crates/quince-vfs/src/lib.rs@@ -78,6 +78,12 @@ pub trait Remote: Send + Sync + 'static {pub trait Store: Send + Sync + 'static {/// A folder's items as last stored; `None` when it has never been listed.fn children(&self, folder: &str) -> Option<Vec<DriveItem>>;+ /// A number that moves whenever anything [`Store::children`] answers may have changed, and+ /// only then — so a folder's listing worked out once can be used again while it stays.+ /// `None` (the default): not known, and every look reads the store again.+ fn version(&self) -> Option<u64> {+ None+ }}/// The root of iCloud Drive, as Apple names it.@@ -92,6 +98,9 @@ pub struct Inodes {/// Where each item is in the folder, as the file manager names it ("Work/Deck.key").paths: HashMap<u64, String>,next: u64,+ /// Moves on every change to the table — never for a call that changed nothing — so what was+ /// worked out from it can be told for still current.+ generation: u64,}impl Inodes {@@ -121,8 +130,9 @@ impl Inodes {/// The inode of an item, given one the first time it is seen; its details kept fresh.pub fn ino(&mut self, item: &DriveItem) -> u64 {if let Some(&ino) = self.by_id.get(&item.drivewsid) {- if ino != 1 {+ if ino != 1 && self.by_ino.get(&ino) != Some(item) {self.by_ino.insert(ino, item.clone());+ self.generation += 1;}return ino;}@@ -130,9 +140,16 @@ impl Inodes {self.next += 1;self.by_id.insert(item.drivewsid.clone(), ino);self.by_ino.insert(ino, item.clone());+ self.generation += 1;ino}+ /// Where the table is in its changes: the same number, the same table.+ #[must_use]+ pub fn generation(&self) -> u64 {+ self.generation+ }+/// Where an item is, from the folder's top, as last listed.#[must_use]pub fn path(&self, ino: u64) -> Option<&str> {@@ -149,7 +166,10 @@ impl Inodes {Some(p) => format!("{p}/{name}"),None => return,};- self.paths.insert(ino, at);+ if self.paths.get(&ino) != Some(&at) {+ self.paths.insert(ino, at);+ self.generation += 1;+ }}/// An inode for a file made on this computer and not yet at Apple: its own id@@ -175,6 +195,7 @@ impl Inodes {};self.by_id.insert(item.drivewsid.clone(), ino);self.by_ino.insert(ino, item);+ self.generation += 1;ino}@@ -192,6 +213,7 @@ impl Inodes {}}self.by_ino.insert(ino, item.clone());+ self.generation += 1;}/// The inode an item already has, by Apple's id.diff --git a/src-tauri/src/contacts_cmds.rs b/src-tauri/src/contacts_cmds.rsindex b9a8242..5b7ba9d 100644--- a/src-tauri/src/contacts_cmds.rs+++ b/src-tauri/src/contacts_cmds.rs@@ -11,8 +11,10 @@ pub async fn contact_addresses(guid: String,data: State<'_, Data>,) -> UiResult<Vec<PostalAddress>> {- let library = data.0.lock().await;- library.contact_addresses(&guid).map_err(|e| e.to_string())+ data.0+ .read(|library| library.contact_addresses(&guid))+ .await+ .map_err(|e| e.to_string())}/// Change a card's addresses: set a part, set a label, add one or take one out — any numberdiff --git a/src-tauri/src/contacts_merge.rs b/src-tauri/src/contacts_merge.rsindex 8bdee42..5f266a6 100644--- a/src-tauri/src/contacts_merge.rs+++ b/src-tauri/src/contacts_merge.rs@@ -17,7 +17,6 @@ use quince_icloud::Error;use serde::{Deserialize, Serialize};use std::time::Duration;use tauri::State;-use tokio::sync::Mutex;/// How long a merge waits before it is sent: the time its Undo is on screen.const HOLD: i64 = 10;@@ -225,7 +224,7 @@ fn unreached(e: &Error) -> bool {/// Send a merge, on Apple's own cards. See the module's notes for why each step is safe to/// take twice.pub async fn send(- library: &Mutex<Library>,+ library: &crate::library_lock::LibraryLock,http: &reqwest::Client,root: &str,params: Vec<(String, String)>,@@ -394,8 +393,11 @@ pub fn encode_photo(bytes: &[u8], limit: usize) -> Option<(String, f64)> {/// Cards that look like one person: the same name, or a number or email they share.#[tauri::command]pub async fn contacts_duplicates(data: State<'_, Data>) -> UiResult<Vec<Duplicates>> {- let library = data.0.lock().await;- let book = library.contacts(None).map_err(|e| e.to_string())?;+ let book = data+ .0+ .read(|library| library.contacts(None))+ .await+ .map_err(|e| e.to_string())?;Ok(core_merge::find_duplicates(&book))}@@ -657,7 +659,7 @@ mod tests {.mount(&server).await;- let lib = Mutex::new(library());+ let lib = crate::library_lock::LibraryLock::new(library(), None);let kept = record(&mut *lib.lock().await, &["A".into(), "B".into()]).unwrap();assert_eq!(kept, "B", "the card holding the most");let p = lib.lock().await.outbox_pending(CONTACTS).unwrap().remove(0);diff --git a/src-tauri/src/library_lock.rs b/src-tauri/src/library_lock.rsnew file mode 100644index 0000000..9a815c4--- /dev/null+++ b/src-tauri/src/library_lock.rs@@ -0,0 +1,348 @@+//! The library as every command shares it: one connection that writes, behind one lock, and+//! read-only connections beside it for the screens.+//!+//! A sync pass stores what it fetched with the library held: a full Notes or Photos pass, or a+//! first sync, holds it for seconds. Every screen used to read through that same lock, so+//! Notes, Reminders and Contacts drew nothing — and an autosave queued — until the pass was in.+//! WAL lets a second connection read what was committed while the writer works, so a read+//! goes to one of those instead whenever only a pass stands in its way.+//!+//! Only a pass. A change made in the window (a note saved, a reminder ticked) still comes+//! first: while anything other than a pass holds the lock or waits for it, a read waits too,+//! in turn, exactly as it always did — so a read asked for after a save never shows the+//! library from before it. A pass is one transaction (`Library::atomically`), so a reader sees+//! all of it or none of it, never half.++use quince_core::Library;+use std::ops::{Deref, DerefMut};+use std::path::{Path, PathBuf};+use std::sync::atomic::{AtomicUsize, Ordering};+use tokio::sync::{Mutex, MutexGuard};++/// The most read-only connections kept open between reads. More are opened when more reads+/// run at once; past this they are closed after use rather than kept.+const KEPT_READERS: usize = 4;++/// The shared library: the connection that writes, and the readers beside it.+pub struct LibraryLock {+ library: Mutex<Library>,+ /// Holders of the lock, and those waiting for it, that are not a sync pass: while there is+ /// one, a read waits its turn on the lock rather than reading beside it.+ others: AtomicUsize,+ /// Where the library's file is; `None` (an in-memory library, in tests) reads through the+ /// lock alone.+ dir: Option<PathBuf>,+ readers: std::sync::Mutex<Vec<Reader>>,+}++/// A read-only connection, and which file it was opened on: an account's library set aside+/// for another's moves the file under it, and a reader on the old one is not reused.+pub struct Reader {+ id: (u64, u64),+ library: Library,+}++impl Reader {+ /// A read-only connection on the library under `dir`, if it can be had.+ pub fn open(dir: &Path) -> Option<Self> {+ let id = identity(dir)?;+ let library = Library::open_reader(dir).ok()?;+ Some(Self { id, library })+ }++ /// Still on the file that is the library now.+ pub fn current(&self, dir: &Path) -> bool {+ identity(dir) == Some(self.id)+ }++ pub fn library(&self) -> &Library {+ &self.library+ }++ /// A number that moves each time another connection commits a change to the library, and+ /// only then (SQLite's `data_version`): what this reader last read is still what is there+ /// for as long as it stands.+ pub fn data_version(&self) -> Option<i64> {+ self.library+ .conn()+ .query_row("PRAGMA data_version", [], |r| r.get(0))+ .ok()+ }+}++fn identity(dir: &Path) -> Option<(u64, u64)> {+ let file = dir.join("library.db");+ std::fs::metadata(&file)+ .ok()+ .map(|m| crate::file_identity(&m))+}++/// The library held, as `tokio::sync::MutexGuard` holds it; counted, while it is held or+/// waited for, as something a read has to wait behind.+pub struct Held<'a> {+ guard: MutexGuard<'a, Library>,+ _turn: Turn<'a>,+}++impl Deref for Held<'_> {+ type Target = Library;+ fn deref(&self) -> &Library {+ &self.guard+ }+}++impl DerefMut for Held<'_> {+ fn deref_mut(&mut self) -> &mut Library {+ &mut self.guard+ }+}++/// One holder or waiter counted for as long as it lives, however it ends — dropped while still+/// waiting (its task cancelled) included.+struct Turn<'a>(&'a AtomicUsize);++impl<'a> Turn<'a> {+ fn take(count: &'a AtomicUsize) -> Self {+ count.fetch_add(1, Ordering::AcqRel);+ Self(count)+ }+}++impl Drop for Turn<'_> {+ fn drop(&mut self) {+ self.0.fetch_sub(1, Ordering::AcqRel);+ }+}++impl LibraryLock {+ pub fn new(library: Library, dir: Option<PathBuf>) -> Self {+ Self {+ library: Mutex::new(library),+ others: AtomicUsize::new(0),+ dir,+ readers: std::sync::Mutex::new(Vec::new()),+ }+ }++ /// The library, to change it or to read it in turn with changes.+ pub async fn lock(&self) -> Held<'_> {+ let turn = Turn::take(&self.others);+ let guard = self.library.lock().await;+ Held { guard, _turn: turn }+ }++ /// [`Self::lock`], from a thread that is not one of the runtime's (a folder's own threads).+ pub fn blocking_lock(&self) -> Held<'_> {+ let turn = Turn::take(&self.others);+ let guard = self.library.blocking_lock();+ Held { guard, _turn: turn }+ }++ pub fn try_lock(&self) -> Result<Held<'_>, tokio::sync::TryLockError> {+ let turn = Turn::take(&self.others);+ let guard = self.library.try_lock()?;+ Ok(Held { guard, _turn: turn })+ }++ /// The library, for a sync pass to store what it fetched as one transaction: reads carry on+ /// beside it on their own connections, seeing the library as it was until the pass is in.+ /// Only for a store made through `Library::atomically`, so no reader sees half of one.+ pub async fn lock_for_pass(&self) -> MutexGuard<'_, Library> {+ self.library.lock().await+ }++ /// Read the library: beside a pass on a read-only connection, or — while a change of the+ /// window's is held or waiting, or no reader can be had — through the lock, in turn.+ ///+ /// A read that fails on a reader is asked again through the lock, so a reader never makes+ /// a read fail that would not have failed before.+ pub async fn read<T>(+ &self,+ read: impl Fn(&Library) -> quince_core::Result<T>,+ ) -> quince_core::Result<T> {+ if let Some(Ok(found)) = self.beside(&read) {+ return Ok(found);+ }+ read(&*self.lock().await)+ }++ /// [`Self::read`], from a thread that is not one of the runtime's.+ pub fn blocking_read<T>(+ &self,+ read: impl Fn(&Library) -> quince_core::Result<T>,+ ) -> quince_core::Result<T> {+ if let Some(Ok(found)) = self.beside(&read) {+ return Ok(found);+ }+ read(&self.blocking_lock())+ }++ /// The read on a reader, when nothing but a pass stands in its way; `None` when it has to+ /// wait its turn.+ fn beside<T>(+ &self,+ read: &impl Fn(&Library) -> quince_core::Result<T>,+ ) -> Option<quince_core::Result<T>> {+ if self.others.load(Ordering::Acquire) != 0 {+ return None;+ }+ let dir = self.dir.as_deref()?;+ let reader = self.take_reader(dir)?;+ let found = read(reader.library());+ self.give_back(reader);+ Some(found)+ }++ fn take_reader(&self, dir: &Path) -> Option<Reader> {+ let kept = {+ let mut readers = self+ .readers+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ readers.pop()+ };+ match kept {+ Some(r) if r.current(dir) => Some(r),+ // On a file that is not the library any more: let go, and a fresh one opened.+ _ => Reader::open(dir),+ }+ }++ fn give_back(&self, reader: Reader) {+ // Where an open file cannot be moved (Windows), none is kept open between reads: an+ // account's library set aside would otherwise fail to move.+ if cfg!(not(unix)) {+ return;+ }+ let mut readers = self+ .readers+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if readers.len() < KEPT_READERS {+ readers.push(reader);+ }+ }++ /// Let go of every kept reader: before the library's files are moved.+ pub fn forget_readers(&self) {+ self.readers+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .clear();+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::sync::Arc;++ fn library(dir: &Path) -> LibraryLock {+ LibraryLock::new(Library::open(dir).unwrap(), Some(dir.to_path_buf()))+ }++ fn state(l: &Library) -> quince_core::Result<Option<String>> {+ l.state("k")+ }++ /// A read while a pass holds the library answers at once, with the library as it was+ /// before the pass — never waiting for it, and never half of it.+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]+ async fn a_read_never_waits_on_a_pass() {+ let dir = tempfile::tempdir().unwrap();+ let shared = Arc::new(library(dir.path()));+ shared.lock().await.set_state("k", "before").unwrap();++ let mut pass = shared.lock_for_pass().await;+ pass.conn().execute_batch("SAVEPOINT p").unwrap();+ pass.set_state("k", "during").unwrap();++ let read = tokio::time::timeout(Duration::from_secs(2), shared.read(state))+ .await+ .expect("the read waited for the pass")+ .unwrap();+ assert_eq!(read.as_deref(), Some("before"));++ pass.conn().execute_batch("RELEASE p").unwrap();+ drop(pass);+ assert_eq!(shared.read(state).await.unwrap().as_deref(), Some("during"));+ }++ /// A change of the window's waiting behind a pass keeps its turn: a read asked for after+ /// it waits too, and sees it, as it did when everything read through the lock.+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]+ async fn a_read_after_a_change_sees_the_change() {+ let dir = tempfile::tempdir().unwrap();+ let shared = Arc::new(library(dir.path()));+ shared.lock().await.set_state("k", "before").unwrap();++ let pass = shared.lock_for_pass().await;+ let writer = {+ let shared = Arc::clone(&shared);+ tokio::spawn(async move {+ shared.lock().await.set_state("k", "saved").unwrap();+ })+ };+ // The change is queued behind the pass.+ while shared.others.load(Ordering::Acquire) == 0 {+ tokio::task::yield_now().await;+ }+ let reader = {+ let shared = Arc::clone(&shared);+ tokio::spawn(async move { shared.read(state).await.unwrap() })+ };+ tokio::time::sleep(Duration::from_millis(50)).await;+ assert!(!reader.is_finished(), "the read went ahead of the change");+ drop(pass);+ writer.await.unwrap();+ assert_eq!(reader.await.unwrap().as_deref(), Some("saved"));+ }++ /// A reader on a file that is no longer the library (an account's set aside, another's put+ /// in its place) is not used: the read is of the library that is there now.+ #[tokio::test]+ async fn a_reader_follows_the_library_to_a_new_file() {+ let dir = tempfile::tempdir().unwrap();+ let shared = library(dir.path());+ shared.lock().await.set_state("k", "first").unwrap();+ assert_eq!(shared.read(state).await.unwrap().as_deref(), Some("first"));++ let elsewhere = tempfile::tempdir().unwrap();+ {+ let mut held = shared.lock().await;+ *held = Library::in_memory().unwrap();+ for name in ["library.db", "library.db-wal", "library.db-shm"] {+ let _ = std::fs::rename(dir.path().join(name), elsewhere.path().join(name));+ }+ *held = Library::open(dir.path()).unwrap();+ held.set_state("k", "second").unwrap();+ }+ assert_eq!(shared.read(state).await.unwrap().as_deref(), Some("second"));+ }++ /// The reader's version moves when the writer commits, and stands while it does not: the+ /// Drive folder keeps a listing for exactly as long as it stands.+ #[test]+ fn a_readers_version_moves_with_each_commit_and_only_then() {+ let dir = tempfile::tempdir().unwrap();+ let mut writer = Library::open(dir.path()).unwrap();+ let reader = Reader::open(dir.path()).unwrap();+ let first = reader.data_version().unwrap();+ assert_eq!(reader.data_version(), Some(first), "nothing committed");+ writer.set_state("k", "v").unwrap();+ let second = reader.data_version().unwrap();+ assert_ne!(second, first, "a commit moves it");+ assert_eq!(reader.data_version(), Some(second));+ // A pass's whole transaction moves it once it is in, not before.+ writer+ .atomically(|l| {+ l.set_state("k", "w")?;+ assert_eq!(reader.data_version(), Some(second), "not while it is open");+ Ok::<_, quince_core::Error>(())+ })+ .unwrap();+ assert_ne!(reader.data_version(), Some(second));+ }++ use std::time::Duration;+}diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rsindex 2449bb2..1e105c6 100644--- a/src-tauri/src/main.rs+++ b/src-tauri/src/main.rs@@ -21,6 +21,7 @@ mod cpu;mod export;mod grants;mod intake;+mod library_lock;mod login;mod migrate;mod mount;@@ -114,7 +115,7 @@ struct Status {library_ready: bool,}-struct Data(Mutex<Library>);+struct Data(library_lock::LibraryLock);/// Errors cross to the UI as plain sentences a person can act on.type UiResult<T> = Result<T, String>;@@ -158,11 +159,28 @@ pub static CALENDAR_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new/// edit, made from it, was refused as a conflict.pub static CONTACTS_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());-/// One Drive folder fetched and stored at a time. A folder opened just before a rename is-/// fetched with the old name; finishing after the rename's own re-read, it stored that old-/// listing last and the rename looked undone. Held from fetch to store, the re-read after a-/// write — which starts only once the write is made — always lands last.-pub static DRIVE_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());+/// One fetch and store of a Drive folder at a time, per folder. A folder opened just before a+/// rename is fetched with the old name; finishing after the rename's own re-read, it stored+/// that old listing last and the rename looked undone. Held from fetch to store, the re-read+/// after a write — which starts only once the write is made — always lands last.+///+/// Per folder, not one for all of Drive: one folder's slow answer held up every other folder+/// opened, the window's ten-second look and the Drive folder's listings behind it. The race+/// is a folder's own. A move touches two, and every move re-reads the folder it left before+/// the one it went to — each re-read waiting out an older look at its own folder — so the+/// destination's, which files the item where it now is, is still the last stored.+pub fn drive_pass(folder: &str) -> Arc<tokio::sync::Mutex<()>> {+ static PASSES: std::sync::Mutex<+ Option<std::collections::HashMap<String, Arc<tokio::sync::Mutex<()>>>>,+ > = std::sync::Mutex::new(None);+ let mut passes = PASSES+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let passes = passes.get_or_insert_with(std::collections::HashMap::new);+ // Those nobody holds or waits for are let go: the map is as large as what is in use.+ passes.retain(|_, pass| Arc::strong_count(pass) > 1);+ Arc::clone(passes.entry(folder.to_string()).or_default())+}/// One Photos pass at a time, from reading its sync position through storing what came back.pub static PHOTOS_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());@@ -188,16 +206,20 @@ async fn refresh_reminders(.await.map_err(|e| e.to_string())?;{- let mut library = data.0.lock().await;+ // Stored as one transaction, so the screens read beside it (`lock_for_pass`).+ let mut library = data.0.lock_for_pass().await;owned_by(&library, asked_for.as_deref())?;// Changes still on their way to Apple stay as made here — stored with the pass, as one.- library- .atomically(|library| {+ // In place, as `refresh::store` does: a whole list's records and alarms are disk and+ // CPU work that would otherwise hold up every task queued on this worker.+ tokio::task::block_in_place(|| {+ library.atomically(|library| {quince_icloud::reminders::apply_cloudkit(library, fetched)?;outbox::replay_reminders(library)?;Ok::<_, quince_icloud::Error>(())})- .map_err(|e| e.to_string())?;+ })+ .map_err(|e| e.to_string())?;}alerts::changed();Ok(())@@ -229,16 +251,18 @@ async fn refresh_notes(let fetched = quince_icloud::notes::fetch_cloudkit(http, root, params, since.as_deref()).await.map_err(|e| e.to_string())?;- let mut library = data.0.lock().await;+ // Stored as one transaction, so the screens read beside it (`lock_for_pass`).+ let mut library = data.0.lock_for_pass().await;owned_by(&library, asked_for.as_deref())?;// Edits still on their way to Apple stay as typed here — stored with the pass, as one.- library- .atomically(|library| {+ tokio::task::block_in_place(|| {+ library.atomically(|library| {quince_icloud::notes::apply_cloudkit(library, fetched)?;outbox::replay(library, outbox::NOTES)?;Ok::<_, quince_icloud::Error>(())})- .map_err(|e| e.to_string())+ })+ .map_err(|e| e.to_string())}async fn cloudkit_context(@@ -1050,6 +1074,8 @@ async fn claim_library(app: &tauri::AppHandle, client: &Client) -> Result<(), Stif known.is_some() || home.exists() {let now = jiff::Timestamp::now().as_second();let away = known.unwrap_or_else(|| format!("unclaimed-{now}"));+ // The screens' readers let go of the file before it moves.+ data.0.forget_readers();switch_library(&mut library, &dir, &away, &home)?;photos::forget_links();}@@ -1624,16 +1650,19 @@ async fn calendar_sync(.await.map_err(|e| e.to_string())?;let summary = {- let mut library = data.0.lock().await;+ // Stored as one transaction, so the screens read beside it (`lock_for_pass`).+ let mut library = data.0.lock_for_pass().await;still_owned(&library, account.as_deref())?;// Changes still on their way to Apple stay as made here — stored with the pass, as one.- library- .atomically(|library| {+ // In place, as `refresh::store` does, so the worker's other tasks are handed on.+ tokio::task::block_in_place(|| {+ library.atomically(|library| {let summary = quince_icloud::sync::apply_calendar(library, fetched)?;outbox::replay(library, outbox::CALENDAR)?;Ok::<_, quince_icloud::Error>(summary)})- .map_err(|e| e.to_string())?+ })+ .map_err(|e| e.to_string())?};// Apple answered: whatever waits to be sent goes now.outbox::kick();@@ -1841,19 +1870,18 @@ async fn calendar_events(occasions: State<'_, Occasions>,feeds_state: State<'_, Feeds>,) -> UiResult<Vec<Event>> {- let (mut events, people) = {- let library = data.0.lock().await;- (- library- .events_between(from, to)- .map_err(|e| e.to_string())?,- // Only who has a birthday, and their names: every card built whole was most of- // the cost of reading a month.- library- .contacts_with_birthdays()- .map_err(|e| e.to_string())?,- )- };+ let (mut events, people) = data+ .0+ .read(|library| {+ Ok((+ library.events_between(from, to)?,+ // Only who has a birthday, and their names: every card built whole was most of+ // the cost of reading a month.+ library.contacts_with_birthdays()?,+ ))+ })+ .await+ .map_err(|e| e.to_string())?;// Birthdays and holidays, as a phone draws them: neither is an iCloud calendar.events.extend(quince_icloud::occasions::birthdays(&people, from, to));events.extend(quince_icloud::occasions::holidays(@@ -2336,13 +2364,11 @@ async fn calendar_list() -> UiResult<Vec<Calendar>> {// The library is let go before the holiday feed and subscriptions are waited for: held// across them, every other screen's read queued behind those locks too.- let (mut calendars, any_birthdays) = {- let library = data.0.lock().await;- (- library.calendars().map_err(|e| e.to_string())?,- library.any_birthday().map_err(|e| e.to_string())?,- )- };+ let (mut calendars, any_birthdays) = data+ .0+ .read(|library| Ok((library.calendars()?, library.any_birthday()?)))+ .await+ .map_err(|e| e.to_string())?;let holidays = {let feed = occasions.feed.lock().await;(!feed.1.is_empty()).then(|| feed.0.clone().unwrap_or_else(|| "Holidays".into()))@@ -2369,8 +2395,10 @@ async fn reminder_counts(until: i64,data: State<'_, Data>,) -> UiResult<quince_core::reminders::ReminderCounts> {- let library = data.0.lock().await;- library.reminder_counts(until).map_err(|e| e.to_string())+ data.0+ .read(|library| library.reminder_counts(until))+ .await+ .map_err(|e| e.to_string())}/// Reminders still to do holding every word of `query`, across every enabled list.@@ -2404,16 +2432,18 @@ async fn reminder_sections(list_guid: String,data: State<'_, Data>,) -> UiResult<Vec<quince_core::reminders::ReminderSection>> {- let library = data.0.lock().await;- library- .reminder_sections(&list_guid)+ data.0+ .read(|library| library.reminder_sections(&list_guid))+ .await.map_err(|e| e.to_string())}#[tauri::command]async fn reminder_lists(data: State<'_, Data>) -> UiResult<Vec<ReminderList>> {- let library = data.0.lock().await;- library.reminder_lists().map_err(|e| e.to_string())+ data.0+ .read(Library::reminder_lists)+ .await+ .map_err(|e| e.to_string())}/// The folders lists are filed under.@@ -2421,8 +2451,10 @@ async fn reminder_lists(data: State<'_, Data>) -> UiResult<Vec<ReminderList>> {async fn reminder_groups(data: State<'_, Data>,) -> UiResult<Vec<quince_core::reminders::ReminderGroup>> {- let library = data.0.lock().await;- library.reminder_groups().map_err(|e| e.to_string())+ data.0+ .read(Library::reminder_groups)+ .await+ .map_err(|e| e.to_string())}#[tauri::command]@@ -2431,9 +2463,9 @@ async fn reminders_in(include_done: bool,data: State<'_, Data>,) -> UiResult<Vec<Reminder>> {- let library = data.0.lock().await;- library- .reminders_in(&list_guid, include_done)+ data.0+ .read(|library| library.reminders_in(&list_guid, include_done))+ .await.map_err(|e| e.to_string())}@@ -2635,9 +2667,9 @@ async fn reminders_due_between(to: i64,data: State<'_, Data>,) -> UiResult<Vec<Reminder>> {- let library = data.0.lock().await;- library- .reminders_due_between(from, to)+ data.0+ .read(|library| library.reminders_due_between(from, to))+ .await.map_err(|e| e.to_string())}@@ -2736,9 +2768,9 @@ async fn reminders_smart(until: i64,data: State<'_, Data>,) -> UiResult<Vec<Reminder>> {- let library = data.0.lock().await;- library- .reminders_smart(kind, until)+ data.0+ .read(|library| library.reminders_smart(kind, until))+ .await.map_err(|e| e.to_string())}@@ -2876,8 +2908,10 @@ async fn note_by_guid(data: &State<'_, Data>, guid: &str) -> Result<Note, String/// the unbounded payload the plan rules out. The note being read asks for its own.#[tauri::command]async fn note_body(guid: String, data: State<'_, Data>) -> UiResult<Option<String>> {- let library = data.0.lock().await;- library.note_body(&guid).map_err(|e| e.to_string())+ data.0+ .read(|library| library.note_body(&guid))+ .await+ .map_err(|e| e.to_string())}/// A note written out as Markdown at `path` (chosen in the save dialog): its text as it is on@@ -2933,8 +2967,11 @@ async fn note_paragraph(/// How a note's text is set — Apple's attribute runs, in order — as stored at the last sync.#[tauri::command]async fn note_runs(guid: String, data: State<'_, Data>) -> UiResult<serde_json::Value> {- let library = data.0.lock().await;- let stored = library.note_runs(&guid).map_err(|e| e.to_string())?;+ let stored = data+ .0+ .read(|library| library.note_runs(&guid))+ .await+ .map_err(|e| e.to_string())?;Ok(stored.and_then(|s| serde_json::from_str(&s).ok()).unwrap_or(serde_json::Value::Array(Vec::new())))@@ -3239,8 +3276,10 @@ async fn note_folder_delete(#[tauri::command]async fn note_folders(data: State<'_, Data>) -> UiResult<Vec<NoteFolder>> {- let library = data.0.lock().await;- library.note_folders().map_err(|e| e.to_string())+ data.0+ .read(Library::note_folders)+ .await+ .map_err(|e| e.to_string())}/// Notes holding every word of `query`, from every folder but Recently Deleted.@@ -3270,16 +3309,18 @@ async fn notes_search(/// How many notes each folder holds, and all of them but Recently Deleted.#[tauri::command]async fn note_counts(data: State<'_, Data>) -> UiResult<(i64, Vec<(String, i64)>)> {- let library = data.0.lock().await;- library.note_counts().map_err(|e| e.to_string())+ data.0+ .read(Library::note_counts)+ .await+ .map_err(|e| e.to_string())}/// Notes in a folder, or every note when no folder is given.#[tauri::command]async fn notes_in(folder_guid: Option<String>, data: State<'_, Data>) -> UiResult<Vec<Note>> {- let library = data.0.lock().await;- library- .notes_in(folder_guid.as_deref())+ data.0+ .read(|library| library.notes_in(folder_guid.as_deref()))+ .await.map_err(|e| e.to_string())}@@ -3322,8 +3363,11 @@ async fn resync_contacts(async fn contact_lists(data: State<'_, Data>,) -> UiResult<Vec<quince_icloud::contacts::ContactList>> {- let library = data.0.lock().await;- Ok(quince_icloud::contacts::stored_lists(&library))+ Ok(data+ .0+ .read(|library| Ok(quince_icloud::contacts::stored_lists(library)))+ .await+ .unwrap_or_default())}/// One Contacts list write at a time. Each reads the list as Apple holds it and sends it@@ -3587,9 +3631,10 @@ async fn contacts_import(/// The card that is this person's own, as the last pass stored it.#[tauri::command]async fn contact_me(data: State<'_, Data>) -> UiResult<Option<String>> {- let library = data.0.lock().await;- Ok(library- .state(quince_icloud::contacts::ME_KEY)+ Ok(data+ .0+ .read(|library| library.state(quince_icloud::contacts::ME_KEY))+ .await.ok().flatten().filter(|s| !s.is_empty()))@@ -3619,8 +3664,10 @@ async fn contacts_list(search: Option<String>,data: State<'_, Data>,) -> UiResult<Vec<quince_core::contacts::Card>> {- let library = data.0.lock().await;- library.cards(search.as_deref()).map_err(|e| e.to_string())+ data.0+ .read(|library| library.cards(search.as_deref()))+ .await+ .map_err(|e| e.to_string())}#[tauri::command]@@ -3637,7 +3684,8 @@ async fn drive_sync_folder(.ok_or("Apple hasn't offered iCloud Drive for this account.")?;(client.http().clone(), root, client.service_params())};- let _one = DRIVE_PASS.lock().await;+ let pass = drive_pass(&folder_id);+ let _one = pass.lock().await;let items = match quince_icloud::drive::fetch_folder(&http, &root, params, &folder_id).await {Ok(items) => items,// Deleted or moved on another device: forgotten here too, so the screen can step back@@ -3657,17 +3705,19 @@ async fn drive_sync_folder(#[tauri::command]async fn drive_children(folder_id: String, data: State<'_, Data>) -> UiResult<Vec<DriveItem>> {- let library = data.0.lock().await;- library- .drive_children(&folder_id)+ data.0+ .read(|library| library.drive_children(&folder_id))+ .await.map_err(|e| e.to_string())}/// One item, for building the breadcrumb trail back to the root.#[tauri::command]async fn drive_item(drivewsid: String, data: State<'_, Data>) -> UiResult<Option<DriveItem>> {- let library = data.0.lock().await;- library.drive_item(&drivewsid).map_err(|e| e.to_string())+ data.0+ .read(|library| library.drive_item(&drivewsid))+ .await+ .map_err(|e| e.to_string())}/// Everything a Drive write needs from the session, with the auth lock released first.@@ -3690,7 +3740,8 @@ async fn resync_drive_folder(data: &State<'_, Data>,folder_id: &str,) -> Result<(), String> {- let _one = DRIVE_PASS.lock().await;+ let pass = drive_pass(folder_id);+ let _one = pass.lock().await;let asked_for = asked_as(¶ms);let items = quince_icloud::drive::fetch_folder(http, root, params, folder_id).await@@ -4324,69 +4375,141 @@ async fn drive_duplicate(settle(resync_drive_folder(&http, &drivews, params, &data, parent).await)}-/// Apple's thumbnails for one folder's files, as `data:` URLs keyed by drivewsid.+/// Apple's thumbnails for one folder's files, keyed by drivewsid: each an address the+/// window's `<img>` loads from the cache on disk itself (`quince://…/dthumb/<file>`).////// Kept on disk beside the library, named by item and version, so a folder opened again-/// shows its pictures at once and only a changed file is fetched anew.+/// shows its pictures at once and only a changed file is fetched anew. Each was read, encoded+/// as a `data:` URL and sent through IPC on every look at the folder — some megabytes of text+/// every ten seconds for a folder of photos, parsed on the window's own thread.#[tauri::command]async fn drive_thumbnails(folder_id: String,app: tauri::AppHandle,) -> UiResult<std::collections::HashMap<String, String>> {- use base64::Engine;- Ok(folder_thumbnails(&app, &folder_id)+ Ok(folder_thumbnail_files(&app, &folder_id).await?.into_iter()- .map(|(id, bytes)| {- let url = data_url(- &bytes,- &base64::engine::general_purpose::STANDARD.encode(&bytes),- );- (id, url)+ .filter_map(|(id, file)| {+ let name = file.file_name()?.to_str()?.to_string();+ Some((id, drive_thumb_src(&name)))}).collect())}-/// Apple's pictures of a folder's files, by item id: for the window's grid and for the iCloud-/// Drive folder's thumbnails (mount.rs), from one cache. Kept on disk beside the library,-/// named by item and version, so a folder opened again has its pictures at once and only a-/// changed file is fetched anew.+/// Where the window loads a cached thumbnail from: the address `convertFileSrc` gives+/// `dthumb/<file>` on the `quince` scheme on this platform.+fn drive_thumb_src(file: &str) -> String {+ if cfg!(any(windows, target_os = "android")) {+ format!("http://quince.localhost/dthumb/{file}")+ } else {+ format!("quince://localhost/dthumb/{file}")+ }+}++/// The cached thumbnail a `quince://` request asks for: `dthumb/<file>`, a name in the+/// thumbnail cache — letters, digits, `_` and `-` only, so never a path anywhere else.+fn drive_thumb_part(path: &str) -> Option<String> {+ let path = percent_decode(path.trim_start_matches('/'));+ let file = path.strip_prefix("dthumb/")?;+ (!file.is_empty()+ && file+ .chars()+ .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'))+ .then(|| file.to_string())+}++/// A cached thumbnail's bytes, for the `quince://` handler. The name holds the file's version,+/// so what is served under one name never changes.+async fn drive_thumb_shown(app: &tauri::AppHandle, file: &str) -> Result<Vec<u8>, String> {+ let bytes = tokio::fs::read(state_dir(app).join("thumbnails").join(file))+ .await+ .map_err(|e| e.to_string())?;+ if bytes.is_empty() {+ return Err("no picture".into());+ }+ Ok(bytes)+}++/// Apple's pictures of a folder's files, by item id: for the iCloud Drive folder's thumbnails+/// (mount.rs), from the same cache as the window's grid.pub(crate) async fn folder_thumbnails(app: &tauri::AppHandle,folder_id: &str,) -> Result<std::collections::HashMap<String, Vec<u8>>, String> {- let files = {- let data = app.state::<Data>();- let library = data.0.lock().await;- library- .drive_children(folder_id)- .map_err(|e| e.to_string())?- };- let cache = state_dir(app).join("thumbnails");- std::fs::create_dir_all(&cache).ok();- let key = |f: &quince_core::drive::DriveItem| {- let safe = |s: &str| {- s.chars()- .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })- .collect::<String>()- };- cache.join(format!(- "{}-{}",- safe(&f.drivewsid),- safe(f.etag.as_deref().unwrap_or(""))- ))+ let files = folder_thumbnail_files(app, folder_id).await?;+ tauri::async_runtime::spawn_blocking(move || {+ files+ .into_iter()+ .filter_map(|(id, file)| std::fs::read(file).ok().map(|bytes| (id, bytes)))+ .filter(|(_, bytes)| !bytes.is_empty())+ .collect()+ })+ .await+ .map_err(|e| e.to_string())+}++/// Where in the cache `file` keeps its thumbnail: named by item and version.+fn thumbnail_file(cache: &std::path::Path, file: &quince_core::drive::DriveItem) -> PathBuf {+ let safe = |s: &str| {+ s.chars()+ .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })+ .collect::<String>()};- let mut out = std::collections::HashMap::new();+ cache.join(format!(+ "{}-{}",+ safe(&file.drivewsid),+ safe(file.etag.as_deref().unwrap_or(""))+ ))+}++/// Which of `files` have a picture in the cache (by id, where it is), and which were never+/// asked about at this version. One empty file records "Apple has none": in neither.+fn cached_thumbnails(+ cache: &std::path::Path,+ files: &[quince_core::drive::DriveItem],+) -> (+ std::collections::HashMap<String, PathBuf>,+ Vec<quince_core::drive::DriveItem>,+) {+ let mut found = std::collections::HashMap::new();let mut missing = Vec::new();for f in files.iter().filter(|f| f.kind == "FILE") {- match std::fs::read(key(f)) {- Ok(bytes) if bytes.is_empty() => {} // known to have none- Ok(bytes) => {- out.insert(f.drivewsid.clone(), bytes);+ let at = thumbnail_file(cache, f);+ match std::fs::metadata(&at) {+ Ok(m) if m.len() == 0 => {} // known to have none+ Ok(_) => {+ found.insert(f.drivewsid.clone(), at);}Err(_) => missing.push(f.clone()),}}+ (found, missing)+}++/// Apple's pictures of a folder's files, by item id, as files in the cache: those already+/// there, and those not yet asked about fetched from Apple once and kept.+async fn folder_thumbnail_files(+ app: &tauri::AppHandle,+ folder_id: &str,+) -> Result<std::collections::HashMap<String, PathBuf>, String> {+ let files = app+ .state::<Data>()+ .0+ .read(|library| library.drive_children(folder_id))+ .await+ .map_err(|e| e.to_string())?;+ let cache = state_dir(app).join("thumbnails");+ // Looked over off the async threads: a folder of thousands is that many looks at the disk.+ let (mut out, missing) = {+ let cache = cache.clone();+ tauri::async_runtime::spawn_blocking(move || {+ std::fs::create_dir_all(&cache).ok();+ cached_thumbnails(&cache, &files)+ })+ .await+ .map_err(|e| e.to_string())?+ };if !missing.is_empty() {let (http, docws, params) = {let auth = app.state::<Auth>();@@ -4408,19 +4531,20 @@ pub(crate) async fn folder_thumbnails(.iter().find(|(id, _)| id == &f.drivewsid).map(|(_, b)| b.clone());+ let at = thumbnail_file(&cache, f);// An empty file records "Apple has no picture for this version", so it is not asked again.- std::fs::write(key(f), bytes.as_deref().unwrap_or(&[])).ok();- if let Some(bytes) = bytes {- out.insert(f.drivewsid.clone(), bytes);+ let written = tokio::fs::write(&at, bytes.as_deref().unwrap_or(&[])).await;+ if bytes.is_some() && written.is_ok() {+ out.insert(f.drivewsid.clone(), at);}}}Ok(out)}-/// An image as a `data:` URL, its type read from its first bytes.-fn data_url(bytes: &[u8], b64: &str) -> String {- let mime = if bytes.starts_with(&[0x89, b'P', b'N', b'G']) {+/// A thumbnail's type, read from its first bytes.+fn thumb_type(bytes: &[u8]) -> &'static str {+ if bytes.starts_with(&[0x89, b'P', b'N', b'G']) {"image/png"} else if bytes.starts_with(b"RIFF") {"image/webp"@@ -4428,8 +4552,7 @@ fn data_url(bytes: &[u8], b64: &str) -> String {"image/gif"} else {"image/jpeg"- };- format!("data:{mime};base64,{b64}")+ }}/// How much iCloud storage is used and how much there is, for "X GB available".@@ -4582,8 +4705,9 @@ async fn sync_all(let result = refresh::all(&client, &data.0).await;alerts::changed();warm_contact_photos(app.clone());- // Screens read the library, not the network, so they only need to be told to look again.- app.emit(SYNCED, &result).ok();+ // Screens read the library, not the network, so they only need to be told to look again —+ // and were, service by service, as each one landed with something new.+ app.emit(SYNCED, told(&result)).ok();Ok(result)}@@ -4607,7 +4731,10 @@ async fn sync_some(if names.contains(&"contacts") {warm_contact_photos(app.clone());}- app.emit(SYNCED, refresh::status(&data.0).await).ok();+ // What changed was told to the screens as each service landed; this brings the freshness.+ let mut status = refresh::status(&data.0).await;+ status.changed = Some(Vec::new());+ app.emit(SYNCED, status).ok();Ok(result)}@@ -4633,11 +4760,20 @@ fn spawn_auto_sync(app: tauri::AppHandle, client: Arc<Mutex<Client>>) {let result = refresh::all(&client, &data.0).await;alerts::changed();warm_contact_photos(app.clone());- app.emit(SYNCED, &result).ok();+ app.emit(SYNCED, told(&result)).ok();}});}+/// A finished pass as the screens are told it at the end: its report and freshness, with+/// nothing more to read again — each service that stored something said so as it landed.+fn told(result: &refresh::Refresh) -> refresh::Refresh {+ refresh::Refresh {+ changed: Some(Vec::new()),+ ..result.clone()+ }+}+/// Where a contact's picture is cached on disk, named by the signature Apple gives it so a/// changed picture lands at a new path rather than behind a stale one./// The edge of a served avatar.@@ -4762,22 +4898,24 @@ async fn contact_photo(/// time; one that fails is simply tried again after the next sync.pub fn warm_contact_photos(app: tauri::AppHandle) {tauri::async_runtime::spawn(async move {- let wanted: Vec<String> = {- let data = app.state::<Data>();- let library = data.0.lock().await;- let dir = state_dir(&app);- library- .contacts(None)- .unwrap_or_default()+ // Who has a picture, and which — not every card whole with every method, under the+ // lock, after every pass — and then, with the library let go, which are not on disk.+ let pictured = app+ .state::<Data>()+ .0+ .read(Library::contact_photo_signatures)+ .await+ .unwrap_or_default();+ let dir = state_dir(&app);+ let wanted: Vec<String> = tauri::async_runtime::spawn_blocking(move || {+ pictured.into_iter()- .filter(|c| c.photo_url.is_some())- .filter(|c| match &c.photo_signature {- Some(sig) => !photo_path(&dir, &c.guid, sig).exists(),- None => false,- })- .map(|c| c.guid)+ .filter(|(guid, sig)| !photo_path(&dir, guid, sig).exists())+ .map(|(guid, _)| guid).collect()- };+ })+ .await+ .unwrap_or_default();use futures_util::StreamExt;futures_util::stream::iter(wanted).for_each_concurrent(3, |guid| {@@ -5472,6 +5610,7 @@ fn main() {let wanted = photo_guid(uri.host(), uri.path());let attachment = note_part(uri.path());let reminder_image = reminder_image_part(uri.path());+ let drive_thumb = drive_thumb_part(uri.path());// A drawing's ink for a dark page, rather than a light one.let ink_dark = uri.path().ends_with("drawing-dark");let picture = photos::requested(uri.path());@@ -5522,6 +5661,21 @@ fn main() {responder.respond(response.unwrap_or_default());return;}+ // A Drive file's thumbnail, from the cache on disk.+ if let Some(file) = drive_thumb {+ let response = match drive_thumb_shown(&app, &file).await {+ Ok(bytes) => tauri::http::Response::builder()+ .status(200)+ .header("Content-Type", thumb_type(&bytes))+ .header("Cache-Control", "private, max-age=86400")+ .body(bytes),+ Err(_) => tauri::http::Response::builder()+ .status(404)+ .body(Vec::new()),+ };+ responder.respond(response.unwrap_or_default());+ return;+ }// A picture attached to a reminder.if let Some(id) = reminder_image {let response = match reminder_image_shown(&app, &id).await {@@ -5607,7 +5761,10 @@ fn main() {let mut library = Library::open(&dir)?;timing::mark("library open");adopt_writer(&mut library);- app.manage(Data(Mutex::new(library)));+ app.manage(Data(library_lock::LibraryLock::new(+ library,+ Some(dir.clone()),+ )));// The sender of edits made offline, started once what it sends with — the session// and the library — is in place: started first, its opening pass found neither and// its task died, and nothing waiting was sent all session.@@ -5670,10 +5827,13 @@ fn main() {spawn_auto_sync(app.handle().clone(), client);// Each service's part of a pass, told to the open screens as it is stored.let landing = app.handle().clone();- refresh::on_landed(move |_| {+ refresh::on_landed(move |service, changed| {let app = landing.clone();tauri::async_runtime::spawn(async move {- let status = refresh::status(&app.state::<Data>().0).await;+ let mut status = refresh::status(&app.state::<Data>().0).await;+ // Its freshness for every screen; a read again only for its own, and only+ // when it stored something.+ status.changed = Some(if changed { vec![service] } else { Vec::new() });app.emit(SYNCED, status).ok();});});@@ -6144,4 +6304,77 @@ mod tests {assert_eq!(percent_decode("plain-uuid"), "plain-uuid");assert_eq!(percent_decode("%zz"), "%zz", "malformed stays as it was");}++ /// A Drive thumbnail is asked for by its name in the cache, and nothing else: no path to+ /// anywhere outside it gets through.+ #[test]+ fn a_drive_thumbnail_is_served_from_the_cache_alone() {+ use super::{drive_thumb_part, drive_thumb_src};+ let name = "FILE__com_apple_CloudDocs__ABC-etag_1";+ assert_eq!(+ drive_thumb_part(&format!("/dthumb/{name}")).as_deref(),+ Some(name)+ );+ assert!(drive_thumb_src(name).ends_with(&format!("/dthumb/{name}")));+ for bad in [+ "/dthumb/",+ "/dthumb/../library.db",+ "/dthumb/%2E%2E%2Flibrary.db",+ "/dthumb/a/b",+ "/dthumb/a.b",+ "/photo/abc",+ ] {+ assert!(drive_thumb_part(bad).is_none(), "{bad}");+ }+ }++ /// What a look at a folder's pictures finds in the cache: those kept, by where they are;+ /// those Apple has none for, nowhere; and those never asked about, to fetch. Folders have+ /// no picture and are not asked about.+ #[test]+ fn the_thumbnail_cache_says_what_it_holds_without_reading_it() {+ use super::{cached_thumbnails, thumbnail_file};+ use quince_core::drive::DriveItem;+ let cache = tempfile::tempdir().unwrap();+ let item = |id: &str, kind: &str| DriveItem {+ drivewsid: id.into(),+ parent_id: None,+ name: id.into(),+ extension: None,+ kind: kind.into(),+ size: None,+ child_count: None,+ created_at: None,+ modified_at: None,+ etag: Some("e1".into()),+ };+ let kept = item("FILE::kept", "FILE");+ let none = item("FILE::none", "FILE");+ let new = item("FILE::new", "FILE");+ std::fs::write(thumbnail_file(cache.path(), &kept), b"\xff\xd8\xffpicture").unwrap();+ std::fs::write(thumbnail_file(cache.path(), &none), b"").unwrap();+ let files = [kept, none, new, item("FOLDER::trip", "FOLDER")];+ let (found, missing) = cached_thumbnails(cache.path(), &files);+ assert_eq!(found.len(), 1);+ assert_eq!(found["FILE::kept"], thumbnail_file(cache.path(), &files[0]));+ let missing: Vec<&str> = missing.iter().map(|f| f.drivewsid.as_str()).collect();+ assert_eq!(missing, ["FILE::new"]);+ }++ /// One folder's pass waits only for that folder's: another folder is read meanwhile, and+ /// a second look at the same folder waits its turn.+ #[tokio::test]+ async fn drive_folders_are_fetched_side_by_side() {+ let a = super::drive_pass("FOLDER::a");+ let held = a.lock().await;+ let b = super::drive_pass("FOLDER::b");+ assert!(b.try_lock().is_ok(), "another folder is not held up");+ let again = super::drive_pass("FOLDER::a");+ assert!(+ again.try_lock().is_err(),+ "the same folder still takes turns"+ );+ drop(held);+ assert!(again.try_lock().is_ok());+ }}diff --git a/src-tauri/src/mount.rs b/src-tauri/src/mount.rsindex c82cd11..735dc92 100644--- a/src-tauri/src/mount.rs+++ b/src-tauri/src/mount.rs@@ -58,7 +58,8 @@ impl Remote for AppRemote {let (app, folder) = (self.app.clone(), folder.to_string());Box::pin(async move {let (http, root, params) = context(&app, "drivews").await?;- let _one = crate::DRIVE_PASS.lock().await;+ let pass = crate::drive_pass(&folder);+ let _one = pass.lock().await;let items = quince_icloud::drive::fetch_folder(&http, &root, params, &folder).await.map_err(|e| e.to_string())?;@@ -228,14 +229,75 @@ impl Remote for AppRemote {}}-struct AppStore(AppHandle);+struct AppStore {+ app: AppHandle,+ /// The folder's own read-only connection on the library: what it lists never waits on the+ /// lock a sync pass or the window holds, and its version says when a listing is stale.+ reader: Mutex<MountReader>,+}++#[derive(Default)]+struct MountReader {+ reader: Option<crate::library_lock::Reader>,+ /// Which reader this is (opened again on another file, its version starts over) and what+ /// its version last said; `version` moves whenever either does.+ opened: u64,+ seen: Option<(u64, i64)>,+ version: u64,+}++impl AppStore {+ fn new(app: AppHandle) -> Self {+ Self {+ app,+ reader: Mutex::new(MountReader::default()),+ }+ }++ /// Run `read` on the folder's reader, opened (again) as the library's file needs.+ fn with_reader<T>(+ &self,+ read: impl FnOnce(&mut MountReader, &crate::library_lock::Reader) -> Option<T>,+ ) -> Option<T> {+ let dir = crate::state_dir(&self.app);+ let mut held = self+ .reader+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if !held.reader.as_ref().is_some_and(|r| r.current(&dir)) {+ held.reader = crate::library_lock::Reader::open(&dir);+ held.opened += 1;+ }+ let reader = held.reader.take()?;+ let found = read(&mut held, &reader);+ held.reader = Some(reader);+ found+ }+}impl Store for AppStore {fn children(&self, folder: &str) -> Option<Vec<DriveItem>> {- // Called on the folder's own threads, never the async runtime's: waiting is allowed.- let data = self.0.state::<Data>();- let library = data.0.blocking_lock();- library.drive_children(folder).ok()+ // Called on the folder's own threads, never the async runtime's: waiting is allowed —+ // but a file manager's every look at a name came here, and waited out whole sync passes+ // on the library's lock. On the folder's own reader it never waits on one.+ if let Some(Ok(items)) = self.with_reader(|_, r| Some(r.library().drive_children(folder))) {+ return Some(items);+ }+ let data = self.app.state::<Data>();+ data.0+ .blocking_read(|library| library.drive_children(folder))+ .ok()+ }++ fn version(&self) -> Option<u64> {+ self.with_reader(|held, r| {+ let now = (held.opened, r.data_version()?);+ if held.seen != Some(now) {+ held.seen = Some(now);+ held.version += 1;+ }+ Some(held.version)+ })}}@@ -372,7 +434,7 @@ fn start(app: &AppHandle, which: Which, at: &std::path::Path) -> Result<(), Strilinks: Arc::new(Mutex::new(HashMap::new())),});// What is written here waits beside the library (owner-only) until Apple has it.- quince_vfs::fuse::DriveFs::new(remote, Arc::new(AppStore(app.clone())), cache, rt)+ quince_vfs::fuse::DriveFs::new(remote, Arc::new(AppStore::new(app.clone())), cache, rt).with_writes(&crate::state_dir(app).join("drive-writes")).map_err(|e| e.to_string())?}diff --git a/src-tauri/src/mount_photos.rs b/src-tauri/src/mount_photos.rsindex 6453fb7..32ead08 100644--- a/src-tauri/src/mount_photos.rs+++ b/src-tauri/src/mount_photos.rs@@ -123,9 +123,18 @@ fn taken(p: &Photo) -> (i16, i8) {}/// What a folder holds, read from the library (the app keeps it in step with Apple).+///+/// Read beside the library's lock, never behind it: a file manager browsing the folder waited+/// out every sync pass the window was storing.fn children(app: &AppHandle, id: &str) -> Option<Vec<DriveItem>> {- let data = app.state::<Data>();- let library = data.0.blocking_lock();+ app.state::<Data>()+ .0+ .blocking_read(|library| Ok(children_in(library, id)))+ .ok()+ .flatten()+}++fn children_in(library: &quince_core::Library, id: &str) -> Option<Vec<DriveItem>> {let photos = |view: View| library.photos(view).unwrap_or_default();Some(match id {ROOT => vec![@@ -152,7 +161,7 @@ fn children(app: &AppHandle, id: &str) -> Option<Vec<DriveItem>> {.iter().flat_map(|p| files(p, VIDEOS)).collect(),- ALBUMS => albums_in(&library, None, ALBUMS),+ ALBUMS => albums_in(library, None, ALBUMS),_ => {if let Some(y) = id.strip_prefix("PH::y:") {let y: i16 = y.parse().ok()?;@@ -187,7 +196,7 @@ fn children(app: &AppHandle, id: &str) -> Option<Vec<DriveItem>> {.iter().any(|a| a.id == album && a.folder);if is_folder {- albums_in(&library, Some(album), id)+ albums_in(library, Some(album), id)} else {library.album_photos(album)@@ -235,13 +244,16 @@ fn accepts(app: &AppHandle, folder: &str) -> bool {return true;}folder.strip_prefix("PH::album:").is_some_and(|album| {- let data = app.state::<Data>();- let library = data.0.blocking_lock();- library- .albums()- .unwrap_or_default()- .iter()- .any(|a| a.id == album && !a.folder)+ app.state::<Data>()+ .0+ .blocking_read(|library| {+ Ok(library+ .albums()+ .unwrap_or_default()+ .iter()+ .any(|a| a.id == album && !a.folder))+ })+ .unwrap_or(false)})}diff --git a/src-tauri/src/outbox.rs b/src-tauri/src/outbox.rsindex 1431d2a..b6cb0ea 100644--- a/src-tauri/src/outbox.rs+++ b/src-tauri/src/outbox.rs@@ -1180,7 +1180,12 @@ struct Report {/// Send what is waiting for `service`, oldest first, with `send`. Stops at the first change/// Apple can't be reached for, leaving it and everything after it queued, in order.-async fn drain<F, Fut>(library: &Mutex<Library>, service: &str, pass: &Mutex<()>, send: F) -> Report+async fn drain<F, Fut>(+ library: &crate::library_lock::LibraryLock,+ service: &str,+ pass: &Mutex<()>,+ send: F,+) -> ReportwhereF: Fn(Pending) -> Fut,Fut: std::future::Future<Output = Result<(), Error>>,@@ -1294,7 +1299,7 @@ impl Outcome {}async fn send_reminder(- library: &Mutex<Library>,+ library: &crate::library_lock::LibraryLock,http: &reqwest::Client,root: &str,params: Vec<(String, String)>,@@ -1450,7 +1455,7 @@ async fn drain_notes(app: &AppHandle) -> Outcome {}async fn send_note(- library: &Mutex<Library>,+ library: &crate::library_lock::LibraryLock,http: &reqwest::Client,root: &str,params: Vec<(String, String)>,@@ -1519,7 +1524,7 @@ async fn send_note(}async fn send_note_save(- library: &Mutex<Library>,+ library: &crate::library_lock::LibraryLock,http: &reqwest::Client,root: &str,params: Vec<(String, String)>,@@ -1598,7 +1603,7 @@ async fn drain_contacts(app: &AppHandle) -> Outcome {}async fn send_contact(- library: &Mutex<Library>,+ library: &crate::library_lock::LibraryLock,http: &reqwest::Client,root: &str,params: Vec<(String, String)>,@@ -1702,7 +1707,7 @@ async fn drain_calendar(app: &AppHandle) -> Outcome {}async fn send_event(- library: &Mutex<Library>,+ library: &crate::library_lock::LibraryLock,http: &reqwest::Client,root: &str,params: Vec<(String, String)>,@@ -2130,7 +2135,7 @@ mod tests {#[test]fn sends_in_order_keeps_what_cannot_reach_apple_and_holds_refusals() {tauri::async_runtime::block_on(async {- let lib = Mutex::new(library());+ let lib = crate::library_lock::LibraryLock::new(library(), None);let pass = Mutex::new(());{let mut l = lib.lock().await;@@ -2649,7 +2654,8 @@ mod tests {l.outbox_take(REMINDERS).unwrap();made};- let lib = Mutex::new(Library::open(dir.path()).unwrap());+ let lib =+ crate::library_lock::LibraryLock::new(Library::open(dir.path()).unwrap(), None);let pass = Mutex::new(());// Apple, as a set of names: a create of a name it holds makes nothing.let apple = Arc::new(StdMutex::new(std::collections::HashSet::from([@@ -2734,7 +2740,11 @@ mod tests {}/// Drain with a pretend Apple: every change is taken, unless `refuse` names its kind.- async fn send_all(lib: &Mutex<Library>, service: &str, refuse: &'static str) -> Report {+ async fn send_all(+ lib: &crate::library_lock::LibraryLock,+ service: &str,+ refuse: &'static str,+ ) -> Report {let pass = Mutex::new(());drain(lib, service, &pass, move |p| async move {if p.kind == refuse {@@ -2978,7 +2988,8 @@ mod tests {l.outbox_take(NOTES).unwrap();(made, x.guid)};- let lib = Mutex::new(Library::open(dir.path()).unwrap());+ let lib =+ crate::library_lock::LibraryLock::new(Library::open(dir.path()).unwrap(), None);{let l = lib.lock().await;assert!(@@ -3057,7 +3068,7 @@ mod tests {},).unwrap();- let lib = Mutex::new(l);+ let lib = crate::library_lock::LibraryLock::new(l, None);for (service, kind) in [(NOTES, "create"), (CONTACTS, "delete"), (CALENDAR, "save")] {let report = send_all(&lib, service, kind).await;assert_eq!(report.refused.len(), 1, "{service} {kind}");diff --git a/src-tauri/src/photo_cache.rs b/src-tauri/src/photo_cache.rsindex abefbc2..02b4230 100644--- a/src-tauri/src/photo_cache.rs+++ b/src-tauri/src/photo_cache.rs@@ -461,8 +461,7 @@ fn cap_blocking(app: &AppHandle) -> u64 {let saved = app.state::<Data>().0- .blocking_lock()- .state(CAP_KEY)+ .blocking_read(|library| library.state(CAP_KEY)).ok().flatten();let cap = parse_cap(saved);@@ -478,8 +477,7 @@ fn waiting_uploads(app: &AppHandle) -> Vec<(u64, u64, Option<PathBuf>)> {let paths: Vec<PathBuf> = app.state::<Data>().0- .blocking_lock()- .unfinished_uploads()+ .blocking_read(quince_core::Library::unfinished_uploads).unwrap_or_default().into_iter().map(|u| PathBuf::from(u.path))diff --git a/src-tauri/src/photos.rs b/src-tauri/src/photos.rsindex af026f7..07d353b 100644--- a/src-tauri/src/photos.rs+++ b/src-tauri/src/photos.rs@@ -1072,7 +1072,10 @@ pub fn content_type(bytes: &[u8], size: Size) -> &'static str {/// A view's photos, newest first.#[tauri::command]pub async fn photos_list(view: View, data: State<'_, Data>) -> UiResult<Vec<Photo>> {- data.0.lock().await.photos(view).map_err(|e| e.to_string())+ data.0+ .read(|library| library.photos(view))+ .await+ .map_err(|e| e.to_string())}/// What the grid lays out before any photo comes: every photo's id, the date it is grouped@@ -1085,9 +1088,8 @@ pub async fn photos_index(data: State<'_, Data>,) -> UiResult<quince_core::photos::PhotoIndex> {data.0- .lock()+ .read(|library| library.photo_index(&source, order)).await- .photo_index(&source, order).map_err(|e| e.to_string())}@@ -1101,9 +1103,8 @@ pub async fn photos_page(data: State<'_, Data>,) -> UiResult<Vec<Photo>> {data.0- .lock()+ .read(|library| library.photo_page(&source, order, offset, limit)).await- .photo_page(&source, order, offset, limit).map_err(|e| e.to_string())}@@ -1111,9 +1112,8 @@ pub async fn photos_page(#[tauri::command]pub async fn photos_get(ids: Vec<String>, data: State<'_, Data>) -> UiResult<Vec<Photo>> {data.0- .lock()+ .read(|library| library.photos_by_ids(&ids)).await- .photos_by_ids(&ids).map_err(|e| e.to_string())}@@ -1189,9 +1189,8 @@ pub async fn photos_albums() -> UiResult<Vec<(quince_core::photos::Album, usize, Option<String>)>> {let counted = data.0- .lock()+ .read(quince_core::Library::albums_counted).await- .albums_counted().map_err(|e| e.to_string())?;Ok(counted.into_iter()@@ -1204,9 +1203,8 @@ pub async fn photos_albums(#[tauri::command]pub async fn photos_search(query: String, data: State<'_, Data>) -> UiResult<Vec<Photo>> {data.0- .lock()+ .read(|library| library.search_photos(&query, 20_000)).await- .search_photos(&query, 20_000).map_err(|e| e.to_string())}@@ -1214,9 +1212,8 @@ pub async fn photos_search(query: String, data: State<'_, Data>) -> UiResult<Vec#[tauri::command]pub async fn photos_in_album(album: String, data: State<'_, Data>) -> UiResult<Vec<Photo>> {data.0- .lock()+ .read(|library| library.album_photos(&album)).await- .album_photos(&album).map_err(|e| e.to_string())}@@ -1973,9 +1970,8 @@ pub async fn photos_counts(app: AppHandle, data: State<'_, Data>) -> UiResult<Vework(app.clone());keep_ahead(app);data.0- .lock()+ .read(|library| library.photo_counts()).await- .photo_counts().map_err(|e| e.to_string())}diff --git a/src-tauri/src/push.rs b/src-tauri/src/push.rsindex 5e46aaf..be268d1 100644--- a/src-tauri/src/push.rs+++ b/src-tauri/src/push.rs@@ -271,7 +271,10 @@ async fn refresher(crate::warm_contact_photos(app.clone());}// Every service's freshness, not only this one: the window shows the whole.- app.emit(SYNCED, refresh::status(&data.0).await).ok();+ // What it stored, if anything, was told as it landed.+ let mut status = refresh::status(&data.0).await;+ status.changed = Some(Vec::new());+ app.emit(SYNCED, status).ok();});}}diff --git a/src-tauri/src/refresh.rs b/src-tauri/src/refresh.rsindex 7d92366..37f0746 100644--- a/src-tauri/src/refresh.rs+++ b/src-tauri/src/refresh.rs@@ -14,9 +14,11 @@//! service; the failure is recorded against that service alone and its last good data stays//! on screen.+use crate::library_lock::LibraryLock;use quince_auth::Client;use quince_core::Library;use serde::Serialize;+use std::sync::atomic::{AtomicU64, Ordering};use std::sync::Arc;use tokio::sync::Mutex;@@ -42,6 +44,10 @@ pub struct ServiceReport {/// A sentence for the person: what came back, or what went wrong.pub detail: Option<String>,pub last_synced_at: Option<i64>,+ /// It stored something a screen shows — not only where it got to. Told to the screens with+ /// the pass, never sent with the report itself.+ #[serde(skip)]+ pub changed: bool,}/// Everything one refresh pass did.@@ -54,6 +60,10 @@ pub struct Refresh {/// synced.pub as_of: Option<i64>,pub took_ms: u64,+ /// The services whose stored rows this changed, when that is known: a screen reads again+ /// only for its own. Absent, every screen reads again, as it always did.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub changed: Option<Vec<&'static str>>,}/// How often a service reconciles against everything Apple has, rather than the changes@@ -123,36 +133,118 @@ async fn context(client: &Arc<Mutex<Client>>) -> Context {}}-/// Told each time one service's part of a pass has been stored, before the rest are done.-static LANDED: std::sync::OnceLock<Box<dyn Fn(&'static str) + Send + Sync>> =- std::sync::OnceLock::new();+/// Told each time one service's part of a pass has been stored, before the rest are done,+/// with whether it changed anything a screen shows.+type Landed = Box<dyn Fn(&'static str, bool) + Send + Sync>;+static LANDED: std::sync::OnceLock<Landed> = std::sync::OnceLock::new();/// What to do when a service lands: set once, by the app, to tell the open screens.-pub fn on_landed(f: impl Fn(&'static str) + Send + Sync + 'static) {+pub fn on_landed(f: impl Fn(&'static str, bool) + Send + Sync + 'static) {let _ = LANDED.set(Box::new(f));}-fn landed(service: &'static str) {+fn landed(report: &ServiceReport) {if let Some(f) = LANDED.get() {- f(service);+ f(report.service, report.changed);}}+/// Where one service's passes have got to: how many have started, and the last one's report.+struct Turns {+ started: AtomicU64,+ last: std::sync::Mutex<Option<(u64, ServiceReport)>>,+}++impl Turns {+ const fn new() -> Self {+ Self {+ started: AtomicU64::new(0),+ last: std::sync::Mutex::new(None),+ }+ }+}++static TURNS: [Turns; COVERED.len()] = [+ Turns::new(),+ Turns::new(),+ Turns::new(),+ Turns::new(),+ Turns::new(),+ Turns::new(),+];++fn turns(service: &str) -> &'static Turns {+ let at = COVERED.iter().position(|(s, _)| *s == service).unwrap_or(0);+ &TURNS[at]+}++/// Run one service's pass under its pass lock — or, when a pass of it started after this was+/// asked for and has finished while this waited, take that one's report instead of running+/// another.+///+/// Focus in each window, the five-minute timer, every push burst and each window opening all+/// ask for a pass; each used to wait for the one before and then run its own, so a burst of+/// five asked Apple the same questions five times over. A pass that started after the asking+/// has read everything the asking could want, so it answers for all of them: a burst costs at+/// most the pass already running and one more. The second value says whether this ran it.+async fn once<F, Fut>(+ service: &'static str,+ lock: &tokio::sync::Mutex<()>,+ pass: F,+) -> (ServiceReport, bool)+where+ F: FnOnce() -> Fut,+ Fut: std::future::Future<Output = ServiceReport>,+{+ let turns = turns(service);+ let asked = turns.started.load(Ordering::SeqCst);+ let _one = lock.lock().await;+ let done = turns+ .last+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .clone();+ if let Some((n, report)) = done {+ if n > asked {+ return (report, false);+ }+ }+ // Counted before the pass reads anything, so whoever asks from here on waits for the next.+ let n = turns.started.fetch_add(1, Ordering::SeqCst) + 1;+ let report = pass().await;+ *turns+ .last+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some((n, report.clone()));+ (report, true)+}++/// A pass's report, told to the screens as it lands when this pass ran it. One taken from a+/// pass that already ran was told then, and counts as no change here: the screens have read it.+fn arrived((mut report, ran): (ServiceReport, bool)) -> ServiceReport {+ if ran {+ landed(&report);+ } else {+ report.changed = false;+ }+ report+}+/// Refresh every service the account has, and report on each.////// Never returns an error: a refresh that partly failed is a normal outcome with a partial/// result, not an exception, and the interface has to be able to show exactly that.-pub async fn all(client: &Arc<Mutex<Client>>, data: &Mutex<Library>) -> Refresh {+pub async fn all(client: &Arc<Mutex<Client>>, data: &LibraryLock) -> Refresh {run(client, data, &COVERED.map(|(service, _)| service)).await}/// Refresh only the services `wanted` names — what a push from Apple says changed — and/// report on those alone.-pub async fn some(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, wanted: &[&str]) -> Refresh {+pub async fn some(client: &Arc<Mutex<Client>>, data: &LibraryLock, wanted: &[&str]) -> Refresh {run(client, data, wanted).await}-async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&str]) -> Refresh {+async fn run(client: &Arc<Mutex<Client>>, data: &LibraryLock, services: &[&str]) -> Refresh {let wanted = |service: &str| services.contains(&service);let started = std::time::Instant::now();let ctx = context(client).await;@@ -168,41 +260,42 @@ async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&streturn None;}let report = match ctx.calendar.as_deref() {- None => unavailable("calendar", "Calendar"),- Some(root) => {- // Fetch and store as one, never across a save: see `CALENDAR_PASS`.- let _one = crate::CALENDAR_PASS.lock().await;- let fetched = quince_icloud::sync::fetch_calendar(- &ctx.http,- root,- ctx.params.clone(),- &from,- &to,- &zone,- )- .await;- store(- "calendar",- "Calendar",- data,- ctx.account.as_deref(),- fetched,- |library, snapshot| {- let summary = quince_icloud::sync::apply_calendar(library, snapshot)?;- crate::outbox::replay(library, crate::outbox::CALENDAR)?;- Ok(format!(- "{} event{} in {} calendar{}",- summary.events,- plural(summary.events),- summary.calendars,- plural(summary.calendars)- ))- },- )- .await- }+ None => arrived((unavailable("calendar", "Calendar"), true)),+ Some(root) => arrived(+ once("calendar", &crate::CALENDAR_PASS, || async {+ // Fetch and store as one, never across a save: see `CALENDAR_PASS`.+ let fetched = quince_icloud::sync::fetch_calendar(+ &ctx.http,+ root,+ ctx.params.clone(),+ &from,+ &to,+ &zone,+ )+ .await;+ store(+ "calendar",+ "Calendar",+ data,+ ctx.account.as_deref(),+ fetched,+ |library, snapshot| {+ let summary = quince_icloud::sync::apply_calendar(library, snapshot)?;+ crate::outbox::replay(library, crate::outbox::CALENDAR)?;+ Ok(format!(+ "{} event{} in {} calendar{}",+ summary.events,+ plural(summary.events),+ summary.calendars,+ plural(summary.calendars)+ ))+ },+ )+ .await+ })+ .await,+ ),};- landed(report.service);Some(report)};@@ -211,45 +304,46 @@ async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&streturn None;}let report = match ctx.cloudkit.as_deref() {- None => unavailable("reminders", "Reminders"),- Some(root) => {- let _one = crate::REMINDERS_PASS.lock().await;- let since = token(data, "reminders").await;- let full = since.is_none();- let fetched = quince_icloud::reminders::fetch_cloudkit(- &ctx.http,- root,- ctx.params.clone(),- since.as_deref(),- )- .await;- store(- "reminders",- "Reminders",- data,- ctx.account.as_deref(),- fetched,- move |library, f| {- let (lists, count, _) =- quince_icloud::reminders::apply_cloudkit(library, f)?;- // Changes still on their way to Apple stay as made here: without this- // a reminder ticked offline showed open again after every pass, and- // its alert could ring for something already done.- crate::outbox::replay_reminders(library)?;- if full {- library.mark_full_sync("reminders")?;- }- Ok(format!(- "{count} reminder{} in {lists} list{}",- plural(count),- plural(lists)- ))- },- )- .await- }+ None => arrived((unavailable("reminders", "Reminders"), true)),+ Some(root) => arrived(+ once("reminders", &crate::REMINDERS_PASS, || async {+ let since = token(data, "reminders").await;+ let full = since.is_none();+ let fetched = quince_icloud::reminders::fetch_cloudkit(+ &ctx.http,+ root,+ ctx.params.clone(),+ since.as_deref(),+ )+ .await;+ store(+ "reminders",+ "Reminders",+ data,+ ctx.account.as_deref(),+ fetched,+ move |library, f| {+ let (lists, count, _) =+ quince_icloud::reminders::apply_cloudkit(library, f)?;+ // Changes still on their way to Apple stay as made here: without this+ // a reminder ticked offline showed open again after every pass, and+ // its alert could ring for something already done.+ crate::outbox::replay_reminders(library)?;+ if full {+ library.mark_full_sync("reminders")?;+ }+ Ok(format!(+ "{count} reminder{} in {lists} list{}",+ plural(count),+ plural(lists)+ ))+ },+ )+ .await+ })+ .await,+ ),};- landed(report.service);Some(report)};@@ -258,44 +352,46 @@ async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&streturn None;}let report = match ctx.cloudkit.as_deref() {- None => unavailable("notes", "Notes"),- Some(root) => {- // One notes pass at a time, from reading the position through storing what came- // back — see `NOTES_PASS`.- let _one = crate::NOTES_PASS.lock().await;- let since = token(data, "notes").await;- let full = since.is_none();- let fetched = quince_icloud::notes::fetch_cloudkit(- &ctx.http,- root,- ctx.params.clone(),- since.as_deref(),- )- .await;- store(- "notes",- "Notes",- data,- ctx.account.as_deref(),- fetched,- move |library, f| {- let (folders, notes, _) = quince_icloud::notes::apply_cloudkit(library, f)?;- // Edits still on their way to Apple stay as typed here.- crate::outbox::replay(library, crate::outbox::NOTES)?;- if full {- library.mark_full_sync("notes")?;- }- Ok(format!(- "{notes} note{} in {folders} folder{}",- plural(notes),- plural(folders)- ))- },- )- .await- }+ None => arrived((unavailable("notes", "Notes"), true)),+ Some(root) => arrived(+ once("notes", &crate::NOTES_PASS, || async {+ // One notes pass at a time, from reading the position through storing what came+ // back — see `NOTES_PASS`.+ let since = token(data, "notes").await;+ let full = since.is_none();+ let fetched = quince_icloud::notes::fetch_cloudkit(+ &ctx.http,+ root,+ ctx.params.clone(),+ since.as_deref(),+ )+ .await;+ store(+ "notes",+ "Notes",+ data,+ ctx.account.as_deref(),+ fetched,+ move |library, f| {+ let (folders, notes, _) =+ quince_icloud::notes::apply_cloudkit(library, f)?;+ // Edits still on their way to Apple stay as typed here.+ crate::outbox::replay(library, crate::outbox::NOTES)?;+ if full {+ library.mark_full_sync("notes")?;+ }+ Ok(format!(+ "{notes} note{} in {folders} folder{}",+ plural(notes),+ plural(folders)+ ))+ },+ )+ .await+ })+ .await,+ ),};- landed(report.service);Some(report)};@@ -304,92 +400,100 @@ async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&streturn None;}let report = match ctx.contacts.as_deref() {- None => unavailable("contacts", "Contacts"),- Some(root) => {- // Fetch and store as one, never across a save: see `CONTACTS_PASS`. The book- // is read only when Apple says it changed (contacts::pass).- let _one = crate::CONTACTS_PASS.lock().await;- let since = token(data, "contacts")- .await- .and_then(|t| quince_icloud::contacts::Tokens::from_stored(&t));- let full = since.is_none();- let fetched =- quince_icloud::contacts::pass(&ctx.http, root, ctx.params.clone(), since).await;- store(- "contacts",- "Contacts",- data,- ctx.account.as_deref(),- fetched,- move |library, pass| {- use quince_icloud::contacts::Pass;- let count = match pass {- Pass::Read(book, tokens) => {- let n = quince_icloud::contacts::apply(library, book)?;- library.mark_synced("contacts", Some(&tokens.stored()))?;- n- }- Pass::Unchanged(tokens) => {- library.mark_synced("contacts", Some(&tokens.stored()))?;- let n: i64 = library- .conn()- .query_row("SELECT count(*) FROM contacts", [], |r| r.get(0))- .map_err(quince_core::Error::from)?;- usize::try_from(n).unwrap_or(0)+ None => arrived((unavailable("contacts", "Contacts"), true)),+ Some(root) => arrived(+ once("contacts", &crate::CONTACTS_PASS, || async {+ // Fetch and store as one, never across a save: see `CONTACTS_PASS`. The book+ // is read only when Apple says it changed (contacts::pass).+ let since = token(data, "contacts")+ .await+ .and_then(|t| quince_icloud::contacts::Tokens::from_stored(&t));+ let full = since.is_none();+ let fetched =+ quince_icloud::contacts::pass(&ctx.http, root, ctx.params.clone(), since)+ .await;+ store(+ "contacts",+ "Contacts",+ data,+ ctx.account.as_deref(),+ fetched,+ move |library, pass| {+ use quince_icloud::contacts::Pass;+ let count = match pass {+ Pass::Read(book, tokens) => {+ let n = quince_icloud::contacts::apply(library, book)?;+ library.mark_synced("contacts", Some(&tokens.stored()))?;+ n+ }+ Pass::Unchanged(tokens) => {+ library.mark_synced("contacts", Some(&tokens.stored()))?;+ let n: i64 = library+ .conn()+ .query_row("SELECT count(*) FROM contacts", [], |r| {+ r.get(0)+ })+ .map_err(quince_core::Error::from)?;+ usize::try_from(n).unwrap_or(0)+ }+ };+ // Cards saved here and still on their way to Apple stay as saved.+ crate::outbox::replay(library, crate::outbox::CONTACTS)?;+ if full {+ library.mark_full_sync("contacts")?;}- };- // Cards saved here and still on their way to Apple stay as saved.- crate::outbox::replay(library, crate::outbox::CONTACTS)?;- if full {- library.mark_full_sync("contacts")?;- }- Ok(format!("{count} contact{}", plural(count)))- },- )- .await- }+ Ok(format!("{count} contact{}", plural(count)))+ },+ )+ .await+ })+ .await,+ ),};- landed(report.service);Some(report)};// Drive is read a folder at a time as it is opened; the pass keeps its top level current,// so what changed there on another device is here before anyone looks, and its freshness- // is stated with the rest. Fetched and stored as one — see `DRIVE_PASS`.+ // is stated with the rest. Fetched and stored as one — see `drive_pass`.let drive = async {if !wanted("drive") {return None;}let report = match ctx.drive.as_deref() {- None => unavailable("drive", "Drive"),+ None => arrived((unavailable("drive", "Drive"), true)),Some(root) => {- let _one = crate::DRIVE_PASS.lock().await;- let fetched = quince_icloud::drive::fetch_folder(- &ctx.http,- root,- ctx.params.clone(),- quince_core::drive::ROOT,- )- .await;- store(- "drive",- "Drive",- data,- ctx.account.as_deref(),- fetched,- |library, items| {- let n = quince_icloud::drive::store_folder(- library,+ let pass = crate::drive_pass(quince_core::drive::ROOT);+ arrived(+ once("drive", &pass, || async {+ let fetched = quince_icloud::drive::fetch_folder(+ &ctx.http,+ root,+ ctx.params.clone(),quince_core::drive::ROOT,- &items,- )?;- Ok(format!("{n} item{} at the top of iCloud Drive", plural(n)))- },+ )+ .await;+ store(+ "drive",+ "Drive",+ data,+ ctx.account.as_deref(),+ fetched,+ |library, items| {+ let n = quince_icloud::drive::store_folder(+ library,+ quince_core::drive::ROOT,+ &items,+ )?;+ Ok(format!("{n} item{} at the top of iCloud Drive", plural(n)))+ },+ )+ .await+ })+ .await,)- .await}};- landed(report.service);Some(report)};@@ -400,46 +504,64 @@ async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&streturn None;}let report = match ctx.cloudkit.as_deref() {- None => unavailable("photos", "Photos"),- Some(root) => {- let _one = crate::PHOTOS_PASS.lock().await;- let since = token(data, quince_icloud::photos::SERVICE).await;- let full = since.is_none();- let fetched = quince_icloud::photos::fetch(- &ctx.http,- root,- ctx.params.clone(),- since.as_deref(),- )- .await;- store(- "photos",- "Photos",- data,- ctx.account.as_deref(),- fetched,- move |library, f| {- let (_, links) = quince_icloud::photos::apply(library, f)?;- // Marked, or every pass after walked the whole library again: the- // daily full read is due when none was ever recorded.- if full {- library.mark_full_sync(quince_icloud::photos::SERVICE)?;+ None => arrived((unavailable("photos", "Photos"), true)),+ Some(root) => arrived(+ once("photos", &crate::PHOTOS_PASS, || async {+ // A large library is read a few pages at a time, each stored before the next+ // is asked for: the walk holds no more of it in memory than those pages, and+ // goes as far in one pass as it did when it held all of them at once.+ let mut changed = false;+ let mut rounds = 0;+ loop {+ rounds += 1;+ let since = token(data, quince_icloud::photos::SERVICE).await;+ let full = since.is_none();+ let fetched = quince_icloud::photos::fetch(+ &ctx.http,+ root,+ ctx.params.clone(),+ since.as_deref(),+ )+ .await;+ let more = fetched.as_ref().is_ok_and(|f| !f.complete);+ let mut report = store(+ "photos",+ "Photos",+ data,+ ctx.account.as_deref(),+ fetched,+ move |library, f| {+ let (_, links) = quince_icloud::photos::apply(library, f)?;+ // Marked, or every pass after walked the whole library again:+ // the daily full read is due when none was ever recorded.+ if full {+ library.mark_full_sync(quince_icloud::photos::SERVICE)?;+ }+ crate::photos::remember(links);+ // Counted, not read: every row of the library loaded to say+ // how many there were held the library's lock through each pass.+ let n = library+ .photo_counts()?+ .into_iter()+ .find(|(v, _)| *v == quince_core::photos::View::Library)+ .map_or(0, |(_, n)| usize::try_from(n).unwrap_or(0));+ Ok(format!("{n} photo{}", plural(n)))+ },+ )+ .await;+ changed |= report.changed;+ if report.outcome != Outcome::Ok+ || !more+ || rounds >= quince_icloud::photos::PASS_ROUNDS+ {+ report.changed = changed;+ break report;}- crate::photos::remember(links);- // Counted, not read: every row of the library loaded to say how many- // there were held the library's lock through each pass.- let n = library- .photo_counts()?- .into_iter()- .find(|(v, _)| *v == quince_core::photos::View::Library)- .map_or(0, |(_, n)| usize::try_from(n).unwrap_or(0));- Ok(format!("{n} photo{}", plural(n)))- },- )- .await- }+ }+ })+ .await,+ ),};- landed(report.service);Some(report)};@@ -462,15 +584,22 @@ async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&st.filter(|r| r.outcome != Outcome::Unavailable).map(|r| r.service).collect();- let as_of = {- let library = data.lock().await;- library.oldest_sync(&available).ok().flatten()- };+ let as_of = data+ .read(|library| library.oldest_sync(&available))+ .await+ .ok()+ .flatten();+ let changed = reports+ .iter()+ .filter(|r| r.changed)+ .map(|r| r.service)+ .collect();Refresh {services: reports,as_of,took_ms: u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX),+ changed: Some(changed),}}@@ -478,9 +607,19 @@ async fn run(client: &Arc<Mutex<Client>>, data: &Mutex<Library>, services: &[&st////// The window renders from this the instant it opens, so freshness is on screen before the/// first refresh has finished rather than after it.-pub async fn status(data: &Mutex<Library>) -> Refresh {- let library = data.lock().await;- let health = library.sync_health().unwrap_or_default();+pub async fn status(data: &LibraryLock) -> Refresh {+ let covered: Vec<&str> = COVERED.iter().map(|(service, _)| *service).collect();+ // Read beside a pass, not behind it: asked as each service lands, it used to wait for the+ // next service's store to finish.+ let (health, as_of) = data+ .read(|library| {+ Ok((+ library.sync_health().unwrap_or_default(),+ library.oldest_sync(&covered).ok().flatten(),+ ))+ })+ .await+ .unwrap_or_default();let services = COVERED.iter().map(|(service, label)| {@@ -496,14 +635,15 @@ pub async fn status(data: &Mutex<Library>) -> Refresh {},detail: error,last_synced_at: found.and_then(|h| h.last_synced_at),+ changed: false,}}).collect();- let covered: Vec<&str> = COVERED.iter().map(|(service, _)| *service).collect();Refresh {services,- as_of: library.oldest_sync(&covered).ok().flatten(),+ as_of,took_ms: 0,+ changed: None,}}@@ -522,30 +662,40 @@ fn unavailable(service: &'static str, label: &'static str) -> ServiceReport {outcome: Outcome::Unavailable,detail: None,last_synced_at: None,+ changed: false,}}/// Where the last sync got to — or `None` when this service is due a full pass, which is/// what makes the next fetch walk everything.-async fn token(data: &Mutex<Library>, service: &str) -> Option<String> {- let library = data.lock().await;- if library- .needs_full_sync(service, FULL_SYNC_EVERY)- .unwrap_or(false)- {- return None;- }- library.sync_token(service).ok().flatten()+async fn token(data: &LibraryLock, service: &str) -> Option<String> {+ // Read beside the other services' stores, which the passes run alongside.+ data.read(|library| {+ if library+ .needs_full_sync(service, FULL_SYNC_EVERY)+ .unwrap_or(false)+ {+ return Ok(None);+ }+ Ok(library.sync_token(service).ok().flatten())+ })+ .await+ .ok()+ .flatten()}+/// What every pass writes whatever came back: where it got to, and when.+const BOOKKEEPING: &[&str] = &["sync_state"];+/// Store what a fetch returned, and turn either half's failure into a report.////// The library is taken only here — after the network work is done — and released as soon-/// as the rows are in.+/// as the rows are in. Taken as a pass takes it: what it stores is one transaction, and the+/// screens read beside it meanwhile (`LibraryLock::lock_for_pass`).async fn store<T, F>(service: &'static str,label: &'static str,- data: &Mutex<Library>,+ data: &LibraryLock,account: Option<&str>,fetched: quince_icloud::Result<T>,apply: F,@@ -553,7 +703,7 @@ async fn store<T, F>(whereF: FnOnce(&mut Library, T) -> quince_icloud::Result<String>,{- let mut library = data.lock().await;+ let mut library = data.lock_for_pass().await;// Fetched for an account that is no longer the library's — signed out, or another signed// in, while it was on the way: dropped, never laid over someone else's data.let owner = library.state(crate::ACCOUNT_KEY).ok().flatten();@@ -564,6 +714,7 @@ whereoutcome: Outcome::Failed,detail: Some("Signed out while this was being read.".into()),last_synced_at: None,+ changed: false,};}// SQLite is synchronous: a full pass's apply (a whole photo library, every reminder and its@@ -573,8 +724,12 @@ wherelet started = std::time::Instant::now();// And as one change: the records, the changes still waiting laid over them and the new// position land together, or — failing, or the app dying part way — none of them do.- let result = tokio::task::block_in_place(|| {- fetched.and_then(|value| library.atomically(|library| apply(library, value)))+ // Whether it stored anything beyond its position is noted on the way, so a pass that found+ // nothing new does not have every screen read its whole list again.+ let (result, changed) = tokio::task::block_in_place(|| {+ library.changing(BOOKKEEPING, |library| {+ fetched.and_then(|value| library.atomically(|library| apply(library, value)))+ })});let took = started.elapsed();if took > std::time::Duration::from_millis(50) {@@ -587,6 +742,7 @@ whereoutcome: Outcome::Ok,detail: Some(detail),last_synced_at: library.last_synced(service).ok().flatten(),+ changed,},Err(e) => {let message = e.to_string();@@ -597,6 +753,7 @@ whereoutcome: Outcome::Failed,detail: Some(message),last_synced_at: library.last_synced(service).ok().flatten(),+ changed: false,}}}@@ -649,4 +806,57 @@ mod tests {assert_eq!(plural(0), "s");assert_eq!(plural(2), "s");}++ /// Five asks while a pass is running cost that pass and one more, not five: each that+ /// waited takes the report of the pass that started after it asked. An ask after that+ /// one has finished runs a pass of its own.+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]+ async fn a_burst_of_asks_costs_the_running_pass_and_one_more() {+ use std::sync::atomic::AtomicUsize;+ static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());+ static RUNS: AtomicUsize = AtomicUsize::new(0);+ let gate = Arc::new(tokio::sync::Notify::new());+ let pass = |gate: Option<Arc<tokio::sync::Notify>>| {+ || async move {+ let n = RUNS.fetch_add(1, Ordering::SeqCst) + 1;+ if let Some(gate) = gate {+ gate.notified().await;+ }+ let mut report = unavailable("contacts", "Contacts");+ report.detail = Some(format!("pass {n}"));+ report+ }+ };+ let first = {+ let gate = Arc::clone(&gate);+ tokio::spawn(async move { once("contacts", &LOCK, pass(Some(gate))).await })+ };+ while RUNS.load(Ordering::SeqCst) == 0 {+ tokio::task::yield_now().await;+ }+ let waiting: Vec<_> = (0..5)+ .map(|_| tokio::spawn(async move { once("contacts", &LOCK, pass(None)).await }))+ .collect();+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;+ gate.notify_one();+ let (report, ran) = first.await.unwrap();+ assert!(ran);+ assert_eq!(report.detail.as_deref(), Some("pass 1"));+ let mut ran_here = 0;+ for w in waiting {+ let (report, ran) = w.await.unwrap();+ ran_here += usize::from(ran);+ assert_eq!(+ report.detail.as_deref(),+ Some("pass 2"),+ "every waiting ask is answered by the pass after the one running"+ );+ }+ assert_eq!(ran_here, 1);+ assert_eq!(RUNS.load(Ordering::SeqCst), 2);++ let (report, ran) = once("contacts", &LOCK, pass(None)).await;+ assert!(ran, "an ask after the burst runs its own pass");+ assert_eq!(report.detail.as_deref(), Some("pass 3"));+ }}diff --git a/src-tauri/src/reminders_cmds.rs b/src-tauri/src/reminders_cmds.rsindex afd1957..11c4903 100644--- a/src-tauri/src/reminders_cmds.rs+++ b/src-tauri/src/reminders_cmds.rs@@ -19,8 +19,10 @@ fn now() -> i64 {/// One reminder's tags, link and early alerts.#[tauri::command]pub async fn reminder_detail(guid: String, data: State<'_, Data>) -> UiResult<ReminderDetail> {- let library = data.0.lock().await;- library.reminder_detail(&guid).map_err(|e| e.to_string())+ data.0+ .read(|library| library.reminder_detail(&guid))+ .await+ .map_err(|e| e.to_string())}/// The rest of one reminder's Details: its repeat whole, time zone, a place, the person it@@ -219,30 +221,39 @@ async fn read_urgent(app: tauri::AppHandle, unread: Vec<(String, String)>) {/// Every tag on the account, with how many reminders still to do carry each.#[tauri::command]pub async fn reminder_tags(data: State<'_, Data>) -> UiResult<Vec<TagCount>> {- let library = data.0.lock().await;- library.reminder_tags().map_err(|e| e.to_string())+ data.0+ .read(|library| library.reminder_tags())+ .await+ .map_err(|e| e.to_string())}/// Reminders still to do carrying a tag.#[tauri::command]pub async fn reminders_tagged(name: String, data: State<'_, Data>) -> UiResult<Vec<Reminder>> {- let library = data.0.lock().await;- library.reminders_tagged(&name).map_err(|e| e.to_string())+ data.0+ .read(|library| library.reminders_tagged(&name))+ .await+ .map_err(|e| e.to_string())}/// Each reminder still to do with its tags, and how many each custom smart list holds: the/// sidebar's and the rows' tag chips in one read, rather than one per tag and per list.#[tauri::command]pub async fn reminder_extras(data: State<'_, Data>) -> UiResult<ReminderExtras> {- let library = data.0.lock().await;- library.reminder_extras(now()).map_err(|e| e.to_string())+ let at = now();+ data.0+ .read(|library| library.reminder_extras(at))+ .await+ .map_err(|e| e.to_string())}/// The account's smart lists, built-in and custom.#[tauri::command]pub async fn reminder_smart_lists(data: State<'_, Data>) -> UiResult<Vec<ReminderSmartList>> {- let library = data.0.lock().await;- library.reminder_smart_lists().map_err(|e| e.to_string())+ data.0+ .read(|library| library.reminder_smart_lists())+ .await+ .map_err(|e| e.to_string())}/// What a custom smart list holds now, and any part of its filter not understood.@@ -251,9 +262,10 @@ pub async fn reminders_in_smart_list(guid: String,data: State<'_, Data>,) -> UiResult<SmartListContents> {- let library = data.0.lock().await;- library- .reminders_in_smart_list(&guid, now())+ let at = now();+ data.0+ .read(|library| library.reminders_in_smart_list(&guid, at))+ .await.map_err(|e| e.to_string())}diff --git a/src/lib/ipc.ts b/src/lib/ipc.tsindex 53c9e5e..43ec8ef 100644--- a/src/lib/ipc.ts+++ b/src/lib/ipc.ts@@ -43,6 +43,11 @@ export type Refresh = {/** The stalest part of the library — the only freshness the window can claim. */asOf: number | null;tookMs: number;+ /**+ * The services whose stored rows this changed, when the backend knows: a screen reads again+ * only for its own. Absent, every screen reads again.+ */+ changed?: string[];};/** Emitted by the backend whenever a refresh finishes, background ones included. */diff --git a/src/lib/sync.tsx b/src/lib/sync.tsxindex ada2dc2..c9a6ce2 100644--- a/src/lib/sync.tsx+++ b/src/lib/sync.tsx@@ -26,6 +26,8 @@ type Sync = {syncing: boolean;/** Ticks once per completed pass. Screens re-read when it changes. */revision: number;+ /** Ticks per service whose rows a pass changed; `all` for a change not placed. */+ revisions: Readonly<Record<string, number>>;/** Ask for a pass now. Safe to call while one is running; it will not start a second. */refresh: () => Promise<void>;/** The last refresh that failed outright, rather than a single service reporting trouble. */@@ -49,7 +51,22 @@ export function SyncProvider({ children }: { children: ReactNode }) {const [report, setReport] = useState<Refresh | null>(null);const [syncing, setSyncing] = useState(false);const [revision, setRevision] = useState(0);+ const [revisions, setRevisions] = useState<Record<string, number>>({});const [error, setError] = useState<string | null>(null);+ /**+ * A pass landed. With the services it changed named, only their screens read again — and+ * none, when it stored nothing new: a clean pass landing every service told every open+ * screen to read its whole list seven times. Unnamed, every screen reads again.+ */+ const bump = useCallback((changed?: readonly string[]) => {+ if (Array.isArray(changed) && changed.length === 0) return;+ setRevision((n) => n + 1);+ setRevisions((was) => {+ const next = { ...was };+ for (const s of Array.isArray(changed) ? changed : ["all"]) next[s] = (next[s] ?? 0) + 1;+ return next;+ });+ }, []);// A second click while a pass is running would mean two walks of the same zones.const inFlight = useRef(false);const [updating, setUpdating] = useState<string[]>([]);@@ -63,25 +80,32 @@ export function SyncProvider({ children }: { children: ReactNode }) {setSyncing(true);setPassStartedAt(Math.floor(Date.now() / 1000));setError(null);+ // Each service that stored something says so as it lands (the backend's event); a pass+ // that cannot say so has every screen read again, as before.+ let told = false;try {- setReport(await api.syncAll());+ const done = await api.syncAll();+ told = Array.isArray(done?.changed);+ setReport(done);} catch (e) {setError(messageOf(e));} finally {inFlight.current = false;setSyncing(false);setPassStartedAt(null);- setRevision((n) => n + 1);+ if (!told) bump();}- }, []);+ }, [bump]);const refreshService = useCallback(async (service: string) => {if (serviceInFlight.current.has(service)) return;serviceInFlight.current.add(service);setUpdating([...serviceInFlight.current]);setServiceErrors(({ [service]: _gone, ...rest }) => rest);+ let told = false;try {const part = await api.syncSome([service]);+ told = Array.isArray(part?.changed);// The backend's event brings the whole picture; this keeps the answer on screen even// where no event arrives, by folding this service's own report into the last one.setReport((was) =>@@ -101,9 +125,9 @@ export function SyncProvider({ children }: { children: ReactNode }) {} finally {serviceInFlight.current.delete(service);setUpdating([...serviceInFlight.current]);- setRevision((n) => n + 1);+ if (!told) bump();}- }, []);+ }, [bump]);useEffect(() => {let cancelled = false;@@ -128,7 +152,7 @@ export function SyncProvider({ children }: { children: ReactNode }) {const stop = listen<Refresh>(SYNCED, (event) => {if (cancelled) return;setReport(event.payload);- setRevision((n) => n + 1);+ bump(event.payload?.changed);});/*@@ -154,11 +178,11 @@ export function SyncProvider({ children }: { children: ReactNode }) {window.removeEventListener("focus", onReturn);document.removeEventListener("visibilitychange", onReturn);};- }, [refresh]);+ }, [refresh, bump]);const value = useMemo(- () => ({ report, syncing, revision, refresh, error, refreshService, updating, serviceErrors, passStartedAt }),- [report, syncing, revision, refresh, error, refreshService, updating, serviceErrors, passStartedAt]+ () => ({ report, syncing, revision, revisions, refresh, error, refreshService, updating, serviceErrors, passStartedAt }),+ [report, syncing, revision, revisions, refresh, error, refreshService, updating, serviceErrors, passStartedAt]);return <Context.Provider value={value}>{children}</Context.Provider>;}@@ -169,6 +193,16 @@ export function useSync(): Sync {return found;}+/**+ * A revision for the screen that shows these services: it moves when a pass changed one of+ * them (or changed something it could not name), and stays put for passes that changed only+ * what another screen shows.+ */+export function useRevision(...services: string[]): number {+ const { revisions } = useSync();+ return services.reduce((n, s) => n + (revisions[s] ?? 0), revisions.all ?? 0);+}+/** "just now", "4 min ago", "yesterday" — how old the library is, in words. */export function freshness(asOf: number | null): string {if (asOf === null) return "Not synced yet";diff --git a/src/screens/ContactsView.tsx b/src/screens/ContactsView.tsxindex 128eb49..b43616e 100644--- a/src/screens/ContactsView.tsx+++ b/src/screens/ContactsView.tsx@@ -23,7 +23,7 @@ import {type PostalAddress,} from "../lib/ipc";import { Arriving } from "../components/EmptyState";-import { useArriving, useSync } from "../lib/sync";+import { useArriving, useRevision } from "../lib/sync";import { overlayOpen, useActiveRef, useScreenActive } from "../lib/screen";import { Picker } from "../components/Picker";import { RowMenu, type MenuEntry } from "../components/RowMenu";@@ -158,7 +158,7 @@ export function ContactsView() {const [error, setError] = useState<string | null>(null);useErrorToast(error, () => setError(null));const [confirm, confirmSheet] = useConfirm();- const { revision } = useSync();+ const revision = useRevision("contacts");// A pass that lands while another screen is in front is read when this one comes back, once,// not at every landing: a screen out of sight stays mounted (lib/screen).const active = useScreenActive();@@ -282,6 +282,12 @@ export function ContactsView() {/** Cards saved here and not yet at Apple, and any Apple turned down. */const outbox = useOutbox("contacts");+ // A card sent while another screen is in front is read when this one comes back, once — as+ // a landed pass is (`due`): out of sight, each send read the whole book, its duplicates and+ // the open card's addresses again, on the lock the screen in front was waiting for.+ const seenTick = useRef(outbox.tick);+ if (active) seenTick.current = outbox.tick;+ const tickDue = seenTick.current;/** Each read is numbered: one overtaken by a later one (a key typed since) never draws. */const reads = useRef(0);const read = useCallback(async (query: string) => {@@ -308,7 +314,7 @@ export function ContactsView() {};// Re-reading on every keystroke is the point: the filter runs in SQLite. `outbox.tick`: a// card sent, or turned down, changes what the library holds.- }, [read, search, due, outbox.tick]);+ }, [read, search, due, tickDue]);const readLists = useCallback(async () => {const [found, mine] = await Promise.all([api.contactLists(), api.contactMe()]);@@ -379,7 +385,7 @@ export function ContactsView() {}, []);useEffect(() => {readDupes().catch(() => {});- }, [readDupes, due, outbox.tick]);+ }, [readDupes, due, tickDue]);/** Everyone by id, for the duplicates view to draw from. */const byGuid = useMemo(() => new Map(contacts.map((c) => [c.guid, c])), [contacts]);@@ -447,7 +453,7 @@ export function ContactsView() {return () => {cancelled = true;};- }, [openGuid, due, outbox.tick]);+ }, [openGuid, due, tickDue]);/*** Address edits: drawn at once, then sent. Turned down, the list goes back to what it was,diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsxindex 87da50b..1f58c47 100644--- a/src/screens/DriveView.tsx+++ b/src/screens/DriveView.tsx@@ -1,7 +1,7 @@import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";import { overlayOpen, useActiveRef, useScreenActive } from "../lib/screen";import { useModalFocus } from "../lib/focus";-import { useSync } from "../lib/sync";+import { useRevision } from "../lib/sync";import { toastSaved, useErrorToast } from "../components/Toast";import { useConfirm } from "../components/Confirm";import { EmptyState } from "../components/EmptyState";@@ -447,6 +447,8 @@ export function DriveView() {const [inRecents, setInRecents] = useState(false);/** Apple's own pictures of this folder's files, where it has them; the rest keep icons. */const [thumbs, setThumbs] = useState<Record<string, string>>({});+ /** The folder whose pictures have been asked for and answered since it was opened. */+ const thumbsOf = useRef<string | null>(null);/** What is free, as icloud.com says it under a folder's name: "5.18 GB available". */const [available, setAvailable] = useState<string | null>(null);useEffect(() => {@@ -600,6 +602,7 @@ export function DriveView() {// Drive seemed to jump into folders nobody opened.setItems(Array.isArray(cached) ? cached : []);setThumbs({});+ thumbsOf.current = null;await api.driveSyncFolder(folderId);if (token !== openToken.current) return;const fresh = await api.driveChildren(folderId);@@ -607,7 +610,12 @@ export function DriveView() {setItems(Array.isArray(fresh) ? fresh : []);// Pictures last, and never in the way: a folder is usable before they arrive.api.driveThumbnails(folderId).then(- (t) => { if (token === openToken.current && t) setThumbs(t); },+ (t) => {+ if (token === openToken.current && t) {+ thumbsOf.current = folderId;+ setThumbs(t);+ }+ },() => {},);} catch (e) {@@ -661,9 +669,18 @@ export function DriveView() {// Redrawn only when something in it changed: a check that found nothing new leaves the// rows, their selection and their pictures exactly as they were.const next = Array.isArray(fresh) ? fresh : [];+ const same = sameItems(itemsRef.current, next);setItems((was) => (sameItems(was, next) ? was : next));+ // Pictures only when something in the folder changed (or they never came): a check that+ // found nothing new already has every picture it had, and asking again every ten seconds+ // redrew every row with them.+ if (same && thumbsOf.current === folderId) return;api.driveThumbnails(folderId).then(- (t) => { if (token === openToken.current && t) setThumbs((was) => ({ ...was, ...t })); },+ (t) => {+ if (token !== openToken.current || !t) return;+ thumbsOf.current = folderId;+ setThumbs((was) => (Object.entries(t).every(([id, src]) => was[id] === src) ? was : { ...was, ...t }));+ },() => {},);} catch {@@ -699,7 +716,7 @@ export function DriveView() {window.removeEventListener("focus", look);};}, [shown, refresh]);- const { revision } = useSync();+ const revision = useRevision("drive");// A pass that lands while another screen is in front is read when this one comes back, once// (a screen out of sight stays mounted): each landing asked Apple for the folder again.const seen = useRef(revision);diff --git a/src/screens/NotesView.tsx b/src/screens/NotesView.tsxindex 57c7814..8c9b282 100644--- a/src/screens/NotesView.tsx+++ b/src/screens/NotesView.tsx@@ -26,7 +26,7 @@ import { NotePicture } from "./notes/NotePicture";import { OPEN_NOTE_EVENT } from "./notes/links";import { useReveal } from "../lib/reveal";import { Arriving } from "../components/EmptyState";-import { useArriving, useSync } from "../lib/sync";+import { useArriving, useRevision } from "../lib/sync";import { Picker } from "../components/Picker";const ALL = "__all__";@@ -140,7 +140,7 @@ export function NotesView() {const [error, setError] = useState<string | null>(null);useErrorToast(error, () => setError(null));const [confirm, confirmSheet] = useConfirm();- const { revision } = useSync();+ const revision = useRevision("notes");const arriving = useArriving("notes");// Tables on screen keep up with a phone's edits, as the note's text does.useEffect(() => {diff --git a/src/screens/PhotosView.tsx b/src/screens/PhotosView.tsxindex e3c8355..88fd7e2 100644--- a/src/screens/PhotosView.tsx+++ b/src/screens/PhotosView.tsx@@ -27,7 +27,7 @@ import {import { Arriving, EmptyState } from "../components/EmptyState";import { SearchField } from "../components/SearchField";import { PaneTitle } from "../components/PaneTitle";-import { useArriving, useSync } from "../lib/sync";+import { useArriving, useRevision } from "../lib/sync";import { toast, toastSaved, useErrorToast, useToastClearance } from "../components/Toast";import { UploadPanel } from "./photos/UploadPanel";import { RowMenu, type MenuEntry } from "../components/RowMenu";@@ -661,7 +661,7 @@ const Month = memo(function Month({}, sameMonth);export function PhotosView() {- const { revision } = useSync();+ const revision = useRevision("photos");const arriving = useArriving("photos");const [view, setView] = useState<PhotoView>("library");/** An album open instead of a view. */diff --git a/src/screens/RemindersView.tsx b/src/screens/RemindersView.tsxindex 83a6d95..afd1ff5 100644--- a/src/screens/RemindersView.tsx+++ b/src/screens/RemindersView.tsx@@ -36,7 +36,7 @@ import {type UrgentRead,} from "../lib/ipc";import { Arriving } from "../components/EmptyState";-import { useArriving, useSync } from "../lib/sync";+import { useArriving, useRevision } from "../lib/sync";import { Picker } from "../components/Picker";import { RowMenu } from "../components/RowMenu";import { Glyph, emblemGlyph, type GlyphName } from "../components/Glyph";@@ -395,7 +395,7 @@ export function RemindersView() {const [error, setError] = useState<string | null>(null);useErrorToast(error, () => setError(null));const [confirm, confirmSheet] = useConfirm();- const { revision } = useSync();+ const revision = useRevision("reminders");const arriving = useArriving("reminders");/** What each sidebar entry is holding. Apple puts a number on every one. */const [counts, setCounts] = useState<ReminderCounts | null>(null);diff --git a/src/screens/calendar/CalendarView.tsx b/src/screens/calendar/CalendarView.tsxindex d5478b3..a20a0a4 100644--- a/src/screens/calendar/CalendarView.tsx+++ b/src/screens/calendar/CalendarView.tsx@@ -22,7 +22,7 @@ import {calendarFeedsApi,SUBSCRIPTION_PREFIX } from "../../lib/ipc";import { parseQuick } from "./quick";-import { useSync } from "../../lib/sync";+import { useRevision } from "../../lib/sync";import { useOutbox } from "../../lib/outbox";import { Unsent, Waiting } from "../../components/Unsent";import { Picker } from "../../components/Picker";@@ -463,7 +463,7 @@ export function CalendarView() {/** The last event deleted, with its alerts (read from Apple as it goes), held so the deletion* can be taken back. */const [undo, setUndo] = useState<(CalendarEvent & { alerts: Promise<number[]> }) | null>(null);- const { revision } = useSync();+ const revision = useRevision("calendar", "reminders", "contacts");// Changes kept here and not yet at Apple (the outbox): a saved event says so, and one Apple// turned down is shown with what it held.const outbox = useOutbox("calendar");