1- ## @meta-authors TODO
1+ ## @meta-authors Kyle Szklenski
22## @meta-version 2.2
33## A reference to a Firestore Document.
44## Documentation TODO.
@@ -25,8 +25,8 @@ func _init(doc : Dictionary = {}):
2525 document = doc .fields
2626 if doc .has ("name" ):
2727 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 ()
3030
3131 self .create_time = doc .createTime
3232
@@ -44,7 +44,36 @@ func replace(with : FirestoreDocument, is_listener := false) -> void:
4444 else :
4545 var new_value = Utilities .from_firebase_type (document [key ])
4646 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 :
4877 if old_value == null :
4978 changes .removed .push_back ({ "key" : key }) # ??
5079 else :
@@ -126,15 +155,25 @@ func get_value(property : StringName) -> Variant:
126155
127156 if document .has (property ):
128157 var result = Utilities .from_firebase_type (document [property ])
129-
130158 return result
131159
132160 return null
133161
162+ func _get (property : StringName ) -> Variant :
163+ return get_value (property )
164+
134165func _set (property : StringName , value : Variant ) -> bool :
166+ assert (value != null , "When using the dictionary setter, the value cannot be null; use erase_field instead." )
135167 document [property ] = Utilities .to_firebase_type (value )
136168 return true
137169
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+
138177func keys ():
139178 return document .keys ()
140179
0 commit comments