Skip to content

Commit c84d711

Browse files
authored
refactor: remove code for older zero versions (#155)
1 parent 22ce80a commit c84d711

File tree

7 files changed

+34
-179
lines changed

7 files changed

+34
-179
lines changed

pnpm-lock.yaml

Lines changed: 8 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/create-zero-composables.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import type { MaybeRefOrGetter, ShallowRef } from 'vue'
33
import type { QueryResult, UseQueryOptions } from './query'
44
import { Zero } from '@rocicorp/zero'
55
import { shallowRef, toValue, watch } from 'vue'
6-
import { useQueryWithZero } from './query'
7-
8-
export function createZeroComposables<S extends Schema = Schema, MD extends CustomMutatorDefs | undefined = undefined>(optsOrZero: MaybeRefOrGetter<ZeroOptions<S, MD> | { zero: Zero<S, MD> }>) {
6+
import { useQuery as _useQuery } from './query'
7+
8+
export function createZeroComposables<
9+
S extends Schema = Schema,
10+
MD extends CustomMutatorDefs | undefined = undefined,
11+
>(
12+
optsOrZero: MaybeRefOrGetter<ZeroOptions<S, MD> | { zero: Zero<S, MD> }>,
13+
) {
914
let z: ShallowRef<Zero<S, MD>>
1015

1116
function useZero(): ShallowRef<Zero<S, MD>> {
@@ -36,7 +41,7 @@ export function createZeroComposables<S extends Schema = Schema, MD extends Cust
3641
options?: MaybeRefOrGetter<UseQueryOptions>,
3742
): QueryResult<TReturn> {
3843
const z = useZero()
39-
return useQueryWithZero(z, query, options)
44+
return _useQuery(z, query, options)
4045
}
4146

4247
return {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { createZeroComposables } from './create-zero-composables'
22
export type { QueryResult, UseQueryOptions } from './query'
3-
export { useQuery, useQueryWithZero } from './query'
3+
export { useQuery } from './query'

src/query.test.ts

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { TTL } from '@rocicorp/zero'
2-
import type { MockInstance } from 'vitest'
32
import { createBuilder, createSchema, number, string, syncedQuery, table, Zero } from '@rocicorp/zero'
43
import { describe, expect, it, vi } from 'vitest'
54
import { nextTick, ref, watchEffect } from 'vue'
@@ -98,77 +97,23 @@ describe('useQuery', () => {
9897
z.value.close()
9998
})
10099

101-
it('useQuery with ttl ([email protected])', async () => {
100+
it('useQuery with ttl', async () => {
102101
const { z, tableQuery, useQuery } = await setupTestEnvironment()
103-
if (!('updateTTL' in tableQuery)) {
104-
// 0.19 removed updateTTL from the query
105-
return
106-
}
107102

108103
const ttl = ref<TTL>('1m')
109104

110-
const materializeSpy = vi.spyOn(tableQuery, 'materialize')
111-
// @ts-expect-error missing from v0.19+
112-
const updateTTLSpy = vi.spyOn(tableQuery, 'updateTTL')
105+
const materializeSpy = vi.spyOn(z.value, 'materialize')
113106
const queryGetter = vi.fn(() => tableQuery)
114107

115108
useQuery(queryGetter, () => ({ ttl: ttl.value }))
116-
117109
expect(queryGetter).toHaveBeenCalledTimes(1)
118-
expect(updateTTLSpy).toHaveBeenCalledTimes(0)
110+
111+
expect(materializeSpy).toHaveLastReturnedWith(expect.any(VueView))
119112
expect(materializeSpy).toHaveBeenCalledExactlyOnceWith(
113+
tableQuery,
120114
vueViewFactory,
121-
'1m',
115+
{ ttl: '1m' },
122116
)
123-
materializeSpy.mockClear()
124-
125-
ttl.value = '10m'
126-
await nextTick()
127-
128-
expect(materializeSpy).toHaveBeenCalledTimes(0)
129-
expect(updateTTLSpy).toHaveBeenCalledExactlyOnceWith('10m')
130-
131-
z.value.close()
132-
})
133-
134-
it('useQuery with ttl ([email protected])', async () => {
135-
const { z, tableQuery, useQuery } = await setupTestEnvironment()
136-
if ('updateTTL' in tableQuery) {
137-
// 0.19 removed updateTTL from the query
138-
return
139-
}
140-
141-
const ttl = ref<TTL>('1m')
142-
143-
let materializeSpy: MockInstance
144-
// @ts-expect-error only present in v0.23+
145-
if (z.value.materialize) {
146-
materializeSpy = vi.spyOn(z.value, 'materialize')
147-
}
148-
else {
149-
materializeSpy = vi.spyOn(tableQuery, 'materialize')
150-
}
151-
152-
const queryGetter = vi.fn(() => tableQuery)
153-
154-
useQuery(queryGetter, () => ({ ttl: ttl.value }))
155-
expect(queryGetter).toHaveBeenCalledTimes(1)
156-
157-
expect(materializeSpy).toHaveLastReturnedWith(expect.any(VueView))
158-
// @ts-expect-error only present in v0.23+
159-
if (z.value.materialize) {
160-
expect(materializeSpy).toHaveBeenCalledExactlyOnceWith(
161-
tableQuery,
162-
vueViewFactory,
163-
{ ttl: '1m' },
164-
)
165-
}
166-
else {
167-
expect(materializeSpy).toHaveBeenCalledExactlyOnceWith(
168-
vueViewFactory,
169-
'1m',
170-
)
171-
}
172117

173118
const view: VueView<unknown> = materializeSpy.mock.results[0]!.value
174119
const updateTTLSpy = vi.spyOn(view, 'updateTTL')
@@ -329,7 +274,7 @@ describe('useQuery', () => {
329274
await z.mutate.table.insert({ a: 1, b: 'a' })
330275
await z.mutate.table.insert({ a: 2, b: 'b' })
331276

332-
const { data: rows, status } = useQuery(() => z.query.table)
277+
const { data: rows, status } = useQuery(z, () => z.query.table)
333278
expect(rows.value).toMatchInlineSnapshot(`[
334279
{
335280
"a": 1,

src/query.ts

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,17 @@ export interface QueryResult<TReturn> {
2626
error: ComputedRef<QueryError & { retry: () => void } | undefined>
2727
}
2828

29-
/**
30-
* @deprecated
31-
*
32-
* Use `useQuery` returned from `createZero` instead. This function doesn't
33-
* support Synced Queries, and will be removed in a future version.
34-
*
35-
* @param query The query to execute.
36-
* @param options Options for the query.
37-
* @returns The result of the query.
38-
*/
3929
export function useQuery<
4030
TSchema extends Schema,
4131
TTable extends keyof TSchema['tables'] & string,
4232
TReturn,
43-
>(
44-
query: MaybeRefOrGetter<Query<TSchema, TTable, TReturn>>,
45-
options?: MaybeRefOrGetter<UseQueryOptions>,
46-
): QueryResult<TReturn> {
47-
return useQueryWithZero(undefined as unknown as Zero<TSchema>, query, options)
48-
}
49-
50-
export function useQueryWithZero<
51-
TSchema extends Schema,
52-
TTable extends keyof TSchema['tables'] & string,
53-
TReturn,
5433
MD extends CustomMutatorDefs | undefined = undefined,
5534
>(
5635
z: MaybeRefOrGetter<Zero<TSchema, MD>>,
5736
query: MaybeRefOrGetter<Query<TSchema, TTable, TReturn>>,
5837
options?: MaybeRefOrGetter<UseQueryOptions>,
5938
): QueryResult<TReturn> {
60-
const ttl = computed(() => {
61-
return toValue(options)?.ttl ?? DEFAULT_TTL_MS
62-
})
39+
const ttl = computed(() => toValue(options)?.ttl ?? DEFAULT_TTL_MS)
6340
const view = shallowRef<VueView<HumanReadable<TReturn>> | null>(null)
6441
const refetchKey = shallowRef(0)
6542

@@ -71,18 +48,7 @@ export function useQueryWithZero<
7148
],
7249
([q, z]) => {
7350
view.value?.destroy()
74-
75-
// Only present in v0.23+
76-
if (z?.materialize) {
77-
view.value = z.materialize(q, vueViewFactory, { ttl: ttl.value })
78-
return
79-
}
80-
81-
// For synced queries (customQueryID), we need the Zero instance (e.g. during SSR it will be undefined)
82-
if (q.customQueryID)
83-
return
84-
85-
view.value = q.materialize(vueViewFactory, ttl.value)
51+
view.value = z.materialize(q, vueViewFactory, { ttl: toValue(ttl) })
8652
},
8753
{ immediate: true },
8854
)
@@ -96,12 +62,12 @@ export function useQueryWithZero<
9662
}
9763

9864
return {
99-
data: computed(() => view.value!.data),
100-
status: computed(() => view.value!.status),
101-
error: computed(() => view.value!.error
65+
data: computed(() => view.value?.data as HumanReadable<TReturn>),
66+
status: computed(() => view.value?.status ?? 'unknown'),
67+
error: computed(() => view.value?.error
10268
? {
10369
retry: () => { refetchKey.value++ },
104-
...view.value!.error,
70+
...view.value.error,
10571
}
10672
: undefined,
10773
),

src/view.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,7 @@ describe('vueViewFactory', () => {
13061306
onDestroy,
13071307
onTransactionCommit,
13081308
true,
1309+
() => {},
13091310
)
13101311
})
13111312

src/view.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,15 @@ export function vueViewFactory<
162162
onDestroy: () => void,
163163
onTransactionCommit: (cb: () => void) => void,
164164
queryComplete: true | ErroredQuery | Promise<true>,
165-
updateTTL?: (ttl: TTL) => void,
165+
updateTTL: (ttl: TTL) => void,
166166
) {
167-
interface UpdateTTL {
168-
updateTTL: (ttl: TTL) => void
169-
}
170167
return new VueView<HumanReadable<TReturn>>(
171168
input,
172169
onTransactionCommit,
173170
format,
174171
onDestroy,
175172
queryComplete,
176-
// In [email protected] updateTTL is passed in to the view factory.
177-
// In [email protected] it was a property on the query.
178-
updateTTL ?? (ttl =>
179-
(query as unknown as UpdateTTL).updateTTL(ttl)
180-
),
173+
updateTTL,
181174
)
182175
}
183176

0 commit comments

Comments
 (0)