A read of the open archive's settings that an Archive row edit overlapped was dropped and left to the edit's end, so a folder change could complete while the rows still showed the previous archive. The read now waits until no edit is being written and reads again, so the retarget and the reload on showing Settings are done only once the rows show what the archive holds. A smoke test edits a row while Settings reads the archive, with the settings lease held elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
53 lines
2.4 KiB
C#
53 lines
2.4 KiB
C#
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 not shown (<see cref="IsCurrent"/>): its reader waits until no edit is being
|
||
/// written (<see cref="WrittenAsync"/>) and reads again, so a read is done only once the rows show what the archive holds.
|
||
/// 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 TaskCompletionSource? _written;
|
||
|
||
/// <summary>An edit starts being written into the archive.</summary>
|
||
public void BeginWrite()
|
||
{
|
||
_begun++;
|
||
_writing++;
|
||
}
|
||
|
||
/// <summary>The edit was written, or failed. When it was the last one being written, the reads waiting for it go on.</summary>
|
||
public void EndWrite()
|
||
{
|
||
if (_writing == 0) throw new InvalidOperationException("No edit is being written.");
|
||
if (--_writing > 0) return;
|
||
var written = _written;
|
||
_written = null;
|
||
written?.SetResult();
|
||
}
|
||
|
||
/// <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, its reader awaits <see cref="WrittenAsync"/> and reads again.
|
||
/// </summary>
|
||
public bool IsCurrent(int token) => token == _begun;
|
||
|
||
/// <summary>
|
||
/// Done once no edit is being written: at once when none is, else when the last one ends. Its continuations run after
|
||
/// <see cref="EndWrite"/> returns, never inside it.
|
||
/// </summary>
|
||
public Task WrittenAsync() =>
|
||
_writing == 0 ? Task.CompletedTask : (_written ??= new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously)).Task;
|
||
}
|