feat(app): editor session, project history, local source, preview renderer and export planner
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
19 files changed
+2146
No files matched your search
@@ -0,0 +1,89 @@
|
||||
using Titano.Core.Project;
|
||||
|
||||
namespace Titano.App.Editor;
|
||||
|
||||
/// <summary>
|
||||
/// Writes the editor's project shortly after the last change: one write at a time, always the newest project, never
|
||||
/// cancelled half way (a write is a file plus an index row). <see cref="FlushAsync"/> skips the wait and returns once
|
||||
/// everything scheduled is stored. A failed write is reported and the next change tries again.
|
||||
/// </summary>
|
||||
public sealed class ProjectSaver(Func<ProjectSettings, Task> save, TimeSpan delay)
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private ProjectSettings? _pending;
|
||||
private bool _running;
|
||||
private Task _loop = Task.CompletedTask;
|
||||
private CancellationTokenSource _wait = new();
|
||||
|
||||
/// <summary>A write failed (raised on the context that scheduled the write).</summary>
|
||||
public event Action<Exception>? Failed;
|
||||
|
||||
public bool HasPendingChanges
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate) return _running;
|
||||
}
|
||||
}
|
||||
|
||||
public void Schedule(ProjectSettings project)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(project);
|
||||
TaskCompletionSource start;
|
||||
lock (_gate)
|
||||
{
|
||||
_pending = project;
|
||||
if (_running) return;
|
||||
_running = true;
|
||||
if (!_wait.TryReset()) _wait = new CancellationTokenSource();
|
||||
start = new TaskCompletionSource();
|
||||
_loop = LoopAsync(start.Task, _wait.Token);
|
||||
}
|
||||
// The loop starts outside the lock: with nothing to wait for it would otherwise save while holding it.
|
||||
start.SetResult();
|
||||
}
|
||||
|
||||
/// <summary>Stores what is pending now, without the delay.</summary>
|
||||
public Task FlushAsync()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_wait.Cancel();
|
||||
return _loop;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoopAsync(Task started, CancellationToken wait)
|
||||
{
|
||||
await started;
|
||||
try
|
||||
{
|
||||
await Task.Delay(delay, wait);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
ProjectSettings next;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_pending is null)
|
||||
{
|
||||
_running = false;
|
||||
return;
|
||||
}
|
||||
next = _pending;
|
||||
_pending = null;
|
||||
}
|
||||
try
|
||||
{
|
||||
await save(next);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Failed?.Invoke(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user