using System.Collections.Generic; namespace ThingModel.Proto { internal class ProtoModelObserver : IWarehouseObserver { private readonly object _lockHashSet = new object(); public readonly HashSet Updates = new HashSet(); public readonly HashSet Deletions = new HashSet(); public readonly HashSet Definitions = new HashSet(); public readonly HashSet PermanentDefinitions = new HashSet(); public void Reset() { lock (_lockHashSet) { Updates.Clear(); Deletions.Clear(); Definitions.Clear(); } } public void New(ThingModel.Thing thing, string sender) { lock (_lockHashSet) { Updates.Add(thing); } } public void Deleted(ThingModel.Thing thing, string sender) { lock (_lockHashSet) { Updates.Remove(thing); Deletions.Add(thing); } } public void Updated(ThingModel.Thing thing, string sender) { lock (_lockHashSet) { Updates.Add(thing); } } public void Define(ThingModel.ThingType thing, string sender) { lock (_lockHashSet) { Definitions.Add(thing); } } public bool SomethingChanged() { lock (_lockHashSet) { return Updates.Count != 0 || Deletions.Count != 0 || Definitions.Count != 0; } } public Transaction GetTransaction(ToProtobuf toProtobuf, string senderID, bool allDefinitions = false, bool onlyDefinitions = false) { List copyUpdates; List copyDeletions; List copyDefinitions; lock (_lockHashSet) { if (onlyDefinitions) { copyUpdates = new List(); copyDeletions = new List(); } else { copyUpdates = new List(Updates); copyDeletions = new List(Deletions); } copyDefinitions = new List(allDefinitions ? PermanentDefinitions : Definitions); } return toProtobuf.Convert(copyUpdates, copyDeletions, copyDefinitions, senderID); } } }