Skip to content

Commit c2e7d9c

Browse files
committed
implement 'immediate' as third parameter of Listen() method.
1 parent 4ea3483 commit c2e7d9c

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

Assets/Plugins/Colyseus/StateListener/StateContainer.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public FallbackPatchListener Listen(Action<PatchObject> callback)
7272
return listener;
7373
}
7474

75-
public PatchListener Listen(string segments, Action<DataChange> callback) {
75+
public PatchListener Listen(string segments, Action<DataChange> callback, bool immediate = false) {
7676
var rawRules = segments.Split ('/');
7777
var regexpRules = this.ParseRegexRules (rawRules);
7878

@@ -84,6 +84,12 @@ public PatchListener Listen(string segments, Action<DataChange> callback) {
8484

8585
this.listeners.Add(listener);
8686

87+
if (immediate) {
88+
List<PatchListener> onlyListener = new List<PatchListener>();
89+
onlyListener.Add(listener);
90+
this.CheckPatches(Compare.GetPatchList(new IndexedDictionary<string, object>(), this.state), onlyListener);
91+
}
92+
8793
return listener;
8894
}
8995

@@ -128,23 +134,29 @@ protected Regex[] ParseRegexRules (string[] rules)
128134
return regexpRules;
129135
}
130136

131-
private void CheckPatches(PatchObject[] patches)
137+
private void CheckPatches(PatchObject[] patches, List<PatchListener> _listeners = null)
132138
{
139+
if (_listeners == null)
140+
{
141+
_listeners = this.listeners;
142+
}
133143

134144
for (var i = patches.Length - 1; i >= 0; i--)
135145
{
136146
var matched = false;
137147

138-
for (var j = 0; j < this.listeners.Count; j++)
148+
for (var j = 0; j < _listeners.Count; j++)
139149
{
140-
var listener = this.listeners[j];
150+
var listener = _listeners[j];
141151
var pathVariables = this.GetPathVariables(patches[i], listener);
142152
if (pathVariables != null)
143153
{
144-
var dataChange = new DataChange ();
145-
dataChange.path = pathVariables;
146-
dataChange.operation = patches [i].operation;
147-
dataChange.value = patches [i].value;
154+
DataChange dataChange = new DataChange
155+
{
156+
path = pathVariables,
157+
operation = patches[i].operation,
158+
value = patches[i].value
159+
};
148160

149161
listener.callback.Invoke (dataChange);
150162
matched = true;

Assets/Tests/StateContainerTest.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using UnityEngine;
2-
using UnityEngine.TestTools;
3-
using NUnit.Framework;
4-
using System.Collections;
1+
using NUnit.Framework;
52
using System.Collections.Generic;
63

74
using Colyseus;
@@ -232,6 +229,31 @@ public void ListenInitialState() {
232229
Assert.AreEqual (9, listenCalls);
233230
}
234231

232+
[Test]
233+
public void ListenWithImmediate()
234+
{
235+
var container = new StateContainer(GetRawData());
236+
var listenCalls = 0;
237+
238+
container.Listen("players/:id/position/:attribute", (DataChange change) => {
239+
listenCalls++;
240+
}, true);
241+
242+
container.Listen("turn", (DataChange change) => {
243+
listenCalls++;
244+
}, true);
245+
246+
container.Listen("game/turn", (DataChange change) => {
247+
listenCalls++;
248+
}, true);
249+
250+
container.Listen("messages/:number", (DataChange change) => {
251+
listenCalls++;
252+
}, true);
253+
254+
Assert.AreEqual(9, listenCalls);
255+
}
256+
235257
protected IndexedDictionary<string, object> GetRawData () {
236258
var data = new IndexedDictionary<string, object> ();
237259
var players = new IndexedDictionary<string, object> ();

0 commit comments

Comments
 (0)