Skip to content
This repository was archived by the owner on Feb 20, 2022. It is now read-only.

Commit 3281924

Browse files
author
Vincent Cantin
committed
Added a test and fixed a bug on the localDB's changed message handling.
1 parent c2cae80 commit 3281924

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

example-server/imports/api/friends.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ Meteor.methods({
1616
'friends.removeAll'() {
1717
Friends.remove({});
1818
},
19+
'friends.addAttributes'(name, fields) {
20+
Friends.update(
21+
{
22+
name: name
23+
},
24+
{
25+
$set: fields
26+
});
27+
},
28+
'friends.removeAttributes'(name, fields) {
29+
Friends.update(
30+
{
31+
name: name
32+
},
33+
{
34+
$unset: fields
35+
});
36+
},
1937
'friends.add'(a, b) {
2038
return a + b;
2139
}

unity-project/Assets/example/TestLocalDB.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,7 @@ public void Update() {
7979
}
8080

8181
if (Input.GetKeyDown(KeyCode.F)) {
82-
MethodCall methodCall = ddpConnection.Call("friends.create", JSONObject.CreateStringObject("Coco"));
83-
methodCall.OnUpdated = (MethodCall obj) => {
84-
Debug.Log("Updated, methodId=" + obj.id);
85-
};
86-
methodCall.OnResult = (MethodCall obj) => {
87-
Debug.Log("Result = " + obj.result);
88-
};
82+
ddpConnection.Call("friends.create", JSONObject.CreateStringObject("Coco"));
8983
}
9084

9185
if (Input.GetKeyDown(KeyCode.D)) {
@@ -94,6 +88,25 @@ public void Update() {
9488
}
9589
}
9690

91+
if (Input.GetKeyDown(KeyCode.O)) {
92+
JSONObject parents = new JSONObject();
93+
parents.AddField("mother", "wonder woman");
94+
parents.AddField("father", "batman");
95+
JSONObject attr = new JSONObject();
96+
attr.AddField("age", 24);
97+
attr.AddField("height", 180);
98+
attr.AddField("parents", parents);
99+
ddpConnection.Call("friends.addAttributes", JSONObject.StringObject("Coco"), attr);
100+
}
101+
102+
if (Input.GetKeyDown(KeyCode.P)) {
103+
JSONObject attr = new JSONObject();
104+
attr.AddField("age", 1);
105+
attr.AddField("height", 1);
106+
attr.AddField("parents.mother", 1);
107+
ddpConnection.Call("friends.removeAttributes", JSONObject.StringObject("Coco"), attr);
108+
}
109+
97110
}
98111

99112
}

unity-project/Assets/unity3d-ddp-client/local-db/JsonObjectCollection.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@ public void Add(string docId, JSONObject fields) {
2929
documents.Add(docId, fields);
3030
}
3131

32-
// TODO: This implementation is not tested, and is probably not complete.
3332
public void Change(string docId, JSONObject fields, JSONObject cleared) {
3433
if (OnChanged != null) {
3534
OnChanged(docId, fields, cleared);
3635
}
3736

3837
JSONObject document = documents[docId];
39-
document.Merge(fields);
40-
foreach (JSONObject field in cleared.list) {
41-
document.RemoveField(field.str);
38+
39+
if (fields != null) {
40+
foreach (string field in fields.keys) {
41+
document.SetField(field, fields[field]);
42+
}
43+
}
44+
45+
if (cleared != null) {
46+
foreach (JSONObject field in cleared.list) {
47+
document.RemoveField(field.str);
48+
}
4249
}
4350
}
4451

unity-project/Assets/unity3d-ddp-client/local-db/TypedCollection.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public void Add(string docId, JSONObject fields) {
3636
documents.Add(docId, document);
3737
}
3838

39-
// TODO: This implementation is not tested, and is probably not complete.
4039
public void Change(string docId, JSONObject fields, JSONObject cleared) {
4140
JSONObject jsonDocument = DocumentToJSONObject(documents[docId]);
4241

@@ -45,9 +44,16 @@ public void Change(string docId, JSONObject fields, JSONObject cleared) {
4544
oldDocument = JSONObjectToDocument(jsonDocument.Copy());
4645
}
4746

48-
jsonDocument.Merge(fields);
49-
foreach (JSONObject field in cleared.list) {
50-
jsonDocument.RemoveField(field.str);
47+
if (fields != null) {
48+
foreach (string field in fields.keys) {
49+
jsonDocument.SetField(field, fields[field]);
50+
}
51+
}
52+
53+
if (cleared != null) {
54+
foreach (JSONObject field in cleared.list) {
55+
jsonDocument.RemoveField(field.str);
56+
}
5157
}
5258

5359
DocType newDocument = JSONObjectToDocument(jsonDocument);

0 commit comments

Comments
 (0)