Skip to content

Commit ef9aeb2

Browse files
committed
fix(refs): always rebind when updating documents
1 parent 39f646b commit ef9aeb2

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

src/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,8 @@ function subscribeToRefs ({
3030
const sub = subs[refKey]
3131
const ref = refs[refKey]
3232

33-
if (sub) {
34-
if (sub.path !== ref.path) {
35-
sub.unbind()
36-
} else {
37-
// skip it as it's already bound
38-
// NOTE this is valid as long as target is the same
39-
// which is not checked anywhere but should be ok
40-
// because the subs object is created when needed
41-
return
42-
}
43-
}
33+
// documents are fully replaced so it's necessary to bind things again
34+
if (sub) sub.unbind()
4435

4536
// maybe wrap the unbind function to call unbind on every child
4637
const [innerObj, innerKey] = deepGetSplit(target[key], refKey)

test/refs-collections.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,15 @@ test('unbinds refs when items are modified', async () => {
125125

126126
spyA.mockRestore()
127127
})
128+
129+
test('updates when modifying an item', async () => {
130+
await vm.$bind('items', collection)
131+
132+
await first.update({ newThing: true })
133+
await delay(5)
134+
135+
expect(vm.items).toEqual([
136+
{ ref: { isA: true }, newThing: true },
137+
{ ref: { isB: true }}
138+
])
139+
})

test/refs-documents.spec.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,6 @@ test('unbinds previously bound document when overwriting a bound', async () => {
154154
spy.mockRestore()
155155
})
156156

157-
test('does not rebind if it is the same ref', async () => {
158-
const c = collection.doc()
159-
160-
const spy = spyOnSnapshot(c)
161-
await c.update({ baz: 'baz' })
162-
await d.update({ ref: c })
163-
// NOTE see #1
164-
await delay(5)
165-
expect(spy).toHaveBeenCalledTimes(1)
166-
167-
await d.update({ ref: c })
168-
await delay(5)
169-
170-
expect(spy).toHaveBeenCalledTimes(1)
171-
spy.mockRestore()
172-
})
173-
174157
test('resolves the promise when refs are resolved in a document', async () => {
175158
await a.update({ a: true })
176159
await b.update({ ref: a })
@@ -383,3 +366,25 @@ test.skip('binds refs on arrays', async () => {
383366
]
384367
})
385368
})
369+
370+
test('properly updates a documen with refs', async () => {
371+
const item = db.collection().doc()
372+
const a = db.collection().doc()
373+
await a.update({ isA: true })
374+
await item.update({ a })
375+
await vm.$bind('item', item)
376+
377+
expect(vm.item).toEqual({
378+
a: { isA: true }
379+
})
380+
381+
await item.update({ newThing: true })
382+
383+
await delay(5)
384+
385+
expect(vm.item).toEqual({
386+
newThing: true,
387+
a: { isA: true }
388+
})
389+
390+
})

0 commit comments

Comments
 (0)