fix(settings): a read of the archive's settings never undoes an edit saved meanwhile

Settings › Archive reads archive.json without the settings lease each time it
is shown and after a waited edit. On a slow share such a read could return
the file as it was before a template, proxy or thumbnail edit that was being
written, then put the old value back on the row and in this PC's defaults
while the archive kept the edit. ArchiveReadGate now drops a read that
overlapped an edit being written; the rows are read again at once, or when
the last edit being written ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 19:00:45 +00:00
1 parent 93054ba5de
commit 59d49f16af
3 files changed
+167 -9

No files matched your search

@@ -0,0 +1,56 @@
namespace Titano.App.Settings;
/// <summary>
/// Orders Settings › Archive's reads of the open archive's settings (its archive.json and index row) against the edits
/// written into them. A read that overlapped a write may hold the file as it was before the edit (a share waking up
/// answers late, and reads take no settings lease): showing it would undo the edit on the rows and in this PC's defaults
/// while the archive keeps it. Such a read is dropped, and the rows are read again once no edit is being written: at
/// once when none is left (<see cref="IsCurrent"/>), else when the last one ends (<see cref="EndWrite"/>). Used from the
/// interface thread only.
/// </summary>
public sealed class ArchiveReadGate
{
/// <summary>The token of a read begun while an edit was being written: never current.</summary>
private const int Overlapped = -1;
private int _begun;
private int _writing;
private bool _readAfterWrites;
/// <summary>An edit starts being written into the archive.</summary>
public void BeginWrite()
{
_begun++;
_writing++;
}
/// <summary>
/// The edit was written, or failed. True when the rows must be read again now: a read it overlapped was dropped and
/// no other edit is being written.
/// </summary>
public bool EndWrite()
{
if (_writing == 0) throw new InvalidOperationException("No edit is being written.");
_writing--;
if (_writing > 0 || !_readAfterWrites) return false;
_readAfterWrites = false;
return true;
}
/// <summary>A read starts; its token tells <see cref="IsCurrent"/> which edits it may have missed.</summary>
public int BeginRead() => _writing > 0 ? Overlapped : _begun;
/// <summary>
/// Whether what a read begun with <paramref name="token"/> found may be shown: no edit was being written when it began
/// and none began since. When it may not, the rows are read again: now (<paramref name="readAgain"/>) when every edit
/// it overlapped is written, else once the last one ends.
/// </summary>
public bool IsCurrent(int token, out bool readAgain)
{
readAgain = false;
if (token == _begun) return true;
if (_writing > 0) _readAfterWrites = true;
else readAgain = true;
return false;
}
}