Skip to content

Commit

Permalink
feat: SyncList raise event when initially synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Oct 23, 2020
1 parent b6305e5 commit 9f679c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Assets/Mirror/Runtime/SyncList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,22 @@ public void OnDeserializeAll(NetworkReader reader)
int count = (int)reader.ReadPackedUInt32();

objects.Clear();
OnClear?.Invoke();
changes.Clear();

for (int i = 0; i < count; i++)
{
T obj = reader.Read<T>();
objects.Add(obj);
OnInsert?.Invoke(i, obj);
}

// We will need to skip all these changes
// the next time the list is synchronized
// because they have already been applied
changesAhead = (int)reader.ReadPackedUInt32();

OnChange?.Invoke();
}

public void OnDeserializeDelta(NetworkReader reader)
Expand Down
33 changes: 33 additions & 0 deletions Assets/Tests/Editor/SyncListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ public void TestInit()
Assert.That(clientSyncList, Is.EquivalentTo(new[] { "Hello", "World", "!" }));
}

[Test]
public void ClearEventOnSyncAll()
{
Action callback = Substitute.For<Action>();
clientSyncList.OnClear += callback;
SerializeAllTo(serverSyncList, clientSyncList);
callback.Received().Invoke();
}

[Test]
public void InsertEventOnSyncAll()
{
Action<int, string> callback = Substitute.For<Action<int, string>>();
clientSyncList.OnInsert += callback;
SerializeAllTo(serverSyncList, clientSyncList);

Received.InOrder(() =>
{
callback.Invoke(0, "Hello");
callback.Invoke(1, "World");
callback.Invoke(2, "!");
});
}

[Test]
public void ChangeEventOnSyncAll()
{
Action callback = Substitute.For<Action>();
clientSyncList.OnChange += callback;
SerializeAllTo(serverSyncList, clientSyncList);
callback.Received().Invoke();
}

[Test]
public void TestAdd()
{
Expand Down

0 comments on commit 9f679c5

Please sign in to comment.