using System.Diagnostics.CodeAnalysis;
namespace MeterVault.App;
///
/// Unsaved dialog input that has to survive a detour to another page of the same circuit.
///
///
/// A meter's source dialog sends the user off to create the connector the source needs, and the meter
/// page is disposed on the way. Keeping the draft here, keyed by what it belongs to, lets the dialog come
/// back with everything that was typed instead of empty. Scoped: it lives as long as the circuit and is
/// never shared between users.
///
public sealed class DraftStore
{
private readonly Dictionary _drafts = new(StringComparer.Ordinal);
public void Save(string key, object draft) => _drafts[key] = draft;
/// Hands back a draft once; it is gone afterwards.
public bool TryTake(string key, [NotNullWhen(true)] out T? draft)
where T : class
{
if (_drafts.Remove(key, out var stored) && stored is T typed)
{
draft = typed;
return true;
}
draft = null;
return false;
}
public void Discard(string key) => _drafts.Remove(key);
}