Skip to content

Commit

Permalink
feat(refs): unbind missing refs in collections
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 26, 2017
1 parent 5079a35 commit 5bf1ffe
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,21 @@ function bindCollection ({
})
},
modified: ({ oldIndex, newIndex, doc }) => {
array.splice(oldIndex, 1)
array.splice(newIndex, 0, createSnapshot(doc))
// TODO replace listeners of nested refs
const subs = arraySubs.splice(oldIndex, 1)[0]
arraySubs.splice(newIndex, 0, subs)
array.splice(oldIndex, 1)
const snapshot = createSnapshot(doc)
array.splice(newIndex, 0, snapshot)
const [data, refs] = extractRefs(snapshot)
subscribeToRefs({
data,
refs,
subs,
target: array,
key: newIndex,
depth: 0,
resolve
})
},
removed: ({ oldIndex }) => {
array.splice(oldIndex, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('add elements', async () => {

test('delets items', async () => {
await collection.add({ text: 'foo' })
await collection.doc(vm.items[0].id).delete()
await collection.doc(new Key(vm.items[0].id)).delete()
expect(vm.items).toEqual([])
})

Expand Down
18 changes: 12 additions & 6 deletions test/helpers/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ export class DocumentReference {
Object.assign(this.data, data)
this.exists = true
this.cb(new DocumentSnapshot(null, this.id, this.data, true))
return this.collection._modify(this.id, this.data)
return this.collection._modify(this.id, this.data, this)
}

async set (data) {
this.data = { ...data }
this.exists = true
this.cb(new DocumentSnapshot(null, this.id, this.data, true))
return this.collection._modify(this.id, this.data)
return this.collection._modify(this.id, this.data, this)
}
}

Expand Down Expand Up @@ -130,12 +130,12 @@ class CollectionReference {

doc (id) {
id = id || new Key()
return (this.data[id.v] = this.data[id.v] || new DocumentReference({
return this.data[id.v] || new DocumentReference({
collection: this,
id,
data: {},
index: Object.keys(this.data).length
}))
})
}

async _remove (id) {
Expand All @@ -151,10 +151,16 @@ class CollectionReference {
ref.data = null
}

async _modify (id, data) {
async _modify (id, data, ref) {
let type = 'modified'
if (!this.data[id.v]) {
ref.index = Object.keys(this.data).length
this.data[id.v] = ref
type = 'added'
}
this.cb({
docChanges: [{
type: 'modified',
type,
doc: new DocumentSnapshot(null, id, data),
oldIndex: this.data[id.v].index,
newIndex: this.data[id.v].index
Expand Down
19 changes: 16 additions & 3 deletions test/refs-collections.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import {
tick,
delay,
delayUpdate,
Key,
spyUnbind,
Vue
} from './helpers'

Vue.use(Vuefire)

let vm, collection, a, b
let vm, collection, a, b, first
beforeEach(async () => {
a = db.collection().doc()
b = db.collection().doc()
await a.update({ isA: true })
await b.update({ isB: true })
collection = db.collection()
await collection.add({ ref: a })
first = await collection.add({ ref: a })
await collection.add({ ref: b })

vm = new Vue({
Expand Down Expand Up @@ -108,7 +109,19 @@ test('unbinds refs when items are removed', async () => {
await vm.$bind('items', collection)
expect(spyA).toHaveBeenCalledTimes(0)

await collection.doc(a.id).delete()
await collection.doc(new Key(vm.items[0].id)).delete()
expect(spyA).toHaveBeenCalledTimes(1)

spyA.mockRestore()
})

test('unbinds refs when items are modified', async () => {
const spyA = spyUnbind(a)
await vm.$bind('items', collection)
expect(spyA).toHaveBeenCalledTimes(0)

await first.set({ b })

expect(spyA).toHaveBeenCalledTimes(1)

spyA.mockRestore()
Expand Down
2 changes: 1 addition & 1 deletion test/unbind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Vue.use(Vuefire)
let collection, document, vm
beforeEach(async () => {
collection = db.collection()
document = collection.doc()
document = db.collection().doc()
vm = new Vue({
// purposely set items as null
// but it's a good practice to set it to an empty array
Expand Down

0 comments on commit 5bf1ffe

Please sign in to comment.