Skip to content

Commit

Permalink
feat: SyncDictionary 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 03f2075 commit 23349af
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Assets/Mirror/Runtime/SyncDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,21 @@ public void OnDeserializeAll(NetworkReader reader)

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

for (int i = 0; i < count; i++)
{
TKey key = reader.Read<TKey>();
TValue obj = reader.Read<TValue>();
objects.Add(key, obj);
OnInsert?.Invoke(key, 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
30 changes: 30 additions & 0 deletions Assets/Tests/Editor/SyncDictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ public void TestInit()
Assert.That(clientSyncDictionary, Is.EquivalentTo(comparer));
}

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

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

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

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

[Test]
public void TestAdd()
{
Expand Down

0 comments on commit 23349af

Please sign in to comment.