132 行
3.8 KiB
C#
132 行
3.8 KiB
C#
using System.IO;
|
|
using System.Windows;
|
|
|
|
namespace UndoTree;
|
|
|
|
internal sealed class UndoTreeRecorder : IDisposable
|
|
{
|
|
const int DebounceMilliseconds = 2000;
|
|
|
|
readonly UndoTreeHistory history;
|
|
readonly UndoTreeRecordStore store;
|
|
readonly Func<string?> projectPath;
|
|
readonly Timer timer;
|
|
string? sessionPath;
|
|
UndoTreeRecord? previous;
|
|
(string Path, DateTime Stamp, long Length, UndoTreeRecord? Record)? linked;
|
|
bool dirty;
|
|
bool pruned;
|
|
|
|
public UndoTreeRecorder(UndoTreeHistory history, UndoTreeRecordStore store, Func<string?> projectPath)
|
|
{
|
|
this.history = history;
|
|
this.store = store;
|
|
this.projectPath = projectPath;
|
|
timer = new Timer(_ => UndoTreeDispatch.Post(Tick));
|
|
history.Changed += History_Changed;
|
|
if (Application.Current is { } application)
|
|
application.Exit += Application_Exit;
|
|
|
|
History_Changed(this, EventArgs.Empty);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
history.Changed -= History_Changed;
|
|
if (Application.Current is { } application)
|
|
application.Exit -= Application_Exit;
|
|
|
|
timer.Dispose();
|
|
Flush();
|
|
}
|
|
|
|
public void Flush() => Prepare(projectPath())?.Invoke();
|
|
|
|
public string? CurrentPath() => projectPath();
|
|
|
|
public bool HasPrevious()
|
|
{
|
|
var path = projectPath();
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
return false;
|
|
|
|
return IsSession(path) ? previous is not null : store.Exists(path) || LinkedRecord(path) is not null;
|
|
}
|
|
|
|
public UndoTreeRecord? Previous()
|
|
{
|
|
var path = projectPath();
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
return null;
|
|
|
|
return IsSession(path) ? previous : store.Load(path)?.Latest ?? LinkedRecord(path);
|
|
}
|
|
|
|
internal Action? Prepare(string? path)
|
|
{
|
|
if (!dirty || string.IsNullOrWhiteSpace(path))
|
|
return null;
|
|
|
|
dirty = false;
|
|
if (!IsSession(path))
|
|
{
|
|
previous = store.Load(path)?.Latest ?? LinkedRecord(path);
|
|
sessionPath = path;
|
|
}
|
|
|
|
var file = new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, path, previous, history.ToRecord());
|
|
var settings = UndoTreeSettings.Default;
|
|
var retentionDays = settings.RetentionDays;
|
|
var maxBytes = settings.MaxTotalSizeMegabytes * UndoTreeRecordStore.BytesPerMegabyte;
|
|
var prune = !pruned;
|
|
pruned = true;
|
|
return () =>
|
|
{
|
|
store.Save(path, file);
|
|
if (prune)
|
|
store.Prune(retentionDays, maxBytes, DateTime.Now);
|
|
};
|
|
}
|
|
|
|
UndoTreeRecord? LinkedRecord(string path)
|
|
{
|
|
if (store.Exists(path) || new FileInfo(path) is not { Exists: true } file)
|
|
return null;
|
|
|
|
if (linked is { } known && known.Path == path && known.Stamp == file.LastWriteTimeUtc && known.Length == file.Length)
|
|
return known.Record;
|
|
|
|
var record = UndoTreeProject.HashOf(path) is { } hash ? store.FindByHash(hash, path)?.Latest : null;
|
|
linked = (path, file.LastWriteTimeUtc, file.Length, record);
|
|
return record;
|
|
}
|
|
|
|
bool IsSession(string path) => string.Equals(path, sessionPath, StringComparison.OrdinalIgnoreCase);
|
|
|
|
void Tick()
|
|
{
|
|
if (Prepare(projectPath()) is { } write)
|
|
Task.Run(write);
|
|
}
|
|
|
|
void History_Changed(object? sender, EventArgs e)
|
|
{
|
|
if (history.Count <= 1)
|
|
{
|
|
timer.Change(Timeout.Infinite, Timeout.Infinite);
|
|
dirty = false;
|
|
sessionPath = null;
|
|
previous = null;
|
|
return;
|
|
}
|
|
|
|
if (!UndoTreeSettings.Default.SaveHistory)
|
|
return;
|
|
|
|
dirty = true;
|
|
timer.Change(DebounceMilliseconds, Timeout.Infinite);
|
|
}
|
|
|
|
void Application_Exit(object sender, ExitEventArgs e) => Flush();
|
|
}
|