Skip to content

Commit

Permalink
fix(refs): resolve the promise when all resolve are called
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 29, 2017
1 parent 1029c92 commit cb1ab88
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ function subscribeToRefs ({
subs[refKey].unsub()
delete subs[refKey]
})
if (!refKeys.length) return resolve()
if (!refKeys.length) return resolve(path)
// TODO max depth param, default to 1?
if (++depth > 3) throw new Error('more than 5 nested refs')

let resolvedCount = 0
const totalToResolve = refKeys.length
const validResolves = Object.create(null)
function deepResolve (key) {
if (key in validResolves) {
if (++resolvedCount >= totalToResolve) resolve(path)
}
}

refKeys.forEach(refKey => {
const sub = subs[refKey]
const ref = refs[refKey]
const docPath = `${path}.${refKey}`

validResolves[docPath] = true

// unsubscribe if bound to a different ref
if (sub) {
Expand All @@ -44,9 +56,9 @@ function subscribeToRefs ({
unsub: subscribeToDocument({
ref,
target,
path: `${path}.${refKey}`,
path: docPath,
depth,
resolve
resolve: deepResolve.bind(null, docPath)
}),
path: ref.path
}
Expand Down Expand Up @@ -177,7 +189,7 @@ function subscribeToDocument ({ ref, target, path, depth, resolve }) {
})
} else {
walkSet(target, path, null)
resolve()
resolve(path)
}
})

Expand Down
104 changes: 104 additions & 0 deletions test/bind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vuefire from '../src'
import {
db,
tick,
delayUpdate,
Vue
} from './helpers'

Expand Down Expand Up @@ -84,3 +85,106 @@ test('unbinds previously bound refs', async () => {
expect(vm.$firestoreRefs.item).toBe(doc2)
expect(vm.item).toEqual({ bar: 'bar' })
})

test('waits for all refs in document', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
delayUpdate(b)
await document.update({ a, b })

await vm.$bind('item', document)

expect(vm.item).toEqual({
a: null,
b: null
})
})

test('waits for all refs in collection', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
delayUpdate(b)
await collection.add({ a })
await collection.add({ b })

await vm.$bind('items', collection)

expect(vm.items).toEqual([
{ a: null },
{ b: null }
])
})

test('waits for nested refs in document', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
const c = db.collection().doc()
await b.update({ c })
delayUpdate(b)
delayUpdate(c, 5)
await document.update({ a, b })

await vm.$bind('item', document)

expect(vm.item).toEqual({
a: null,
b: { c: null }
})
})

test('waits for nested refs with data in document', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
const c = db.collection().doc()
await a.update({ isA: true })
await c.update({ isC: true })
await b.update({ c })
delayUpdate(b)
delayUpdate(c, 5)
await document.update({ a, b })

await vm.$bind('item', document)

expect(vm.item).toEqual({
a: { isA: true },
b: { c: { isC: true }}
})
})

test('waits for nested refs in collections', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
const c = db.collection().doc()
await b.update({ c })
delayUpdate(b)
delayUpdate(c, 5)
await collection.add({ a })
await collection.add({ b })

await vm.$bind('items', collection)

expect(vm.items).toEqual([
{ a: null },
{ b: { c: null }}
])
})

test('waits for nested refs with data in collections', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
const c = db.collection().doc()
await a.update({ isA: true })
await c.update({ isC: true })
await b.update({ c })
delayUpdate(b)
delayUpdate(c, 5)
await collection.add({ a })
await collection.add({ b })

await vm.$bind('items', collection)

expect(vm.items).toEqual([
{ a: { isA: true }},
{ b: { c: { isC: true }}}
])
})

0 comments on commit cb1ab88

Please sign in to comment.