Skip to content

Commit cde4861

Browse files
committed
fix: async requerying
1 parent e502f08 commit cde4861

13 files changed

+40
-22
lines changed

__tests__/Cursor.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe('Cursor', () => {
323323
expect(callbacks.removed).not.toHaveBeenCalled()
324324
})
325325

326-
it('should call the appropriate callbacks when items are added, moved, changed, or removed', () => {
326+
it('should call the appropriate callbacks when items are added, moved, changed, or removed', async () => {
327327
const col = new Collection<TestItem & { count: number }>()
328328
items.forEach((item, index) => col.insert({ ...item, count: index }))
329329

@@ -342,28 +342,33 @@ describe('Cursor', () => {
342342

343343
// Change data
344344
col.insert({ id: 4, name: 'item4', count: 99 }) // Add new item
345+
await new Promise((resolve) => { setTimeout(resolve, 0) })
345346
expect(callbacks.added).toHaveBeenCalledWith(expect.objectContaining({ id: 4, name: 'item4' }))
346347
expect(callbacks.addedBefore).toHaveBeenCalledWith(
347348
expect.objectContaining({ id: 4, name: 'item4' }),
348349
null,
349350
)
350351

351352
col.updateOne({ id: 1 }, { $set: { name: 'item1_modified' } }) // Modify existing item
353+
await new Promise((resolve) => { setTimeout(resolve, 0) })
352354
expect(callbacks.changed).toHaveBeenCalledWith(expect.objectContaining({ id: 1, name: 'item1_modified' }))
353355

354356
col.updateOne({ id: 1 }, { $set: { count: 42 } }) // Move existing item
357+
await new Promise((resolve) => { setTimeout(resolve, 0) })
355358
expect(callbacks.movedBefore).toHaveBeenCalledWith(
356359
expect.objectContaining({ id: 1 }),
357360
expect.objectContaining({ id: 4 }),
358361
)
359362

360363
col.updateOne({ id: 2 }, { $set: { count: 999 } }) // Move existing item
364+
await new Promise((resolve) => { setTimeout(resolve, 0) })
361365
expect(callbacks.movedBefore).toHaveBeenCalledWith(
362366
expect.objectContaining({ id: 2 }),
363367
null,
364368
)
365369

366370
col.removeOne({ id: 2 }) // Remove item
371+
await new Promise((resolve) => { setTimeout(resolve, 0) })
367372
expect(callbacks.removed).toHaveBeenCalledWith(expect.objectContaining({ id: 2, name: 'Item 2' }))
368373
})
369374

__tests__/reactivity/maverick-js.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('@maverick-js/signals', () => {
2828
}),
2929
})
3030

31-
it('should be reactive with @maverick-js/signals', () => {
31+
it('should be reactive with @maverick-js/signals', async () => {
3232
const collection = new Collection({ reactivity })
3333
const callback = vi.fn()
3434

@@ -38,6 +38,7 @@ describe('@maverick-js/signals', () => {
3838
tick()
3939
collection.insert({ id: '1', name: 'John' })
4040
tick()
41+
await new Promise((resolve) => { setTimeout(resolve, 0) })
4142
expect(reactivity.onDispose).toHaveBeenCalledTimes(2)
4243
expect(callback).toHaveBeenCalledTimes(2)
4344
expect(callback).toHaveBeenLastCalledWith(1)

__tests__/reactivity/mobx.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('MobX', () => {
2929
},
3030
})
3131

32-
it('should be reactive with MobX', () => {
32+
it('should be reactive with MobX', async () => {
3333
const collection = new Collection({ reactivity })
3434
const callback = vi.fn()
3535

@@ -38,6 +38,7 @@ describe('MobX', () => {
3838
callback(cursor.count())
3939
})
4040
collection.insert({ id: '1', name: 'John' })
41+
await new Promise((resolve) => { setTimeout(resolve, 0) })
4142
expect(callback).toHaveBeenCalledTimes(2)
4243
expect(callback).toHaveBeenLastCalledWith(1)
4344
stop()

__tests__/reactivity/oby.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('oby', () => {
2727
}),
2828
})
2929

30-
it('should be reactive with oby', () => {
30+
it('should be reactive with oby', async () => {
3131
const collection = new Collection({ reactivity })
3232
const callback = vi.fn()
3333

@@ -37,6 +37,7 @@ describe('oby', () => {
3737
tick()
3838
collection.insert({ id: '1', name: 'John' })
3939
tick()
40+
await new Promise((resolve) => { setTimeout(resolve, 0) })
4041
expect(reactivity.onDispose).toHaveBeenCalledTimes(2)
4142
expect(callback).toHaveBeenCalledTimes(2)
4243
expect(callback).toHaveBeenLastCalledWith(1)

__tests__/reactivity/preact.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('preact', () => {
1818
},
1919
})
2020

21-
it('should be reactive with preact', () => {
21+
it('should be reactive with preact', async () => {
2222
const collection = new Collection({ reactivity })
2323
const callback = vi.fn()
2424
const cleanup = vi.fn()
@@ -32,6 +32,7 @@ describe('preact', () => {
3232
}
3333
})
3434
collection.insert({ id: '1', name: 'John' })
35+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3536
expect(cleanup).toHaveBeenCalledTimes(1)
3637
expect(callback).toHaveBeenCalledTimes(2)
3738
expect(callback).toHaveBeenLastCalledWith(1)

__tests__/reactivity/reactively.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('@reactively/core', () => {
2020
}),
2121
})
2222

23-
it('should be reactive with reactively', () => {
23+
it('should be reactive with reactively', async () => {
2424
const collection = new Collection({ reactivity })
2525
const callback = vi.fn()
2626

@@ -30,7 +30,9 @@ describe('@reactively/core', () => {
3030
})
3131
exec.get()
3232
collection.insert({ id: '1', name: 'John' })
33+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3334
exec.get()
35+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3436

3537
expect(reactivity.onDispose).toHaveBeenCalledTimes(2)
3638
expect(callback).toHaveBeenCalledTimes(2)

__tests__/reactivity/s-js.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('S.js', () => {
2020
}),
2121
})
2222

23-
it('should be reactive with S.js', () => {
23+
it('should be reactive with S.js', async () => {
2424
const collection = new Collection({ reactivity })
2525
const callback = vi.fn()
2626

@@ -29,6 +29,7 @@ describe('S.js', () => {
2929
callback(cursor.count())
3030
})
3131
collection.insert({ id: '1', name: 'John' })
32+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3233
expect(reactivity.onDispose).toHaveBeenCalledTimes(2)
3334
expect(callback).toHaveBeenCalledTimes(2)
3435
expect(callback).toHaveBeenLastCalledWith(1)

__tests__/reactivity/sinuous.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('sinuous', () => {
2323
}),
2424
})
2525

26-
it('should be reactive with sinuous', () => {
26+
it('should be reactive with sinuous', async () => {
2727
const collection = new Collection({ reactivity })
2828
const callback = vi.fn()
2929

@@ -32,6 +32,7 @@ describe('sinuous', () => {
3232
callback(cursor.count())
3333
})
3434
collection.insert({ id: '1', name: 'John' })
35+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3536
expect(reactivity.onDispose).toHaveBeenCalledTimes(2)
3637
expect(callback).toHaveBeenCalledTimes(2)
3738
expect(callback).toHaveBeenLastCalledWith(1)

__tests__/reactivity/solid-js.spec.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@ describe('solid', () => {
2626
},
2727
})
2828

29-
it('should be reactive with solid', () => {
29+
it('should be reactive with solid', async () => {
3030
const callback = vi.fn()
31+
const collection = new Collection({ reactivity })
3132
createRoot(() => {
32-
const collection = new Collection({ reactivity })
33-
3433
const cursor = collection.find({ name: 'John' })
3534
createEffect(() => {
3635
const c = cursor.count()
3736
callback(c)
3837
})
39-
setTimeout(() => {
40-
collection.insert({ id: '1', name: 'John' })
41-
42-
expect(callback).toHaveBeenCalledTimes(2)
43-
expect(callback).toHaveBeenLastCalledWith(1)
44-
})
4538
})
39+
await new Promise((resolve) => { setTimeout(resolve, 0) })
40+
collection.insert({ id: '1', name: 'John' })
41+
42+
await new Promise((resolve) => { setTimeout(resolve, 0) })
43+
expect(callback).toHaveBeenCalledTimes(2)
44+
expect(callback).toHaveBeenLastCalledWith(1)
4645
})
4746
})

__tests__/reactivity/tracker.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Tracker', () => {
2121
}),
2222
})
2323

24-
it('should be reactive with Tracker', () => {
24+
it('should be reactive with Tracker', async () => {
2525
const collection = new Collection({ reactivity })
2626
const callback = vi.fn()
2727

@@ -30,13 +30,15 @@ describe('Tracker', () => {
3030
})
3131
Tracker.flush()
3232
collection.insert({ id: '1', name: 'John' })
33+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3334
Tracker.flush()
35+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3436
expect(reactivity.onDispose).toHaveBeenCalledTimes(2)
3537
expect(callback).toHaveBeenCalledTimes(2)
3638
expect(callback).toHaveBeenLastCalledWith(1)
3739
})
3840

39-
it('should allow overriding reactivity primitives for query', () => {
41+
it('should allow overriding reactivity primitives for query', async () => {
4042
const collection = new Collection()
4143
const callback = vi.fn()
4244

@@ -47,7 +49,9 @@ describe('Tracker', () => {
4749
})
4850
Tracker.flush()
4951
collection.insert({ id: '1', name: 'John' })
52+
await new Promise((resolve) => { setTimeout(resolve, 0) })
5053
Tracker.flush()
54+
await new Promise((resolve) => { setTimeout(resolve, 0) })
5155
expect(callback).toHaveBeenCalledTimes(2)
5256
expect(callback).toHaveBeenLastCalledWith(1)
5357
})

__tests__/reactivity/usignal.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('usignal', () => {
2121
},
2222
})
2323

24-
it('should be reactive with usignal', () => {
24+
it('should be reactive with usignal', async () => {
2525
const collection = new Collection({ reactivity })
2626
const callback = vi.fn()
2727
const cleanup = vi.fn()
@@ -35,6 +35,7 @@ describe('usignal', () => {
3535
}
3636
})
3737
collection.insert({ id: '1', name: 'John' })
38+
await new Promise((resolve) => { setTimeout(resolve, 0) })
3839
expect(cleanup).toHaveBeenCalledTimes(1)
3940
expect(callback).toHaveBeenCalledTimes(2)
4041
expect(callback).toHaveBeenLastCalledWith(1)

__tests__/reactivity/vue.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe('Vue.js', () => {
4444
await vueNextTick()
4545
collection.insert({ id: '1', name: 'John' })
4646
await vueNextTick()
47+
await new Promise((resolve) => { setTimeout(resolve, 0) })
4748
expect(callback).toHaveBeenLastCalledWith(1)
4849
expect(callback).toHaveBeenCalledTimes(2)
4950
expect(reactivity.onDispose).toHaveBeenCalledTimes(2)

src/Collection/Cursor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ export default class Cursor<T extends BaseItem, U = T> {
137137
const observer = new Observer(
138138
transformedCallbacks,
139139
() => {
140-
const cleanup = this.options.bindEvents
141-
&& this.options.bindEvents(() => observer.check(this.getItems()))
140+
const cleanup = this.options.bindEvents && this.options.bindEvents(() =>
141+
setTimeout(() => observer.check(this.getItems()), 0))
142142
return () => {
143143
if (cleanup) cleanup()
144144
}

0 commit comments

Comments
 (0)