Skip to content

Commit

Permalink
feat(refs): handle object-nested refs in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 23, 2017
1 parent 6114c68 commit 1b84695
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function bindDocument ({
// const boundRefs = Object.create(null)

const subs = Object.create(null)
// bind here the function so it can be resolve anywhere
// bind here the function so it can be resolved anywhere
// this is specially useful for refs
resolve = callOnceWithArg(resolve, () => vm[key])
const unbind = document.onSnapshot(doc => {
Expand Down
19 changes: 15 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ export function createSnapshot (doc) {
})
}

export function extractRefs (doc) {
function isObject (o) {
return o && typeof o === 'object'
}

export function extractRefs (doc, path = '', result = [{}, {}]) {
return Object.keys(doc).reduce((tot, key) => {
const ref = doc[key]
// if it's a ref
if (typeof ref.isEqual === 'function') {
tot[0][key] = ref.path
// TODO handle subpathes?
tot[1][key] = ref
tot[1][path + key] = ref
} else if (Array.isArray(ref)) {
// TODO hndle array
tot[0][key] = ref
} else if (isObject(ref)) {
console.log('isobject omg', key, ref)
tot[0][key] = {}
extractRefs(ref, key + '.', [tot[0][key], tot[1]])
} else {
// TODO recursive check
tot[0][key] = ref
}
return tot
}, [{}, {}])
}, result)
}

export function callOnceWithArg (fn, argFn) {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Key {
}
}

class DocumentReference {
export class DocumentReference {
constructor ({ collection, id, data, index }) {
this.collection = collection
this.id = id
Expand Down
42 changes: 38 additions & 4 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import {
createSnapshot
createSnapshot,
extractRefs
} from '../src/utils'
import {
Key,
db,
_id,
DocumentReference,
DocumentSnapshot
} from './helpers'

let id, doc, snapshot
let id, doc, snapshot, collection, docRef
beforeEach(() => {
collection = db.collection()
docRef = new DocumentReference({
collection,
id: new Key(),
data: {},
index: 0
})
id = _id
doc = new DocumentSnapshot(null, new Key(), {
n: 42,
is: true,
items: [{ text: 'foo' }]
items: [{ text: 'foo' }],
ref: docRef
})
snapshot = createSnapshot(doc)
})
Expand All @@ -30,6 +41,29 @@ test('contains all the data', () => {
expect(snapshot).toEqual({
n: 42,
is: true,
items: [{ text: 'foo' }]
items: [{ text: 'foo' }],
ref: docRef
})
})

test('extract refs from document', () => {
const [noRefsDoc, refs] = extractRefs(doc.data())
expect(noRefsDoc.ref).toEqual(docRef.path)
expect(refs).toEqual({
ref: docRef
})
})

test('extract object nested refs from document', () => {
const [noRefsDoc, refs] = extractRefs({
obj: {
ref: docRef
}
})
console.log(noRefsDoc)
expect(noRefsDoc.obj.ref).toEqual(docRef.path)
console.log(refs)
expect(refs).toEqual({
'obj.ref': docRef
})
})

0 comments on commit 1b84695

Please sign in to comment.