1
1
import { createSnapshot , extractRefs , callOnceWithArg , deepGetSplit } from './utils'
2
2
3
+ // NOTE not convinced by the naming of subscribeToRefs and subscribeToDocument
4
+ // first one is calling the other on every ref and subscribeToDocument may call
5
+ // updateDataFromDocumentSnapshot which may call subscribeToRefs as well
6
+ function subscribeToRefs ( {
7
+ subs,
8
+ refs,
9
+ target,
10
+ key,
11
+ data,
12
+ depth,
13
+ resolve
14
+ } ) {
15
+ const refKeys = Object . keys ( refs )
16
+ if ( ! refKeys . length ) return resolve ( )
17
+ // TODO check if no ref is missing
18
+ // TODO max depth param, default to 1?
19
+ if ( ++ depth > 3 ) throw new Error ( 'more than 5 nested refs' )
20
+ refKeys . forEach ( refKey => {
21
+ // check if already bound to the same ref -> skip
22
+ // TODO reuse if already bound?
23
+ const sub = subs [ refKey ]
24
+ const ref = refs [ refKey ]
25
+
26
+ if ( sub ) {
27
+ if ( sub . path !== ref . path ) {
28
+ sub . unbind ( )
29
+ } else {
30
+ // skip it as it's already bound
31
+ // NOTE this is valid as long as target is the same
32
+ // which is not checked anywhere but should be ok
33
+ // because the subs object is created when needed
34
+ return
35
+ }
36
+ }
37
+
38
+ // maybe wrap the unbind function to call unbind on every child
39
+ const [ innerObj , innerKey ] = deepGetSplit ( target [ key ] , refKey )
40
+ if ( ! innerObj ) {
41
+ console . log ( '=== ERROR ===' )
42
+ console . log ( data , refKey , key , innerObj , innerKey )
43
+ console . log ( '===' )
44
+ }
45
+ subs [ refKey ] = {
46
+ unbind : subscribeToDocument ( {
47
+ ref,
48
+ target : innerObj ,
49
+ key : innerKey ,
50
+ depth,
51
+ resolve
52
+ } ) ,
53
+ path : ref . path
54
+ }
55
+ } )
56
+ }
57
+
3
58
function bindCollection ( {
4
59
vm,
5
60
key,
@@ -9,48 +64,21 @@ function bindCollection ({
9
64
} ) {
10
65
// TODO wait to get all data
11
66
const array = vm [ key ] = [ ]
12
- const depth = 0
13
67
14
68
const change = {
15
69
added : ( { newIndex, doc } ) => {
16
70
const subs = { }
17
71
const snapshot = createSnapshot ( doc )
18
72
const [ data , refs ] = extractRefs ( snapshot )
19
73
array . splice ( newIndex , 0 , data )
20
- const refKeys = Object . keys ( refs )
21
- if ( ! refKeys . length ) return // resolve()
22
- // TODO check if no ref is missing
23
- // TODO max depth param, default to 1?
24
- // if (++depth > 3) throw new Error('more than 5 nested refs')
25
- refKeys . forEach ( refKey => {
26
- // check if already bound to the same ref -> skip
27
- const sub = subs [ refKey ]
28
- const ref = refs [ refKey ]
29
- if ( sub && sub . path !== ref . path ) {
30
- sub . unbind ( )
31
- }
32
- // maybe wrap the unbind function to call unbind on every child
33
- const [ innerObj , innerKey ] = deepGetSplit ( array [ newIndex ] , refKey )
34
- if ( ! innerObj ) {
35
- console . log ( '=== ERROR ===' )
36
- console . log ( data , refKey , newIndex , innerObj , innerKey )
37
- console . log ( '===' )
38
- }
39
- subs [ refKey ] = {
40
- unbind : subscribeToDocument ( {
41
- ref,
42
- target : innerObj ,
43
- key : innerKey ,
44
- depth,
45
- // TODO parentSubs
46
- resolve
47
- } ) ,
48
- path : ref . path
49
- }
50
- // unbind currently bound ref
51
- // bind ref
52
- // save unbind callback
53
- // probably save key or something as well
74
+ subscribeToRefs ( {
75
+ data,
76
+ refs,
77
+ subs,
78
+ target : array ,
79
+ key : newIndex ,
80
+ depth : 0 ,
81
+ resolve
54
82
} )
55
83
} ,
56
84
modified : ( { oldIndex, newIndex, doc } ) => {
@@ -80,38 +108,16 @@ function bindCollection ({
80
108
}
81
109
82
110
function updateDataFromDocumentSnapshot ( { snapshot, target, key, subs, depth = 0 , resolve } ) {
83
- // TODO extract refs
84
111
const [ data , refs ] = extractRefs ( snapshot )
85
112
target [ key ] = data
86
- const refKeys = Object . keys ( refs )
87
- if ( ! refKeys . length ) return resolve ( )
88
- // TODO check if no ref is missing
89
- // TODO max depth param, default to 1?
90
- if ( ++ depth > 3 ) throw new Error ( 'more than 5 nested refs' )
91
- refKeys . forEach ( refKey => {
92
- // check if already bound to the same ref -> skip
93
- const sub = subs [ refKey ]
94
- const ref = refs [ refKey ]
95
- if ( sub && sub . path !== ref . path ) {
96
- sub . unbind ( )
97
- }
98
- // maybe wrap the unbind function to call unbind on every child
99
- const [ innerObj , innerKey ] = deepGetSplit ( target [ key ] , refKey )
100
- subs [ refKey ] = {
101
- unbind : subscribeToDocument ( {
102
- ref,
103
- target : innerObj ,
104
- key : innerKey ,
105
- depth,
106
- // TODO parentSubs
107
- resolve
108
- } ) ,
109
- path : ref . path
110
- }
111
- // unbind currently bound ref
112
- // bind ref
113
- // save unbind callback
114
- // probably save key or something as well
113
+ subscribeToRefs ( {
114
+ data,
115
+ subs,
116
+ refs,
117
+ target,
118
+ key,
119
+ depth,
120
+ resolve
115
121
} )
116
122
}
117
123
0 commit comments