@@ -30,26 +30,45 @@ export class Key {
30
30
}
31
31
}
32
32
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 {
34
58
constructor ( { collection, id, data, index } ) {
59
+ super ( )
35
60
this . collection = collection
36
61
this . id = id
37
62
this . data = data
38
63
this . index = index
39
64
this . exists = false
40
- this . cb = this . onError = noop
41
65
}
42
66
43
67
onSnapshot ( cb , onError ) {
44
- this . cb = cb
45
- this . onError = onError
46
- // TODO timeout a cb
47
68
setTimeout ( ( ) => {
48
- this . cb ( new DocumentSnapshot ( null , this . id , this . data , this . exists ) )
69
+ cb ( new DocumentSnapshot ( null , this . id , this . data , this . exists ) )
49
70
} , 0 )
50
- return ( ) => {
51
- this . cb = this . onError = noop
52
- }
71
+ return this . _addCallbacks ( cb , onError )
53
72
}
54
73
55
74
get path ( ) {
@@ -68,31 +87,29 @@ export class DocumentReference {
68
87
async update ( data ) {
69
88
Object . assign ( this . data , data )
70
89
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 ) )
72
91
return this . collection . _modify ( this . id , this . data , this )
73
92
}
74
93
75
94
async set ( data ) {
76
95
this . data = { ...data }
77
96
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 ) )
79
98
return this . collection . _modify ( this . id , this . data , this )
80
99
}
81
100
}
82
101
83
- class CollectionReference {
102
+ class CollectionReference extends callbacksAndErrors {
84
103
constructor ( name ) {
104
+ super ( )
85
105
this . data = { }
86
106
this . name = name
87
- this . cb = this . onError = noop
88
107
}
89
108
90
109
onSnapshot ( cb , onError ) {
91
- this . cb = cb
92
- this . onError = onError
93
110
setTimeout ( ( ) => {
94
111
// Object.keys(this.data).map((k, i) => console.log(k, 'at', i, this.data[k].data))
95
- this . cb ( {
112
+ cb ( {
96
113
docChanges : Object . keys ( this . data ) . map ( ( id , newIndex ) => ( {
97
114
type : 'added' ,
98
115
doc : new DocumentSnapshot ( null , new Key ( id ) , this . data [ id ] . data ) ,
@@ -101,9 +118,7 @@ class CollectionReference {
101
118
} ) )
102
119
} )
103
120
} , 0 )
104
- return ( ) => {
105
- this . cb = this . onError = noop
106
- }
121
+ return this . _addCallbacks ( cb , onError )
107
122
}
108
123
109
124
async add ( data ) {
@@ -114,7 +129,7 @@ class CollectionReference {
114
129
data,
115
130
index : Object . keys ( this . data ) . length
116
131
} )
117
- this . cb ( {
132
+ this . _callCallbacks ( {
118
133
docChanges : [ {
119
134
type : 'added' ,
120
135
doc : new DocumentSnapshot ( null , id , data ) ,
@@ -141,7 +156,7 @@ class CollectionReference {
141
156
async _remove ( id ) {
142
157
const ref = this . data [ id . v ]
143
158
delete this . data [ id . v ]
144
- this . cb ( {
159
+ this . _callCallbacks ( {
145
160
docChanges : [ {
146
161
doc : new DocumentSnapshot ( null , id , ref . data ) ,
147
162
type : 'removed'
@@ -158,7 +173,7 @@ class CollectionReference {
158
173
this . data [ id . v ] = ref
159
174
type = 'added'
160
175
}
161
- this . cb ( {
176
+ this . _callCallbacks ( {
162
177
docChanges : [ {
163
178
type,
164
179
doc : new DocumentSnapshot ( null , id , data ) ,
0 commit comments