@@ -69,14 +69,14 @@ export class DocumentReference {
6969 Object . assign ( this . data , data )
7070 this . exists = true
7171 this . cb ( new DocumentSnapshot ( null , this . id , this . data , true ) )
72- return this . collection . _modify ( this . id , this . data )
72+ return this . collection . _modify ( this . id , this . data , this )
7373 }
7474
7575 async set ( data ) {
7676 this . data = { ...data }
7777 this . exists = true
7878 this . cb ( new DocumentSnapshot ( null , this . id , this . data , true ) )
79- return this . collection . _modify ( this . id , this . data )
79+ return this . collection . _modify ( this . id , this . data , this )
8080 }
8181}
8282
@@ -130,12 +130,12 @@ class CollectionReference {
130130
131131 doc ( id ) {
132132 id = id || new Key ( )
133- return ( this . data [ id . v ] = this . data [ id . v ] || new DocumentReference ( {
133+ return this . data [ id . v ] || new DocumentReference ( {
134134 collection : this ,
135135 id,
136136 data : { } ,
137137 index : Object . keys ( this . data ) . length
138- } ) )
138+ } )
139139 }
140140
141141 async _remove ( id ) {
@@ -151,10 +151,16 @@ class CollectionReference {
151151 ref . data = null
152152 }
153153
154- async _modify ( id , data ) {
154+ async _modify ( id , data , ref ) {
155+ let type = 'modified'
156+ if ( ! this . data [ id . v ] ) {
157+ ref . index = Object . keys ( this . data ) . length
158+ this . data [ id . v ] = ref
159+ type = 'added'
160+ }
155161 this . cb ( {
156162 docChanges : [ {
157- type : 'modified' ,
163+ type,
158164 doc : new DocumentSnapshot ( null , id , data ) ,
159165 oldIndex : this . data [ id . v ] . index ,
160166 newIndex : this . data [ id . v ] . index
0 commit comments