File tree 6 files changed +115
-19
lines changed
6 files changed +115
-19
lines changed Original file line number Diff line number Diff line change 1
- import { createSnapshot } from './utils'
1
+ import { createSnapshot , extractRefs } from './utils'
2
2
3
- function bindCollection ( {
3
+ function bindCollection ( {
4
4
vm,
5
5
key,
6
6
collection,
@@ -34,6 +34,38 @@ function bindCollection({
34
34
35
35
}
36
36
37
+ function bindDocument ( {
38
+ vm,
39
+ key,
40
+ document,
41
+ } ) {
42
+ // TODO warning check if key exists?
43
+ const boundRefs = Object . create ( null )
44
+
45
+ const unbind = document . onSnapshot ( ( doc ) => {
46
+ // TODO test doc.exist
47
+ console . log ( 'doc data' , doc )
48
+ const [ data , refs ] = extractRefs ( createSnapshot ( doc ) )
49
+ vm [ key ] = data
50
+ return
51
+ // TODO bind refs
52
+ const d = doc . data ( )
53
+ if ( ! boundRefs [ d . path ] ) {
54
+ console . log ( 'bound ref' , d . path )
55
+ boundRefs [ d . path ] = d . onSnapshot ( ( doc ) => {
56
+ console . log ( 'ref snap' , doc )
57
+ } , err => console . log ( 'onSnapshot ref ERR' , err ) )
58
+ }
59
+ } , err => {
60
+ console . log ( 'onSnapshot ERR' , err )
61
+ } )
62
+
63
+ return ( ) => {
64
+ // TODO unbind all from boundRefs
65
+ return unbind ( )
66
+ }
67
+ }
68
+
37
69
function install ( Vue , options ) {
38
70
Vue . mixin ( {
39
71
created ( ) {
@@ -48,7 +80,11 @@ function install (Vue, options) {
48
80
collection : ref ,
49
81
} )
50
82
} else {
51
- // TODO
83
+ bindDocument ( {
84
+ vm : this ,
85
+ key,
86
+ document : ref ,
87
+ } )
52
88
}
53
89
} )
54
90
}
Original file line number Diff line number Diff line change @@ -4,3 +4,18 @@ export function createSnapshot (doc) {
4
4
value : doc . id ,
5
5
} )
6
6
}
7
+
8
+ export function extractRefs ( doc ) {
9
+ return Object . keys ( doc ) . reduce ( ( tot , key ) => {
10
+ const ref = doc [ key ]
11
+ if ( typeof ref . isEqual === 'function' ) {
12
+ tot [ 0 ] [ key ] = null
13
+ // TODO handle subpathes?
14
+ tot [ 1 ] [ key ] = ref
15
+ } else {
16
+ // TODO recursive check
17
+ tot [ 0 ] [ key ] = ref
18
+ }
19
+ return tot
20
+ } , [ { } , { } ] )
21
+ }
Original file line number Diff line number Diff line change @@ -45,9 +45,9 @@ test('delets items', async t => {
45
45
} )
46
46
47
47
test ( 'update items' , async t => {
48
- const doc = await t . context . collection . add ( { text : 'foo' } )
48
+ const doc = await t . context . collection . add ( { text : 'foo' , more : true } )
49
49
await doc . update ( { text : 'bar' } )
50
- t . deepEqual ( t . context . vm . items [ 0 ] , { text : 'bar' } )
50
+ t . deepEqual ( t . context . vm . items [ 0 ] , { text : 'bar' , more : true } )
51
51
} )
52
52
53
53
test ( 'add properties' , async t => {
Original file line number Diff line number Diff line change
1
+ import test from 'ava'
2
+ import Vuefire from '../src'
3
+ import {
4
+ createSnapshot
5
+ } from '../src/utils'
6
+ import {
7
+ db ,
8
+ tick ,
9
+ Vue
10
+ } from './helpers'
11
+
12
+ Vue . use ( Vuefire )
13
+
14
+ test . beforeEach ( async t => {
15
+ t . context . collection = db . collection ( )
16
+ t . context . document = t . context . collection . doc ( )
17
+ t . context . vm = new Vue ( {
18
+ render ( h ) {
19
+ return h ( 'ul' , this . items . map (
20
+ item => h ( 'li' , [ item ] )
21
+ ) )
22
+ } ,
23
+ // purposely set items as null
24
+ // but it's a good practice to set it to an empty array
25
+ data : ( ) => ( {
26
+ items : null ,
27
+ item : null ,
28
+ } ) ,
29
+ firestore : {
30
+ items : t . context . collection ,
31
+ item : t . context . document
32
+ }
33
+ } ) . $mount ( )
34
+ await tick ( )
35
+ } )
36
+
37
+ test ( 'binds a document' , t => {
38
+ t . deepEqual ( t . context . vm . item , null )
39
+ } )
40
+
41
+ test ( 'updates a document' , async t => {
42
+ await t . context . document . update ( { foo : 'foo' } )
43
+ t . deepEqual ( t . context . vm . item , { foo : 'foo' } )
44
+ await t . context . document . update ( { bar : 'bar' } )
45
+ t . deepEqual ( t . context . vm . item , { foo : 'foo' , bar : 'bar' } )
46
+ } )
Original file line number Diff line number Diff line change @@ -27,14 +27,22 @@ class DocumentReference {
27
27
this . id = id
28
28
this . data = data
29
29
this . index = index
30
+ this . cb = this . onError = noop
30
31
}
31
32
33
+ onSnapshot ( cb , onError ) {
34
+ this . cb = cb
35
+ this . onError = onError
36
+ }
37
+
38
+
32
39
async delete ( ) {
33
40
return this . collection . _remove ( this . id )
34
41
}
35
42
36
43
async update ( data ) {
37
44
Object . assign ( this . data , data )
45
+ this . cb ( new DocumentSnapshot ( null , this . id , this . data ) )
38
46
return this . collection . _modify ( this . id , this . data )
39
47
}
40
48
}
Original file line number Diff line number Diff line change @@ -22,7 +22,10 @@ test.beforeEach(async t => {
22
22
} ,
23
23
// purposely set items as null
24
24
// but it's a good practice to set it to an empty array
25
- data : ( ) => ( { items : null } ) ,
25
+ data : ( ) => ( {
26
+ items : null ,
27
+ item : null ,
28
+ } ) ,
26
29
firestore : {
27
30
items : t . context . collection ,
28
31
item : t . context . document
@@ -31,22 +34,10 @@ test.beforeEach(async t => {
31
34
await tick ( )
32
35
} )
33
36
34
- test ( 'detects collections' , async t => {
35
- await t . context . document . update ( { foo : 'foo' } )
36
- t . deepEqual ( t . context . vm . item , { foo : 'foo' } )
37
- } )
38
-
39
- test ( 'detects documents' , async t => {
40
- await t . context . collection . add ( { foo : 'foo' } )
41
- t . deepEqual ( t . context . vm . items , [ { foo : 'foo' } ] )
42
- } )
43
-
44
37
test ( 'does nothing with no firestore' , t => {
45
38
const vm = new Vue ( {
46
39
render ( h ) {
47
- return h ( 'ul' , this . items . map (
48
- item => h ( 'li' , [ item ] )
49
- ) )
40
+ return h ( 'p' , 'eh' )
50
41
} ,
51
42
data : ( ) => ( { items : null } ) ,
52
43
} )
You can’t perform that action at this time.
0 commit comments