@@ -64,7 +64,9 @@ function bindCollection ({
64
64
} ) {
65
65
// TODO wait to get all data
66
66
const array = vm [ key ] = [ ]
67
- resolve = callOnceWithArg ( resolve , ( ) => vm [ key ] )
67
+ const originalResolve = resolve
68
+ // resolve = callOnceWithArg(resolve, () => vm[key])
69
+ let isResolved
68
70
69
71
const change = {
70
72
added : ( { newIndex, doc } ) => {
@@ -79,7 +81,7 @@ function bindCollection ({
79
81
target : array ,
80
82
key : newIndex ,
81
83
depth : 0 ,
82
- resolve
84
+ resolve : resolve . bind ( null , doc )
83
85
} )
84
86
} ,
85
87
modified : ( { oldIndex, newIndex, doc } ) => {
@@ -93,9 +95,32 @@ function bindCollection ({
93
95
}
94
96
}
95
97
98
+ // TODO return custom unbind function that unbinds nested refs
96
99
return collection . onSnapshot ( ( { docChanges } ) => {
97
100
// console.log('pending', metadata.hasPendingWrites)
98
101
// docs.forEach(d => console.log('doc', d, '\n', 'data', d.data()))
102
+ // NOTE this will only be triggered once and it will be with all the documents
103
+ // from the query appearing as added
104
+ // (https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots)
105
+ if ( ! isResolved && docChanges . length ) {
106
+ // isResolved is only meant to make sure we do the check only once
107
+ isResolved = true
108
+ let count = 0
109
+ const expectedItems = docChanges . length
110
+ const validDocs = docChanges . reduce ( ( dict , { doc } ) => {
111
+ dict [ doc . id ] = false
112
+ return dict
113
+ } , Object . create ( null ) )
114
+ resolve = ( { id } ) => {
115
+ if ( id in validDocs ) {
116
+ if ( ++ count >= expectedItems ) {
117
+ originalResolve ( vm [ key ] )
118
+ // noop
119
+ resolve = ( ) => { }
120
+ }
121
+ }
122
+ }
123
+ }
99
124
docChanges . forEach ( c => {
100
125
// console.log(c)
101
126
change [ c . type ] ( c )
0 commit comments