Skip to content

Commit 1b84695

Browse files
committed
feat(refs): handle object-nested refs in docs
1 parent 6114c68 commit 1b84695

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function bindDocument ({
111111
// const boundRefs = Object.create(null)
112112

113113
const subs = Object.create(null)
114-
// bind here the function so it can be resolve anywhere
114+
// bind here the function so it can be resolved anywhere
115115
// this is specially useful for refs
116116
resolve = callOnceWithArg(resolve, () => vm[key])
117117
const unbind = document.onSnapshot(doc => {

src/utils.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,30 @@ export function createSnapshot (doc) {
55
})
66
}
77

8-
export function extractRefs (doc) {
8+
function isObject (o) {
9+
return o && typeof o === 'object'
10+
}
11+
12+
export function extractRefs (doc, path = '', result = [{}, {}]) {
913
return Object.keys(doc).reduce((tot, key) => {
1014
const ref = doc[key]
15+
// if it's a ref
1116
if (typeof ref.isEqual === 'function') {
1217
tot[0][key] = ref.path
1318
// TODO handle subpathes?
14-
tot[1][key] = ref
19+
tot[1][path + key] = ref
20+
} else if (Array.isArray(ref)) {
21+
// TODO hndle array
22+
tot[0][key] = ref
23+
} else if (isObject(ref)) {
24+
console.log('isobject omg', key, ref)
25+
tot[0][key] = {}
26+
extractRefs(ref, key + '.', [tot[0][key], tot[1]])
1527
} else {
16-
// TODO recursive check
1728
tot[0][key] = ref
1829
}
1930
return tot
20-
}, [{}, {}])
31+
}, result)
2132
}
2233

2334
export function callOnceWithArg (fn, argFn) {

test/helpers/mock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class Key {
3030
}
3131
}
3232

33-
class DocumentReference {
33+
export class DocumentReference {
3434
constructor ({ collection, id, data, index }) {
3535
this.collection = collection
3636
this.id = id

test/utils.spec.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import {
2-
createSnapshot
2+
createSnapshot,
3+
extractRefs
34
} from '../src/utils'
45
import {
56
Key,
7+
db,
68
_id,
9+
DocumentReference,
710
DocumentSnapshot
811
} from './helpers'
912

10-
let id, doc, snapshot
13+
let id, doc, snapshot, collection, docRef
1114
beforeEach(() => {
15+
collection = db.collection()
16+
docRef = new DocumentReference({
17+
collection,
18+
id: new Key(),
19+
data: {},
20+
index: 0
21+
})
1222
id = _id
1323
doc = new DocumentSnapshot(null, new Key(), {
1424
n: 42,
1525
is: true,
16-
items: [{ text: 'foo' }]
26+
items: [{ text: 'foo' }],
27+
ref: docRef
1728
})
1829
snapshot = createSnapshot(doc)
1930
})
@@ -30,6 +41,29 @@ test('contains all the data', () => {
3041
expect(snapshot).toEqual({
3142
n: 42,
3243
is: true,
33-
items: [{ text: 'foo' }]
44+
items: [{ text: 'foo' }],
45+
ref: docRef
46+
})
47+
})
48+
49+
test('extract refs from document', () => {
50+
const [noRefsDoc, refs] = extractRefs(doc.data())
51+
expect(noRefsDoc.ref).toEqual(docRef.path)
52+
expect(refs).toEqual({
53+
ref: docRef
54+
})
55+
})
56+
57+
test('extract object nested refs from document', () => {
58+
const [noRefsDoc, refs] = extractRefs({
59+
obj: {
60+
ref: docRef
61+
}
62+
})
63+
console.log(noRefsDoc)
64+
expect(noRefsDoc.obj.ref).toEqual(docRef.path)
65+
console.log(refs)
66+
expect(refs).toEqual({
67+
'obj.ref': docRef
3468
})
3569
})

0 commit comments

Comments
 (0)