Skip to content

Commit 1ac8dae

Browse files
committed
feat: move default options from client constructor to observer constructor
1 parent fc5b56f commit 1ac8dae

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed

libs/core/src/LiveInfiniteQueryObserver.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "@tanstack/query-core";
77

88
import {LiveQueryClient} from "./LiveQueryClient";
9+
import {normalizeRecordQueryResult} from "./utils";
910

1011
export class LiveInfiniteQueryObserver<
1112
TQueryFnData extends InitializedRecord[] = InitializedRecord[],
@@ -31,6 +32,32 @@ export class LiveInfiniteQueryObserver<
3132
>,
3233
) {
3334
super(client, {
35+
// eslint-disable-next-line @typescript-eslint/promise-function-async
36+
queryFn: (ctx) => {
37+
const getQueryOrExpressions = ctx.meta?.getQueryOrExpressions;
38+
if (getQueryOrExpressions == null) {
39+
// eslint-disable-next-line prefer-promise-reject-errors
40+
return Promise.reject("Missing meta.getQueryOrExpressions");
41+
}
42+
const memorySource = client.getMemorySource();
43+
return memorySource.activated.then(async () => {
44+
const result = await memorySource.query(
45+
getQueryOrExpressions(
46+
memorySource.queryBuilder,
47+
ctx.queryKey,
48+
ctx.pageParam ?? 0,
49+
),
50+
);
51+
const normalizedResult = normalizeRecordQueryResult(result);
52+
if (normalizedResult.length > 0) {
53+
return result;
54+
}
55+
if (Array.isArray(result)) {
56+
return [];
57+
}
58+
return undefined;
59+
}) as Promise<TQueryFnData>;
60+
},
3461
initialData: () => {
3562
const enabled = options.enabled;
3663
if (enabled === false) {
@@ -68,6 +95,8 @@ export class LiveInfiniteQueryObserver<
6895
}
6996
return undefined;
7097
},
98+
staleTime: Infinity,
99+
cacheTime: 0,
71100
...options,
72101
meta: {
73102
isInfinite: true,

libs/core/src/LiveQueryClient.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {RecordQueryBuilder} from "@orbit/records";
33
import {QueryClient, QueryClientConfig, QueryKey} from "@tanstack/query-core";
44

55
import {LiveQueryAdapterCache} from "./LiveQueryAdapterCache";
6-
import {normalizeRecordQueryResult} from "./utils";
76

87
export type GetQueryOrExpressions<TQueryKey extends QueryKey = QueryKey> = (
98
queryBuilder: RecordQueryBuilder,
@@ -21,46 +20,6 @@ declare module "@tanstack/query-core" {
2120

2221
export type {QueryMeta} from "@tanstack/query-core";
2322

24-
const mergeLiveQueryClientConfig = (
25-
config: LiveQueryClientConfig,
26-
): QueryClientConfig => ({
27-
...config,
28-
defaultOptions: {
29-
...config.defaultOptions,
30-
queries: {
31-
// eslint-disable-next-line @typescript-eslint/promise-function-async
32-
queryFn: (ctx) => {
33-
const getQueryOrExpressions = ctx.meta?.getQueryOrExpressions;
34-
if (getQueryOrExpressions == null) {
35-
// eslint-disable-next-line prefer-promise-reject-errors
36-
return Promise.reject("Missing meta.getQueryOrExpressions");
37-
}
38-
const memorySource = config.memorySource;
39-
return memorySource.activated.then(async () => {
40-
const result = await memorySource.query(
41-
getQueryOrExpressions(
42-
memorySource.queryBuilder,
43-
ctx.queryKey,
44-
ctx.pageParam ?? 0,
45-
),
46-
);
47-
const normalizedResult = normalizeRecordQueryResult(result);
48-
if (normalizedResult.length > 0) {
49-
return result;
50-
}
51-
if (Array.isArray(result)) {
52-
return [];
53-
}
54-
return undefined;
55-
});
56-
},
57-
staleTime: Infinity,
58-
cacheTime: 0,
59-
...config.defaultOptions?.queries,
60-
},
61-
},
62-
});
63-
6423
export interface LiveQueryClientConfig extends QueryClientConfig {
6524
memorySource: MemorySource;
6625
}
@@ -72,7 +31,7 @@ export class LiveQueryClient extends QueryClient {
7231
private memorySourceIsActivated = false;
7332

7433
constructor(config: LiveQueryClientConfig) {
75-
super(mergeLiveQueryClientConfig(config));
34+
super(config);
7635
const memorySource = (this.memorySource = config.memorySource);
7736
this.liveQueryAdapterCache = new LiveQueryAdapterCache({
7837
client: this,

libs/core/src/LiveQueryObserver.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ export class LiveQueryObserver<
2929
>,
3030
) {
3131
super(client, {
32+
// eslint-disable-next-line @typescript-eslint/promise-function-async
33+
queryFn: (ctx) => {
34+
const getQueryOrExpressions = ctx.meta?.getQueryOrExpressions;
35+
if (getQueryOrExpressions == null) {
36+
// eslint-disable-next-line prefer-promise-reject-errors
37+
return Promise.reject("Missing meta.getQueryOrExpressions");
38+
}
39+
const memorySource = client.getMemorySource();
40+
return memorySource.activated.then(async () => {
41+
const result = await memorySource.query(
42+
getQueryOrExpressions(
43+
memorySource.queryBuilder,
44+
ctx.queryKey,
45+
ctx.pageParam ?? 0,
46+
),
47+
);
48+
const normalizedResult = normalizeRecordQueryResult(result);
49+
if (normalizedResult.length > 0) {
50+
return result;
51+
}
52+
if (Array.isArray(result)) {
53+
return [];
54+
}
55+
return undefined;
56+
}) as Promise<TQueryFnData>;
57+
},
3258
initialData: () => {
3359
const enabled = options.enabled;
3460
if (enabled === false) {
@@ -59,6 +85,8 @@ export class LiveQueryObserver<
5985
}
6086
return undefined;
6187
},
88+
staleTime: Infinity,
89+
cacheTime: 0,
6290
...options,
6391
});
6492
}

0 commit comments

Comments
 (0)