@@ -17,17 +17,15 @@ function subscribeToRefs ({
17
17
data,
18
18
depth,
19
19
resolve
20
- } ) {
20
+ } , options ) {
21
21
const refKeys = Object . keys ( refs )
22
22
const missingKeys = Object . keys ( subs ) . filter ( refKey => refKeys . indexOf ( refKey ) < 0 )
23
23
// unbind keys that are no longer there
24
24
missingKeys . forEach ( refKey => {
25
25
subs [ refKey ] . unsub ( )
26
26
delete subs [ refKey ]
27
27
} )
28
- if ( ! refKeys . length ) return resolve ( path )
29
- // TODO max depth param, default to 1?
30
- if ( ++ depth > 3 ) throw new Error ( 'more than 5 nested refs' )
28
+ if ( ! refKeys . length || ++ depth > options . maxRefDepth ) return resolve ( path )
31
29
32
30
let resolvedCount = 0
33
31
const totalToResolve = refKeys . length
@@ -59,7 +57,7 @@ function subscribeToRefs ({
59
57
path : docPath ,
60
58
depth,
61
59
resolve : deepResolve . bind ( null , docPath )
62
- } ) ,
60
+ } , options ) ,
63
61
path : ref . path
64
62
}
65
63
} )
@@ -71,7 +69,7 @@ function bindCollection ({
71
69
collection,
72
70
resolve,
73
71
reject
74
- } ) {
72
+ } , options ) {
75
73
// TODO support pathes? nested.obj.list (walkSet)
76
74
const array = vm [ key ] = [ ]
77
75
const originalResolve = resolve
@@ -96,7 +94,7 @@ function bindCollection ({
96
94
path : newIndex ,
97
95
depth : 0 ,
98
96
resolve : resolve . bind ( null , doc )
99
- } )
97
+ } , options )
100
98
} ,
101
99
modified : ( { oldIndex, newIndex, doc } ) => {
102
100
const subs = arraySubs . splice ( oldIndex , 1 ) [ 0 ]
@@ -113,7 +111,7 @@ function bindCollection ({
113
111
path : newIndex ,
114
112
depth : 0 ,
115
113
resolve
116
- } )
114
+ } , options )
117
115
} ,
118
116
removed : ( { oldIndex } ) => {
119
117
array . splice ( oldIndex , 1 )
@@ -147,7 +145,6 @@ function bindCollection ({
147
145
}
148
146
}
149
147
docChanges . forEach ( c => {
150
- // console.log(c)
151
148
change [ c . type ] ( c )
152
149
} )
153
150
@@ -161,7 +158,7 @@ function bindCollection ({
161
158
}
162
159
}
163
160
164
- function updateDataFromDocumentSnapshot ( { snapshot, target, path, subs, depth = 0 , resolve } ) {
161
+ function updateDataFromDocumentSnapshot ( { snapshot, target, path, subs, depth = 0 , resolve } , options ) {
165
162
const [ data , refs ] = extractRefs ( snapshot , walkGet ( target , path ) )
166
163
walkSet ( target , path , data )
167
164
subscribeToRefs ( {
@@ -172,10 +169,10 @@ function updateDataFromDocumentSnapshot ({ snapshot, target, path, subs, depth =
172
169
path,
173
170
depth,
174
171
resolve
175
- } )
172
+ } , options )
176
173
}
177
174
178
- function subscribeToDocument ( { ref, target, path, depth, resolve } ) {
175
+ function subscribeToDocument ( { ref, target, path, depth, resolve } , options ) {
179
176
const subs = Object . create ( null )
180
177
const unbind = ref . onSnapshot ( doc => {
181
178
if ( doc . exists ) {
@@ -186,7 +183,7 @@ function subscribeToDocument ({ ref, target, path, depth, resolve }) {
186
183
subs,
187
184
depth,
188
185
resolve
189
- } )
186
+ } , options )
190
187
} else {
191
188
walkSet ( target , path , null )
192
189
resolve ( path )
@@ -205,7 +202,7 @@ function bindDocument ({
205
202
document,
206
203
resolve,
207
204
reject
208
- } ) {
205
+ } , options ) {
209
206
// TODO warning check if key exists?
210
207
// const boundRefs = Object.create(null)
211
208
@@ -222,7 +219,7 @@ function bindDocument ({
222
219
path : key ,
223
220
subs,
224
221
resolve
225
- } )
222
+ } , options )
226
223
} else {
227
224
resolve ( )
228
225
}
@@ -234,7 +231,7 @@ function bindDocument ({
234
231
}
235
232
}
236
233
237
- function bind ( { vm, key, ref } ) {
234
+ function bind ( { vm, key, ref } , options = { maxRefDepth : 2 } ) {
238
235
return new Promise ( ( resolve , reject ) => {
239
236
let unbind
240
237
if ( ref . where ) {
@@ -244,15 +241,15 @@ function bind ({ vm, key, ref }) {
244
241
collection : ref ,
245
242
resolve,
246
243
reject
247
- } )
244
+ } , options )
248
245
} else {
249
246
unbind = bindDocument ( {
250
247
vm,
251
248
key,
252
249
document : ref ,
253
250
resolve,
254
251
reject
255
- } )
252
+ } , options )
256
253
}
257
254
vm . _firestoreUnbinds [ key ] = unbind
258
255
} )
@@ -286,15 +283,15 @@ function install (Vue, options) {
286
283
} )
287
284
288
285
// TODO test if $bind exist and warns
289
- Vue . prototype . $bind = function ( key , ref ) {
286
+ Vue . prototype . $bind = function ( key , ref , options ) {
290
287
if ( this . _firestoreUnbinds [ key ] ) {
291
288
this . $unbind ( key )
292
289
}
293
290
const promise = bind ( {
294
291
vm : this ,
295
292
key,
296
293
ref
297
- } )
294
+ } , options )
298
295
this . $firestoreRefs [ key ] = ref
299
296
return promise
300
297
}
0 commit comments