@@ -30,26 +30,45 @@ export class Key {
3030 }
3131}
3232
33- export class DocumentReference {
33+ class callbacksAndErrors {
34+ constructor ( ) {
35+ this . _cbId = 0
36+ this . cbs = { }
37+ this . onErrors = { }
38+ }
39+
40+ _addCallbacks ( cb , onError ) {
41+ const id = this . _cbId ++
42+ this . cbs [ id ] = cb
43+ this . onErrors [ id ] = onError
44+ return ( ) => {
45+ delete this . cbs [ id ]
46+ delete this . onErrors [ id ]
47+ }
48+ }
49+
50+ _callCallbacks ( data ) {
51+ Object . values ( this . cbs ) . forEach (
52+ cb => cb ( data )
53+ )
54+ }
55+ }
56+
57+ export class DocumentReference extends callbacksAndErrors {
3458 constructor ( { collection, id, data, index } ) {
59+ super ( )
3560 this . collection = collection
3661 this . id = id
3762 this . data = data
3863 this . index = index
3964 this . exists = false
40- this . cb = this . onError = noop
4165 }
4266
4367 onSnapshot ( cb , onError ) {
44- this . cb = cb
45- this . onError = onError
46- // TODO timeout a cb
4768 setTimeout ( ( ) => {
48- this . cb ( new DocumentSnapshot ( null , this . id , this . data , this . exists ) )
69+ cb ( new DocumentSnapshot ( null , this . id , this . data , this . exists ) )
4970 } , 0 )
50- return ( ) => {
51- this . cb = this . onError = noop
52- }
71+ return this . _addCallbacks ( cb , onError )
5372 }
5473
5574 get path ( ) {
@@ -68,31 +87,29 @@ export class DocumentReference {
6887 async update ( data ) {
6988 Object . assign ( this . data , data )
7089 this . exists = true
71- this . cb ( new DocumentSnapshot ( null , this . id , this . data , true ) )
90+ this . _callCallbacks ( new DocumentSnapshot ( null , this . id , this . data , true ) )
7291 return this . collection . _modify ( this . id , this . data , this )
7392 }
7493
7594 async set ( data ) {
7695 this . data = { ...data }
7796 this . exists = true
78- this . cb ( new DocumentSnapshot ( null , this . id , this . data , true ) )
97+ this . _callCallbacks ( new DocumentSnapshot ( null , this . id , this . data , true ) )
7998 return this . collection . _modify ( this . id , this . data , this )
8099 }
81100}
82101
83- class CollectionReference {
102+ class CollectionReference extends callbacksAndErrors {
84103 constructor ( name ) {
104+ super ( )
85105 this . data = { }
86106 this . name = name
87- this . cb = this . onError = noop
88107 }
89108
90109 onSnapshot ( cb , onError ) {
91- this . cb = cb
92- this . onError = onError
93110 setTimeout ( ( ) => {
94111 // Object.keys(this.data).map((k, i) => console.log(k, 'at', i, this.data[k].data))
95- this . cb ( {
112+ cb ( {
96113 docChanges : Object . keys ( this . data ) . map ( ( id , newIndex ) => ( {
97114 type : 'added' ,
98115 doc : new DocumentSnapshot ( null , new Key ( id ) , this . data [ id ] . data ) ,
@@ -101,9 +118,7 @@ class CollectionReference {
101118 } ) )
102119 } )
103120 } , 0 )
104- return ( ) => {
105- this . cb = this . onError = noop
106- }
121+ return this . _addCallbacks ( cb , onError )
107122 }
108123
109124 async add ( data ) {
@@ -114,7 +129,7 @@ class CollectionReference {
114129 data,
115130 index : Object . keys ( this . data ) . length
116131 } )
117- this . cb ( {
132+ this . _callCallbacks ( {
118133 docChanges : [ {
119134 type : 'added' ,
120135 doc : new DocumentSnapshot ( null , id , data ) ,
@@ -141,7 +156,7 @@ class CollectionReference {
141156 async _remove ( id ) {
142157 const ref = this . data [ id . v ]
143158 delete this . data [ id . v ]
144- this . cb ( {
159+ this . _callCallbacks ( {
145160 docChanges : [ {
146161 doc : new DocumentSnapshot ( null , id , ref . data ) ,
147162 type : 'removed'
@@ -158,7 +173,7 @@ class CollectionReference {
158173 this . data [ id . v ] = ref
159174 type = 'added'
160175 }
161- this . cb ( {
176+ this . _callCallbacks ( {
162177 docChanges : [ {
163178 type,
164179 doc : new DocumentSnapshot ( null , id , data ) ,
0 commit comments