Skip to content

Commit a7684e3

Browse files
committed
fix comparing existing 'null' values. closes #35
1 parent a7e9910 commit a7684e3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

Assets/Plugins/Colyseus/StateListener/Compare.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ protected static void Generate(IndexedDictionary<string, object> mirror, Indexed
6363
var oldVal = mirror[key];
6464
var newVal = obj[key];
6565

66-
var oldValType = oldVal.GetType ();
67-
var newValType = newVal.GetType ();
68-
6966
if (
70-
!oldValType.IsPrimitive && oldValType != typeof(string) &&
71-
!newValType.IsPrimitive && oldValType != typeof(string) &&
72-
Object.ReferenceEquals(oldValType, newValType)
67+
oldVal != null && newVal != null &&
68+
!oldVal.GetType ().IsPrimitive && oldVal.GetType () != typeof(string) &&
69+
!newVal.GetType ().IsPrimitive && newVal.GetType () != typeof(string) &&
70+
Object.ReferenceEquals(oldVal.GetType (), newVal.GetType ())
7371
)
7472
{
7573
List<string> deeperPath = new List<string>(path);
@@ -93,7 +91,10 @@ protected static void Generate(IndexedDictionary<string, object> mirror, Indexed
9391
}
9492

9593
} else {
96-
if (!oldVal.Equals(newVal))
94+
if (
95+
(oldVal == null && newVal != null) ||
96+
!oldVal.Equals(newVal)
97+
)
9798
{
9899
List<string> replacePath = new List<string>(path);
99100
replacePath.Add((string) key);

Assets/Tests/StateContainerTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ public void ListenAddString() {
3636
Assert.AreEqual (1, listenCalls);
3737
}
3838

39+
[Test]
40+
public void ListenReplaceNull() {
41+
var newData = GetRawData ();
42+
43+
var listenCalls = 0;
44+
container.Listen ("null", (DataChange change) => {
45+
listenCalls++;
46+
Assert.AreEqual("replace", change.operation);
47+
Assert.AreEqual(10, change.value);
48+
});
49+
newData ["null"] = 10;
50+
51+
container.Set (newData);
52+
Assert.AreEqual (1, listenCalls);
53+
}
54+
3955
[Test]
4056
public void ListenAddNull() {
4157
var newData = GetRawData ();

0 commit comments

Comments
 (0)