// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; namespace osu.Game.Screens.Edit { /// /// A component that tracks a batch change, only applying after all active changes are completed. /// public abstract class TransactionalCommitComponent { public bool TransactionActive => bulkChangesStarted > 0; private int bulkChangesStarted; /// /// Signal the beginning of a change. /// public void BeginChange() => bulkChangesStarted++; /// /// Signal the end of a change. /// /// Throws if was not first called. public void EndChange() { if (bulkChangesStarted == 0) throw new InvalidOperationException($"Cannot call {nameof(EndChange)} without a previous call to {nameof(BeginChange)}."); if (--bulkChangesStarted == 0) UpdateState(); } public void SaveState() { if (bulkChangesStarted > 0) return; UpdateState(); } protected abstract void UpdateState(); } }