Skip to content

Commit 8d5a91b

Browse files
committed
refactor: use nested resolve like in document binding
1 parent 773117e commit 8d5a91b

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

src/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function bindCollection ({
6464
}) {
6565
// TODO wait to get all data
6666
const array = vm[key] = []
67+
resolve = callOnceWithArg(resolve, () => vm[key])
6768

6869
const change = {
6970
added: ({ newIndex, doc }) => {
@@ -92,18 +93,16 @@ function bindCollection ({
9293
}
9394
}
9495

95-
let ready
9696
return collection.onSnapshot(({ docChanges }) => {
9797
// console.log('pending', metadata.hasPendingWrites)
9898
// docs.forEach(d => console.log('doc', d, '\n', 'data', d.data()))
9999
docChanges.forEach(c => {
100100
// console.log(c)
101101
change[c.type](c)
102102
})
103-
if (!ready) {
104-
ready = true
105-
resolve(array)
106-
}
103+
104+
// resolves when array is empty
105+
if (!docChanges.length) resolve()
107106
}, reject)
108107
}
109108

test/refs-collections.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ test('binds refs on collections', async () => {
3030
await vm.$bind('items', collection)
3131

3232
// XXX dirty hack until $bind resolves when all refs are bound
33-
// NOTE should add option for it waitForRefs: true (by default)
3433
await delay(5)
3534

3635
expect(vm.items).toEqual([
@@ -45,6 +44,7 @@ test('binds refs when adding to collection', async () => {
4544
await c.update({ isC: true })
4645

4746
await collection.add({ ref: c })
47+
// NOTE wait for refs to update
4848
await delay(5)
4949

5050
expect(vm.items).toEqual([

test/refs-documents.spec.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ beforeEach(async () => {
3535
}
3636
})
3737
await tick()
38-
// XXX dirty hack until $bind resolves when all refs are bound
3938
// NOTE should add option for it waitForRefs: true (by default)
39+
// wait for refs to be ready as well
4040
await delay(5)
4141
})
4242

@@ -74,8 +74,7 @@ test('binds refs on documents', async () => {
7474
await c.update({ foo: 'foo' })
7575
await a.update({ ref: c })
7676

77-
// XXX dirty hack until $bind resolves when all refs are bound
78-
// NOTE should add option for it waitForRefs: true (by default)
77+
// NOTE(1) need to wait because we updated with a ref
7978
await delay(5)
8079

8180
expect(vm.a).toEqual({
@@ -92,9 +91,6 @@ test('binds refs nested in documents (objects)', async () => {
9291
})
9392
await vm.$bind('item', item)
9493

95-
// NOTE same as above
96-
await delay(5)
97-
9894
expect(vm.item).toEqual({
9995
obj: {
10096
ref: { c: true }
@@ -113,9 +109,6 @@ test('binds refs deeply nested in documents (objects)', async () => {
113109
})
114110
await vm.$bind('item', item)
115111

116-
// NOTE same as above
117-
await delay(5)
118-
119112
expect(vm.item).toEqual({
120113
obj: {
121114
nested: {
@@ -146,6 +139,7 @@ test('update inner ref', async () => {
146139
test('is null if ref does not exist', async () => {
147140
await d.update({ ref: a })
148141

142+
// NOTE see #1
149143
await delay(5)
150144

151145
expect(vm.d).toEqual({
@@ -160,24 +154,24 @@ test('unbinds previously bound document when overwriting a bound', async () => {
160154
const spy = spyOnSnapshotCallback(c)
161155
await c.update({ baz: 'baz' })
162156
await d.update({ ref: c })
157+
// NOTE see #1
163158
await delay(5)
164159
expect(spy).toHaveBeenCalledTimes(1)
165160
await c.update({ baz: 'bar' })
166-
await delay(5)
167161
// make sure things are updating correctly
168162
expect(vm.d).toEqual({
169163
ref: { baz: 'bar' }
170164
})
171165
// we call update twice to make sure our mock works
172166
expect(spy).toHaveBeenCalledTimes(2)
173167
await d.update({ ref: a })
168+
// NOTE see #1
174169
await delay(5)
175170

176171
expect(vm.d).toEqual({
177172
ref: null
178173
})
179174
await c.update({ foo: 'bar' })
180-
await delay(5)
181175

182176
expect(spy).toHaveBeenCalledTimes(2)
183177
expect(vm.d).toEqual({
@@ -192,6 +186,7 @@ test('does not rebind if it is the same ref', async () => {
192186
const spy = spyOnSnapshot(c)
193187
await c.update({ baz: 'baz' })
194188
await d.update({ ref: c })
189+
// NOTE see #1
195190
await delay(5)
196191
expect(spy).toHaveBeenCalledTimes(1)
197192

@@ -324,7 +319,7 @@ test('unbinds when a ref is replaced', async () => {
324319
})
325320

326321
await d.update({ ref: a })
327-
// wait for nested ref update
322+
// NOTE see #1
328323
await delay(5)
329324
expect(vm.d).toEqual({
330325
ref: {

0 commit comments

Comments
 (0)