fix(SyncList): Clear after Callback

Allows users to iterate the list before it's wiped
This commit is contained in:
MrGadget 2024-03-23 12:09:00 -04:00
parent c7305db462
commit aecc8f2587
2 changed files with 16 additions and 2 deletions

View File

@ -192,12 +192,14 @@ public override void OnDeserializeDelta(NetworkReader reader)
case Operation.OP_CLEAR:
if (apply)
{
objects.Clear();
// add dirty + changes.
// ClientToServer needs to set dirty in server OnDeserialize.
// no access check: server OnDeserialize can always
// write, even for ClientToServer (for broadcasting).
AddOperation(Operation.OP_CLEAR, 0, default, default, false);
// clear after invoking the callback so users can iterate the list
// and take appropriate action on the items before they are wiped.
objects.Clear();
}
break;
@ -267,8 +269,10 @@ public void AddRange(IEnumerable<T> range)
public void Clear()
{
objects.Clear();
AddOperation(Operation.OP_CLEAR, 0, default, default, true);
// clear after invoking the callback so users can iterate the list
// and take appropriate action on the items before they are wiped.
objects.Clear();
}
public bool Contains(T item) => IndexOf(item) >= 0;

View File

@ -97,9 +97,19 @@ public void TestAddRange()
[Test]
public void TestClear()
{
bool called = false;
clientSyncList.Callback = (op, index, oldItem, newItem) =>
{
called = true;
Assert.That(op, Is.EqualTo(SyncList<string>.Operation.OP_CLEAR));
Assert.That(clientSyncList.Count, Is.EqualTo(3));
};
serverSyncList.Clear();
SerializeDeltaTo(serverSyncList, clientSyncList);
Assert.That(clientSyncList, Is.EquivalentTo(new string[] {}));
Assert.That(called, Is.True);
}
[Test]