1
- import { createSnapshot , extractRefs , callOnceWithArg , deepGetSplit } from './utils'
1
+ import { createSnapshot , extractRefs , callOnceWithArg , walkGet , walkSet } from './utils'
2
2
3
3
// NOTE not convinced by the naming of subscribeToRefs and subscribeToDocument
4
4
// first one is calling the other on every ref and subscribeToDocument may call
@@ -7,7 +7,7 @@ function subscribeToRefs ({
7
7
subs,
8
8
refs,
9
9
target,
10
- key ,
10
+ path ,
11
11
data,
12
12
depth,
13
13
resolve
@@ -30,22 +30,24 @@ function subscribeToRefs ({
30
30
const sub = subs [ refKey ]
31
31
const ref = refs [ refKey ]
32
32
33
- // documents are fully replaced so it's necessary to bind things again
34
- // TODO it would be nice to prevent unnecessary unbind
35
- if ( sub ) sub . unbind ( )
33
+ if ( sub ) {
34
+ if ( sub . path !== ref . path ) sub . unbind ( )
35
+ // if has already be bound and as we always walk the objects, it will work
36
+ else return
37
+ }
36
38
37
39
// maybe wrap the unbind function to call unbind on every child
38
- const [ innerObj , innerKey ] = deepGetSplit ( target [ key ] , refKey )
39
- if ( ! innerObj ) {
40
- console . log ( '=== ERROR ===' )
41
- console . log ( data , refKey , key , innerObj , innerKey )
42
- console . log ( '===' )
43
- }
40
+ // const [innerObj, innerKey] = deepGetSplit(target[key], refKey)
41
+ // if (!innerObj) {
42
+ // console.log('=== ERROR ===')
43
+ // console.log(data, refKey, key, innerObj, innerKey)
44
+ // console.log('===')
45
+ // }
44
46
subs [ refKey ] = {
45
47
unbind : subscribeToDocument ( {
46
48
ref,
47
- target : innerObj ,
48
- key : innerKey ,
49
+ target,
50
+ path : ` ${ path } . ${ refKey } ` ,
49
51
depth,
50
52
resolve
51
53
} ) ,
@@ -62,6 +64,7 @@ function bindCollection ({
62
64
reject
63
65
} ) {
64
66
// TODO wait to get all data
67
+ // XXX support pathes? nested.obj.list
65
68
const array = vm [ key ] = [ ]
66
69
const originalResolve = resolve
67
70
let isResolved
@@ -82,24 +85,24 @@ function bindCollection ({
82
85
refs,
83
86
subs,
84
87
target : array ,
85
- key : newIndex ,
88
+ path : newIndex ,
86
89
depth : 0 ,
87
90
resolve : resolve . bind ( null , doc )
88
91
} )
89
92
} ,
90
93
modified : ( { oldIndex, newIndex, doc } ) => {
91
94
const subs = arraySubs . splice ( oldIndex , 1 ) [ 0 ]
92
95
arraySubs . splice ( newIndex , 0 , subs )
93
- array . splice ( oldIndex , 1 )
96
+ const oldData = array . splice ( oldIndex , 1 ) [ 0 ]
94
97
const snapshot = createSnapshot ( doc )
95
- const [ data , refs ] = extractRefs ( snapshot )
98
+ const [ data , refs ] = extractRefs ( snapshot , oldData )
96
99
array . splice ( newIndex , 0 , data )
97
100
subscribeToRefs ( {
98
101
data,
99
102
refs,
100
103
subs,
101
104
target : array ,
102
- key : newIndex ,
105
+ path : newIndex ,
103
106
depth : 0 ,
104
107
resolve
105
108
} )
@@ -131,6 +134,7 @@ function bindCollection ({
131
134
resolve = ( { id } ) => {
132
135
if ( id in validDocs ) {
133
136
if ( ++ count >= expectedItems ) {
137
+ // TODO use array instead?
134
138
originalResolve ( vm [ key ] )
135
139
// reset resolve to noop
136
140
resolve = _ => { }
@@ -157,21 +161,23 @@ function bindCollection ({
157
161
}
158
162
}
159
163
160
- function updateDataFromDocumentSnapshot ( { snapshot, target, key, subs, depth = 0 , resolve } ) {
161
- const [ data , refs ] = extractRefs ( snapshot )
162
- target [ key ] = data
164
+ function updateDataFromDocumentSnapshot ( { snapshot, target, path, subs, depth = 0 , resolve } ) {
165
+ const [ data , refs ] = extractRefs ( snapshot , walkGet ( target , path ) )
166
+ // TODO use walkSet?
167
+ walkSet ( target , path , data )
168
+ // target[key] = data
163
169
subscribeToRefs ( {
164
170
data,
165
171
subs,
166
172
refs,
167
173
target,
168
- key ,
174
+ path ,
169
175
depth,
170
176
resolve
171
177
} )
172
178
}
173
179
174
- function subscribeToDocument ( { ref, target, key , depth, resolve } ) {
180
+ function subscribeToDocument ( { ref, target, path , depth, resolve } ) {
175
181
const subs = Object . create ( null )
176
182
// console.log('subDoc(1)', key)
177
183
// const lol = key
@@ -181,13 +187,15 @@ function subscribeToDocument ({ ref, target, key, depth, resolve }) {
181
187
updateDataFromDocumentSnapshot ( {
182
188
snapshot : createSnapshot ( doc ) ,
183
189
target,
184
- key ,
190
+ path ,
185
191
subs,
186
192
depth,
187
193
resolve
188
194
} )
189
195
} else {
190
- target [ key ] = null
196
+ // TODO use deep set
197
+ walkSet ( target , path , null )
198
+ // target[path] = null
191
199
resolve ( )
192
200
}
193
201
} )
@@ -214,13 +222,14 @@ function bindDocument ({
214
222
const subs = Object . create ( null )
215
223
// bind here the function so it can be resolved anywhere
216
224
// this is specially useful for refs
225
+ // TODO use walkGet?
217
226
resolve = callOnceWithArg ( resolve , ( ) => vm [ key ] )
218
227
const unbind = document . onSnapshot ( doc => {
219
228
if ( doc . exists ) {
220
229
updateDataFromDocumentSnapshot ( {
221
230
snapshot : createSnapshot ( doc ) ,
222
231
target : vm ,
223
- key,
232
+ path : key ,
224
233
subs,
225
234
resolve
226
235
} )
0 commit comments