1
- import { createSnapshot , extractRefs } from './utils'
1
+ import { createSnapshot , extractRefs , callOnceWithArg } from './utils'
2
2
3
3
function bindCollection ( {
4
4
vm,
@@ -38,12 +38,14 @@ function bindCollection ({
38
38
} , reject )
39
39
}
40
40
41
- function updateDataFromDocumentSnapshot ( { snapshot, obj, key, subs, depth = 0 } ) {
41
+ function updateDataFromDocumentSnapshot ( { snapshot, obj, key, subs, depth = 0 , resolve } ) {
42
42
// TODO extract refs
43
43
const [ data , refs ] = extractRefs ( snapshot )
44
44
obj [ key ] = data
45
+ const refKeys = Object . keys ( refs )
46
+ if ( ! refKeys . length ) resolve ( )
45
47
// TODO check if no ref is missing
46
- Object . keys ( refs ) . forEach ( refKey => {
48
+ refKeys . forEach ( refKey => {
47
49
// check if already bound to the same ref -> skip
48
50
const sub = subs [ refKey ]
49
51
const ref = refs [ refKey ]
@@ -56,7 +58,8 @@ function updateDataFromDocumentSnapshot ({ snapshot, obj, key, subs, depth = 0 }
56
58
ref,
57
59
obj : obj [ key ] ,
58
60
key : refKey ,
59
- depth : depth + 1
61
+ depth : depth + 1 ,
62
+ resolve
60
63
} ) ,
61
64
path : ref . path
62
65
}
@@ -67,13 +70,13 @@ function updateDataFromDocumentSnapshot ({ snapshot, obj, key, subs, depth = 0 }
67
70
} )
68
71
}
69
72
70
- function subscribeToDocument ( { ref, obj, key, depth } ) {
73
+ function subscribeToDocument ( { ref, obj, key, depth, resolve } ) {
71
74
// TODO max depth param, default to 1?
72
75
if ( depth > 3 ) throw new Error ( 'more than 5 nested refs' )
73
76
const subs = Object . create ( null )
74
77
return ref . onSnapshot ( doc => {
75
78
if ( doc . exists ) {
76
- updateDataFromDocumentSnapshot ( { snapshot : createSnapshot ( doc ) , obj, key, subs, depth } )
79
+ updateDataFromDocumentSnapshot ( { snapshot : createSnapshot ( doc ) , obj, key, subs, depth, resolve } )
77
80
} else {
78
81
obj [ key ] = null
79
82
}
@@ -88,33 +91,25 @@ function bindDocument ({
88
91
reject
89
92
} ) {
90
93
// TODO warning check if key exists?
91
- // TODO create boundRefs object
92
94
// const boundRefs = Object.create(null)
93
95
94
- let ready
95
96
const subs = Object . create ( null )
97
+ // bind here the function so it can be resolve anywhere
98
+ // this is specially useful for refs
99
+ resolve = callOnceWithArg ( resolve , ( ) => vm [ key ] )
96
100
return document . onSnapshot ( doc => {
97
101
if ( doc . exists ) {
98
102
updateDataFromDocumentSnapshot ( {
99
103
snapshot : createSnapshot ( doc ) ,
100
104
obj : vm ,
101
105
key,
102
- subs
106
+ subs,
107
+ resolve
103
108
} )
109
+ } else {
110
+ resolve ( )
104
111
}
105
- // TODO should resolve be called when all refs are bound?
106
- if ( ! ready ) {
107
- ready = true
108
- resolve ( vm [ key ] )
109
- }
110
- // TODO bind refs
111
- // const d = doc.data()
112
- // if (!boundRefs[d.path]) {
113
- // console.log('bound ref', d.path)
114
- // boundRefs[d.path] = d.onSnapshot((doc) => {
115
- // console.log('ref snap', doc)
116
- // }, err => console.log('onSnapshot ref ERR', err))
117
- // }
112
+ // TODO resolve when does not exist ?
118
113
} , reject )
119
114
120
115
// TODO return a custom unbind function that unbind all refs
0 commit comments