11export class DocumentSnapshot {
2- constructor ( firestore , key , document ) {
2+ constructor ( firestore , key , document , exists ) {
33 this . _firestore = firestore
44 this . _key = key
55 this . _document = document
6+ this . exists = exists
67 }
78
89 data ( ) {
@@ -16,15 +17,15 @@ export class DocumentSnapshot {
1617
1718const noop = _ => null
1819
19- export let id = 0
20+ export let _id = 0
2021export class Key {
21- constructor ( ) {
22- this . id = id ++
22+ constructor ( v ) {
23+ this . v = '' + ( v != null ? v : _id ++ )
2324 }
2425
2526 get path ( ) {
2627 return {
27- lastSegment : ( ) => this . id
28+ lastSegment : ( ) => this . v
2829 }
2930 }
3031}
@@ -35,24 +36,31 @@ class DocumentReference {
3536 this . id = id
3637 this . data = data
3738 this . index = index
39+ this . exists = false
3840 this . cb = this . onError = noop
3941 }
4042
4143 onSnapshot ( cb , onError ) {
4244 this . cb = cb
4345 this . onError = onError
46+ // TODO timeout a cb
47+ setTimeout ( ( ) => {
48+ this . cb ( new DocumentSnapshot ( null , this . id , this . data , this . exists ) )
49+ } , 0 )
4450 return ( ) => {
4551 this . cb = this . onError = noop
4652 }
4753 }
4854
4955 async delete ( ) {
56+ this . exists = false
5057 return this . collection . _remove ( this . id )
5158 }
5259
5360 async update ( data ) {
5461 Object . assign ( this . data , data )
55- this . cb ( new DocumentSnapshot ( null , this . id , this . data ) )
62+ this . exists = true
63+ this . cb ( new DocumentSnapshot ( null , this . id , this . data , true ) )
5664 return this . collection . _modify ( this . id , this . data )
5765 }
5866}
@@ -66,14 +74,25 @@ class CollectionReference {
6674 onSnapshot ( cb , onError ) {
6775 this . cb = cb
6876 this . onError = onError
77+ setTimeout ( ( ) => {
78+ // Object.keys(this.data).map((k, i) => console.log(k, 'at', i, this.data[k].data))
79+ this . cb ( {
80+ docChanges : Object . keys ( this . data ) . map ( ( id , newIndex ) => ( {
81+ type : 'added' ,
82+ doc : new DocumentSnapshot ( null , new Key ( id ) , this . data [ id ] . data ) ,
83+ newIndex,
84+ oldIndex : - 1
85+ } ) )
86+ } )
87+ } , 0 )
6988 return ( ) => {
7089 this . cb = this . onError = noop
7190 }
7291 }
7392
7493 async add ( data ) {
7594 const id = new Key ( )
76- this . data [ id ] = new DocumentReference ( {
95+ this . data [ id . v ] = new DocumentReference ( {
7796 collection : this ,
7897 id,
7998 data,
@@ -87,15 +106,15 @@ class CollectionReference {
87106 oldIndex : - 1
88107 } ]
89108 } )
90- return this . data [ id ]
109+ return this . data [ id . v ]
91110 }
92111
93112 // used to check if it's a collection or document ref
94113 where ( ) { }
95114
96115 doc ( id ) {
97116 id = id || new Key ( )
98- return ( this . data [ id ] = this . data [ id ] || new DocumentReference ( {
117+ return ( this . data [ id . v ] = this . data [ id . v ] || new DocumentReference ( {
99118 collection : this ,
100119 id,
101120 data : { } ,
@@ -104,8 +123,8 @@ class CollectionReference {
104123 }
105124
106125 async _remove ( id ) {
107- const ref = this . data [ id ]
108- delete this . data [ id ]
126+ const ref = this . data [ id . v ]
127+ delete this . data [ id . v ]
109128 this . cb ( {
110129 docChanges : [ {
111130 doc : new DocumentSnapshot ( null , id , ref . data ) ,
@@ -121,8 +140,8 @@ class CollectionReference {
121140 docChanges : [ {
122141 type : 'modified' ,
123142 doc : new DocumentSnapshot ( null , id , data ) ,
124- oldIndex : this . data [ id ] . index ,
125- newIndex : this . data [ id ] . index
143+ oldIndex : this . data [ id . v ] . index ,
144+ newIndex : this . data [ id . v ] . index
126145 } ]
127146 } )
128147 }
0 commit comments