Skip to content

Commit 1feee9c

Browse files
authored
feat: update to support v0.19 (#54)
1 parent 5ca7d44 commit 1feee9c

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/query.test.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createSchema, number, string, table, Zero } from '@rocicorp/zero'
33
import { describe, expect, it, vi } from 'vitest'
44
import { ref, watchEffect } from 'vue'
55
import { useQuery } from './query'
6-
import { vueViewFactory } from './view'
6+
import { VueView, vueViewFactory } from './view'
77

88
async function setupTestEnvironment() {
99
const schema = createSchema({
@@ -80,8 +80,12 @@ describe('useQuery', () => {
8080
z.close()
8181
})
8282

83-
it('useQuery with ttl', async () => {
83+
it('useQuery with ttl (zero@0.18)', async () => {
8484
const { z, tableQuery } = await setupTestEnvironment()
85+
if (!('updateTTL' in tableQuery)) {
86+
// 0.19 removed updateTTL from the query
87+
return
88+
}
8589
const ttl = ref<TTL>('1m')
8690

8791
const materializeSpy = vi.spyOn(tableQuery, 'materialize')
@@ -107,6 +111,40 @@ describe('useQuery', () => {
107111
z.close()
108112
})
109113

114+
it('useQuery with ttl (zero@0.19)', async () => {
115+
const { z, tableQuery } = await setupTestEnvironment()
116+
if ('updateTTL' in tableQuery) {
117+
// 0.19 removed updateTTL from the query
118+
return
119+
}
120+
121+
const ttl = ref<TTL>('1m')
122+
123+
const materializeSpy = vi.spyOn(tableQuery, 'materialize')
124+
125+
const queryGetter = vi.fn(() => tableQuery)
126+
127+
useQuery(queryGetter, () => ({ ttl: ttl.value }))
128+
expect(queryGetter).toHaveBeenCalledTimes(1)
129+
expect(materializeSpy).toHaveBeenCalledExactlyOnceWith(
130+
vueViewFactory,
131+
'1m',
132+
)
133+
expect(materializeSpy).toHaveLastReturnedWith(expect.any(VueView))
134+
const view: VueView<unknown> = materializeSpy.mock.results[0]!.value
135+
const updateTTLSpy = vi.spyOn(view, 'updateTTL')
136+
137+
materializeSpy.mockClear()
138+
139+
ttl.value = '10m'
140+
await 1
141+
142+
expect(materializeSpy).toHaveBeenCalledTimes(0)
143+
expect(updateTTLSpy).toHaveBeenCalledExactlyOnceWith('10m')
144+
145+
z.close()
146+
})
147+
110148
it('useQuery deps change', async () => {
111149
const { z, tableQuery } = await setupTestEnvironment()
112150

src/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function useQuery<
4747
)
4848

4949
watch(ttl, (ttl) => {
50-
toValue(query).updateTTL(ttl)
50+
toValue(view)?.updateTTL(ttl)
5151
})
5252

5353
if (getCurrentInstance()) {

src/view.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ describe('vueView', () => {
12541254
{ singular: false, relationships: {} },
12551255
() => {},
12561256
queryCompleteResolver.promise,
1257+
() => {},
12571258
)
12581259
})
12591260

src/view.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
Query,
1111
ResultType,
1212
Schema,
13+
TTL,
1314
ViewFactory,
1415
} from '@rocicorp/zero'
1516
import { applyChange } from '@rocicorp/zero'
@@ -28,6 +29,7 @@ export class VueView<V> implements Output {
2829
readonly #input: Input
2930
readonly #format: Format
3031
readonly #onDestroy: () => void
32+
readonly #updateTTL: (ttl: TTL) => void
3133

3234
#state: State
3335

@@ -37,10 +39,12 @@ export class VueView<V> implements Output {
3739
format: Format = { singular: false, relationships: {} },
3840
onDestroy: () => void = () => {},
3941
queryComplete: true | Promise<true>,
42+
updateTTL: (ttl: TTL) => void,
4043
) {
4144
this.#input = input
4245
this.#format = format
4346
this.#onDestroy = onDestroy
47+
this.#updateTTL = updateTTL
4448
this.#state = reactive([
4549
{ '': format.singular ? undefined : [] },
4650
queryComplete === true ? complete : unknown,
@@ -83,26 +87,39 @@ export class VueView<V> implements Output {
8387
push(change: Change): void {
8488
this.#applyChange(change)
8589
}
90+
91+
updateTTL(ttl: TTL): void {
92+
this.#updateTTL(ttl)
93+
}
8694
}
8795

8896
export function vueViewFactory<
8997
TSchema extends Schema,
9098
TTable extends keyof TSchema['tables'] & string,
9199
TReturn,
92100
>(
93-
_query: Query<TSchema, TTable, TReturn>,
101+
query: Query<TSchema, TTable, TReturn>,
94102
input: Input,
95103
format: Format,
96104
onDestroy: () => void,
97105
onTransactionCommit: (cb: () => void) => void,
98106
queryComplete: true | Promise<true>,
107+
updateTTL?: (ttl: TTL) => void,
99108
) {
109+
interface UpdateTTL {
110+
updateTTL: (ttl: TTL) => void
111+
}
100112
return new VueView<HumanReadable<TReturn>>(
101113
input,
102114
onTransactionCommit,
103115
format,
104116
onDestroy,
105117
queryComplete,
118+
// In zero@0.19 updateTTL is passed in to the view factory.
119+
// In zero@0.18 it was a property on the query.
120+
updateTTL ?? (ttl =>
121+
(query as unknown as UpdateTTL).updateTTL(ttl)
122+
),
106123
)
107124
}
108125

0 commit comments

Comments
 (0)