1- import { createSnapshot , extractRefs , callOnceWithArg } from './utils'
1+ import { createSnapshot , extractRefs , callOnceWithArg , deepGetSplit } from './utils'
22
33function bindCollection ( {
44 vm,
@@ -43,8 +43,10 @@ function updateDataFromDocumentSnapshot ({ snapshot, obj, key, subs, depth = 0,
4343 const [ data , refs ] = extractRefs ( snapshot )
4444 obj [ key ] = data
4545 const refKeys = Object . keys ( refs )
46- if ( ! refKeys . length ) resolve ( )
46+ if ( ! refKeys . length ) return resolve ( )
4747 // TODO check if no ref is missing
48+ // TODO max depth param, default to 1?
49+ if ( ++ depth > 3 ) throw new Error ( 'more than 5 nested refs' )
4850 refKeys . forEach ( refKey => {
4951 // check if already bound to the same ref -> skip
5052 const sub = subs [ refKey ]
@@ -53,12 +55,13 @@ function updateDataFromDocumentSnapshot ({ snapshot, obj, key, subs, depth = 0,
5355 sub . unbind ( )
5456 }
5557 // maybe wrap the unbind function to call unbind on every child
58+ const [ innerObj , innerKey ] = deepGetSplit ( obj [ key ] , refKey )
5659 subs [ refKey ] = {
5760 unbind : subscribeToDocument ( {
5861 ref,
59- obj : obj [ key ] ,
60- key : refKey ,
61- depth : depth + 1 ,
62+ obj : innerObj ,
63+ key : innerKey ,
64+ depth,
6265 // TODO parentSubs
6366 resolve
6467 } ) ,
@@ -72,8 +75,6 @@ function updateDataFromDocumentSnapshot ({ snapshot, obj, key, subs, depth = 0,
7275}
7376
7477function subscribeToDocument ( { ref, obj, key, depth, resolve } ) {
75- // TODO max depth param, default to 1?
76- if ( depth > 3 ) throw new Error ( 'more than 5 nested refs' )
7778 const subs = Object . create ( null )
7879 const unbind = ref . onSnapshot ( doc => {
7980 if ( doc . exists ) {
0 commit comments