1
- ## @meta-authors TODO
1
+ ## @meta-authors Kyle Szklenski
2
2
## @meta-version 2.2
3
3
## A reference to a Firestore Document.
4
4
## Documentation TODO.
@@ -25,8 +25,8 @@ func _init(doc : Dictionary = {}):
25
25
document = doc .fields
26
26
if doc .has ("name" ):
27
27
doc_name = doc .name
28
- if doc_name .count ("/" ) > 2 :
29
- doc_name = (doc_name .split ("/" ) as Array ).back ()
28
+ if doc_name .count ("/" ) > 2 :
29
+ doc_name = (doc_name .split ("/" ) as Array ).back ()
30
30
31
31
self .create_time = doc .createTime
32
32
@@ -44,7 +44,36 @@ func replace(with : FirestoreDocument, is_listener := false) -> void:
44
44
else :
45
45
var new_value = Utilities .from_firebase_type (document [key ])
46
46
var old_value = Utilities .from_firebase_type (current [key ])
47
- if new_value != old_value :
47
+ if typeof (new_value ) != typeof (old_value ) or new_value != old_value :
48
+ if old_value == null :
49
+ changes .removed .push_back ({ "key" : key }) # ??
50
+ else :
51
+ changes .updated .push_back ({ "key" : key , "old" : old_value , "new" : new_value })
52
+
53
+ for key in document .keys ():
54
+ if not current .has (key ):
55
+ changes .added .push_back ({ "key" : key , "new" : Utilities .from_firebase_type (document [key ]) })
56
+
57
+ if not (changes .added .is_empty () and changes .removed .is_empty () and changes .updated .is_empty ()):
58
+ changed .emit (changes )
59
+
60
+ func new_document (base_document : Dictionary ) -> void :
61
+ var current = document .duplicate ()
62
+ document = {}
63
+ for key in base_document .keys ():
64
+ document [key ] = Utilities .to_firebase_type (key )
65
+
66
+ var changes = {
67
+ "added" : [], "removed" : [], "updated" : [], "is_listener" : false
68
+ }
69
+
70
+ for key in current .keys ():
71
+ if not document .has (key ):
72
+ changes .removed .push_back ({ "key" : key })
73
+ else :
74
+ var new_value = Utilities .from_firebase_type (document [key ])
75
+ var old_value = Utilities .from_firebase_type (current [key ])
76
+ if typeof (new_value ) != typeof (old_value ) or new_value != old_value :
48
77
if old_value == null :
49
78
changes .removed .push_back ({ "key" : key }) # ??
50
79
else :
@@ -126,15 +155,25 @@ func get_value(property : StringName) -> Variant:
126
155
127
156
if document .has (property ):
128
157
var result = Utilities .from_firebase_type (document [property ])
129
-
130
158
return result
131
159
132
160
return null
133
161
162
+ func _get (property : StringName ) -> Variant :
163
+ return get_value (property )
164
+
134
165
func _set (property : StringName , value : Variant ) -> bool :
166
+ assert (value != null , "When using the dictionary setter, the value cannot be null; use erase_field instead." )
135
167
document [property ] = Utilities .to_firebase_type (value )
136
168
return true
137
169
170
+ func get_unsafe_document () -> Dictionary :
171
+ var result = {}
172
+ for key in keys ():
173
+ result [key ] = Utilities .from_firebase_type (document [key ])
174
+
175
+ return result
176
+
138
177
func keys ():
139
178
return document .keys ()
140
179
0 commit comments