Skip to content

Commit

Permalink
feat(SyncSet): Added OnChange Action
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGadget1024 committed Apr 14, 2024
1 parent 6e6227a commit cf20ba2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Assets/Mirror/Core/SyncSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public class SyncSet<T> : SyncObject, ISet<T>
/// <summary>This is called after the item is removed. T is the OLD item</summary>
public Action<T> OnRemove;

/// <summary>
/// This is called for all changes to the Set.
/// <para>For OP_ADD, T is the NEW value of the entry.</para>
/// <para>For OP_REMOVE, T is the OLD value of the entry.</para>
/// <para>For OP_CLEAR, T is default.</para>
/// </summary>
public Action<Operation, T> OnChange;

/// <summary>This is called BEFORE the data is cleared</summary>
public Action OnClear;

Expand Down Expand Up @@ -107,18 +115,21 @@ void AddOperation(Operation op, T oldItem, T newItem, bool checkAccess)
{
case Operation.OP_ADD:
OnAdd?.Invoke(newItem);
OnChange?.Invoke(op, newItem);
#pragma warning disable CS0618 // Type or member is obsolete
Callback?.Invoke(op, newItem);
#pragma warning restore CS0618 // Type or member is obsolete
break;
case Operation.OP_REMOVE:
OnRemove?.Invoke(oldItem);
OnChange?.Invoke(op, oldItem);
#pragma warning disable CS0618 // Type or member is obsolete
Callback?.Invoke(op, oldItem);
#pragma warning restore CS0618 // Type or member is obsolete
break;
case Operation.OP_CLEAR:
OnClear?.Invoke();
OnChange?.Invoke(op, default);
#pragma warning disable CS0618 // Type or member is obsolete
Callback?.Invoke(op, default);
#pragma warning restore CS0618 // Type or member is obsolete
Expand Down
27 changes: 27 additions & 0 deletions Assets/Mirror/Tests/Editor/SyncCollections/SyncSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,20 @@ public void TestClear()
Assert.That(clientSyncSet.Count, Is.EqualTo(3));
};

bool changeActionCalled = false;
clientSyncSet.OnChange = (op, item) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncHashSet<string>.Operation.OP_CLEAR));
Assert.That(clientSyncSet.Count, Is.EqualTo(3));
};

serverSyncSet.Clear();
SerializeDeltaTo(serverSyncSet, clientSyncSet);
Assert.That(clientSyncSet, Is.EquivalentTo(new string[] { }));
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}

[Test]
Expand Down Expand Up @@ -148,10 +157,19 @@ public void CallbackTest()
Assert.That(item, Is.EqualTo("yay"));
};

bool changeActionCalled = false;
clientSyncSet.OnChange = (op, item) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncHashSet<string>.Operation.OP_ADD));
Assert.That(item, Is.EqualTo("yay"));
};

serverSyncSet.Add("yay");
SerializeDeltaTo(serverSyncSet, clientSyncSet);
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}

[Test]
Expand All @@ -175,10 +193,19 @@ public void CallbackRemoveTest()
Assert.That(oldItem, Is.EqualTo("World"));
};

bool changeActionCalled = false;
clientSyncSet.OnChange = (op, item) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncHashSet<string>.Operation.OP_REMOVE));
Assert.That(item, Is.EqualTo("World"));
};

serverSyncSet.Remove("World");
SerializeDeltaTo(serverSyncSet, clientSyncSet);
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}

[Test]
Expand Down

0 comments on commit cf20ba2

Please sign in to comment.