feat(SyncList): Added OnChange Action

This commit is contained in:
MrGadget 2024-04-14 17:01:00 -04:00
parent 732141cf92
commit 0867186289
2 changed files with 43 additions and 0 deletions

View File

@ -27,6 +27,14 @@ public enum Operation : byte
/// <summary>This is called after the item is removed with index and OLD Value</summary>
public Action<int, T> OnRemove;
/// <summary>
/// This is called for all changes to the List.
/// <para>For OP_ADD and OP_INSERT, T is the NEW value of the entry.</para>
/// <para>For OP_SET and OP_REMOVE, T is the OLD value of the entry.</para>
/// <para>For OP_CLEAR, T is default.</para>
/// </summary>
public Action<Operation, int, T> OnChange;
/// <summary>This is called before the list is cleared so the list can be iterated</summary>
public Action OnClear;
@ -106,18 +114,23 @@ void AddOperation(Operation op, int itemIndex, T oldItem, T newItem, bool checkA
{
case Operation.OP_ADD:
OnAdd?.Invoke(itemIndex);
OnChange?.Invoke(op, itemIndex, newItem);
break;
case Operation.OP_INSERT:
OnInsert?.Invoke(itemIndex);
OnChange?.Invoke(op, itemIndex, newItem);
break;
case Operation.OP_SET:
OnSet?.Invoke(itemIndex, oldItem);
OnChange?.Invoke(op, itemIndex, oldItem);
break;
case Operation.OP_REMOVEAT:
OnRemove?.Invoke(itemIndex, oldItem);
OnChange?.Invoke(op, itemIndex, oldItem);
break;
case Operation.OP_CLEAR:
OnClear?.Invoke();
OnChange?.Invoke(op, itemIndex, default);
break;
}

View File

@ -463,10 +463,20 @@ public void CallbackTest()
Assert.That(clientSyncList[index], Is.EqualTo("yay"));
};
bool changeActionCalled = false;
clientSyncList.OnChange = (op, index, oldItem) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncList<string>.Operation.OP_ADD));
Assert.That(index, Is.EqualTo(3));
Assert.That(oldItem, Is.EqualTo("yay"));
};
serverSyncList.Add("yay");
SerializeDeltaTo(serverSyncList, clientSyncList);
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}
[Test]
@ -492,10 +502,20 @@ public void CallbackRemoveTest()
Assert.That(oldItem, Is.EqualTo("World"));
};
bool changeActionCalled = false;
clientSyncList.OnChange = (op, index, oldItem) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncList<string>.Operation.OP_REMOVEAT));
Assert.That(index, Is.EqualTo(1));
Assert.That(oldItem, Is.EqualTo("World"));
};
serverSyncList.Remove("World");
SerializeDeltaTo(serverSyncList, clientSyncList);
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}
[Test]
@ -522,10 +542,20 @@ public void CallbackRemoveAtTest()
Assert.That(oldItem, Is.EqualTo("World"));
};
bool changeActionCalled = false;
clientSyncList.OnChange = (op, index, oldItem) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncList<string>.Operation.OP_REMOVEAT));
Assert.That(index, Is.EqualTo(1));
Assert.That(oldItem, Is.EqualTo("World"));
};
serverSyncList.RemoveAt(1);
SerializeDeltaTo(serverSyncList, clientSyncList);
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}
[Test]