Skip to content

Commit d4ee63c

Browse files
committed
trigger Listen() callbacks on initial state. closes #31
1 parent 5223e7a commit d4ee63c

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Assets/Plugins/Colyseus/DeltaListener/Compare.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,29 @@ protected static void Generate(IndexedDictionary<string, object> mirror, Indexed
133133
List<string> addPath = new List<string>(path);
134134
addPath.Add((string) key);
135135

136+
var newVal = obj [key];
137+
if (newVal != null) {
138+
var newValType = newVal.GetType ();
139+
140+
// compare deeper additions
141+
if (
142+
!newValType.IsPrimitive &&
143+
newValType != typeof(string)
144+
) {
145+
if (newVal is IDictionary) {
146+
Generate(new IndexedDictionary<string, object>(), newVal as IndexedDictionary<string, object>, patches, addPath);
147+
148+
} else if (newVal is IList) {
149+
Generate(new List<object>(), newVal as List<object>, patches, addPath);
150+
}
151+
}
152+
}
153+
136154
patches.Add(new PatchObject
137155
{
138156
operation = "add",
139157
path = addPath.ToArray(),
140-
value = obj[key]
158+
value = newVal
141159
});
142160
}
143161
}

Assets/Tests/DeltaContainerTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ public void ListenRemoveArray() {
190190
Assert.AreEqual (3, listenCalls);
191191
}
192192

193+
[Test]
194+
public void ListenInitialState() {
195+
var container = new DeltaContainer (new IndexedDictionary<string, object>());
196+
var listenCalls = 0;
197+
198+
container.Listen ("players/:id/position/:attribute", (DataChange change) => {
199+
listenCalls++;
200+
});
201+
202+
container.Listen ("turn", (DataChange change) => {
203+
listenCalls++;
204+
});
205+
206+
container.Listen ("game/turn", (DataChange change) => {
207+
listenCalls++;
208+
});
209+
210+
container.Listen ("messages/:number", (DataChange change) => {
211+
listenCalls++;
212+
});
213+
214+
container.Set (GetRawData ());
215+
216+
Assert.AreEqual (9, listenCalls);
217+
}
218+
193219
protected IndexedDictionary<string, object> GetRawData () {
194220
var data = new IndexedDictionary<string, object> ();
195221
var players = new IndexedDictionary<string, object> ();

0 commit comments

Comments
 (0)