Skip to content

Commit

Permalink
feat(SyncList): Added OnChange Action
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGadget1024 committed Apr 14, 2024
1 parent 52e6ca3 commit d5bb965
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Assets/Mirror/Core/SyncList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
30 changes: 30 additions & 0 deletions Assets/Mirror/Tests/Editor/SyncCollections/SyncListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit d5bb965

Please sign in to comment.