floppy-disk 2.6.0-beta.1 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/preact/create-bi-direction-query.d.ts +167 -0
- package/esm/preact/create-bi-direction-query.js +74 -0
- package/esm/preact/index.d.ts +1 -0
- package/esm/preact/index.js +1 -0
- package/esm/react/create-bi-direction-query.d.ts +167 -0
- package/esm/react/create-bi-direction-query.js +74 -0
- package/esm/react/index.d.ts +1 -0
- package/esm/react/index.js +1 -0
- package/lib/preact/create-bi-direction-query.d.ts +167 -0
- package/lib/preact/create-bi-direction-query.js +78 -0
- package/lib/preact/index.d.ts +1 -0
- package/lib/preact/index.js +1 -0
- package/lib/react/create-bi-direction-query.d.ts +167 -0
- package/lib/react/create-bi-direction-query.js +78 -0
- package/lib/react/index.d.ts +1 -0
- package/lib/react/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Maybe } from '../utils';
|
|
2
|
+
import { SelectDeps } from '../vanilla';
|
|
3
|
+
import { CreateQueryOptions, QueryState } from './create-query';
|
|
4
|
+
import { StoreKey } from './create-stores';
|
|
5
|
+
export declare const createBiDirectionQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData extends any[] = any[], TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>, direction: 'prev' | 'next') => Promise<TResponse>, options: Omit<CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>, "select" | "getNextPageParam"> & {
|
|
6
|
+
getPrevPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
7
|
+
getNextPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
8
|
+
select: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, "data" | "key">, direction: 'prev' | 'next') => TData;
|
|
9
|
+
}) => {
|
|
10
|
+
(...args: [Maybe<TKey>, SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?] | [SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?]): {
|
|
11
|
+
data: (never[] | TData)[number][];
|
|
12
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
13
|
+
hasPrevPage: boolean;
|
|
14
|
+
isWaitingPrevPage: boolean;
|
|
15
|
+
key: TKey;
|
|
16
|
+
keyHash: string;
|
|
17
|
+
fetch: () => void;
|
|
18
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
19
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
20
|
+
reset: () => void;
|
|
21
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
22
|
+
revert: () => void;
|
|
23
|
+
invalidate: () => void;
|
|
24
|
+
};
|
|
25
|
+
isWaiting: boolean;
|
|
26
|
+
isWaitingNextPage: boolean;
|
|
27
|
+
isRefetching: boolean;
|
|
28
|
+
isRefetchError: boolean;
|
|
29
|
+
isPreviousData: boolean;
|
|
30
|
+
isOptimisticData: boolean;
|
|
31
|
+
error: TError | undefined;
|
|
32
|
+
errorUpdatedAt: number | undefined;
|
|
33
|
+
retryCount: number;
|
|
34
|
+
isGoingToRetry: boolean;
|
|
35
|
+
pageParam: Maybe<TPageParam>;
|
|
36
|
+
pageParams: Maybe<TPageParam>[];
|
|
37
|
+
hasNextPage: boolean;
|
|
38
|
+
retryNextPageCount: number;
|
|
39
|
+
isGoingToRetryNextPage: boolean;
|
|
40
|
+
status: "loading";
|
|
41
|
+
isLoading: true;
|
|
42
|
+
isSuccess: false;
|
|
43
|
+
isError: false;
|
|
44
|
+
response: undefined;
|
|
45
|
+
responseUpdatedAt: undefined;
|
|
46
|
+
} | {
|
|
47
|
+
data: (never[] | TData)[number][];
|
|
48
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
49
|
+
hasPrevPage: boolean;
|
|
50
|
+
isWaitingPrevPage: boolean;
|
|
51
|
+
key: TKey;
|
|
52
|
+
keyHash: string;
|
|
53
|
+
fetch: () => void;
|
|
54
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
55
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
56
|
+
reset: () => void;
|
|
57
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
58
|
+
revert: () => void;
|
|
59
|
+
invalidate: () => void;
|
|
60
|
+
};
|
|
61
|
+
isWaiting: boolean;
|
|
62
|
+
isWaitingNextPage: boolean;
|
|
63
|
+
isRefetching: boolean;
|
|
64
|
+
isRefetchError: boolean;
|
|
65
|
+
isPreviousData: boolean;
|
|
66
|
+
isOptimisticData: boolean;
|
|
67
|
+
error: TError | undefined;
|
|
68
|
+
errorUpdatedAt: number | undefined;
|
|
69
|
+
retryCount: number;
|
|
70
|
+
isGoingToRetry: boolean;
|
|
71
|
+
pageParam: Maybe<TPageParam>;
|
|
72
|
+
pageParams: Maybe<TPageParam>[];
|
|
73
|
+
hasNextPage: boolean;
|
|
74
|
+
retryNextPageCount: number;
|
|
75
|
+
isGoingToRetryNextPage: boolean;
|
|
76
|
+
status: "error";
|
|
77
|
+
isLoading: false;
|
|
78
|
+
isSuccess: false;
|
|
79
|
+
isError: true;
|
|
80
|
+
response: undefined;
|
|
81
|
+
responseUpdatedAt: undefined;
|
|
82
|
+
} | {
|
|
83
|
+
data: (never[] | TData)[number][];
|
|
84
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
85
|
+
hasPrevPage: boolean;
|
|
86
|
+
isWaitingPrevPage: boolean;
|
|
87
|
+
key: TKey;
|
|
88
|
+
keyHash: string;
|
|
89
|
+
fetch: () => void;
|
|
90
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
91
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
92
|
+
reset: () => void;
|
|
93
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
94
|
+
revert: () => void;
|
|
95
|
+
invalidate: () => void;
|
|
96
|
+
};
|
|
97
|
+
isWaiting: boolean;
|
|
98
|
+
isWaitingNextPage: boolean;
|
|
99
|
+
isRefetching: boolean;
|
|
100
|
+
isRefetchError: boolean;
|
|
101
|
+
isPreviousData: boolean;
|
|
102
|
+
isOptimisticData: boolean;
|
|
103
|
+
error: TError | undefined;
|
|
104
|
+
errorUpdatedAt: number | undefined;
|
|
105
|
+
retryCount: number;
|
|
106
|
+
isGoingToRetry: boolean;
|
|
107
|
+
pageParam: Maybe<TPageParam>;
|
|
108
|
+
pageParams: Maybe<TPageParam>[];
|
|
109
|
+
hasNextPage: boolean;
|
|
110
|
+
retryNextPageCount: number;
|
|
111
|
+
isGoingToRetryNextPage: boolean;
|
|
112
|
+
status: "success";
|
|
113
|
+
isLoading: false;
|
|
114
|
+
isSuccess: true;
|
|
115
|
+
isError: false;
|
|
116
|
+
response: TResponse;
|
|
117
|
+
responseUpdatedAt: number | undefined;
|
|
118
|
+
};
|
|
119
|
+
get(): {
|
|
120
|
+
prev: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
121
|
+
next: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
122
|
+
};
|
|
123
|
+
setInitialResponse: (options: {
|
|
124
|
+
key?: Maybe<TKey>;
|
|
125
|
+
response: TResponse;
|
|
126
|
+
skipRevalidation?: boolean | undefined;
|
|
127
|
+
}) => void;
|
|
128
|
+
reset(): void;
|
|
129
|
+
resetSpecificKey(key: Maybe<TKey>): void;
|
|
130
|
+
invalidate: () => void;
|
|
131
|
+
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
132
|
+
suspend: (key?: Maybe<TKey>) => {
|
|
133
|
+
key: TKey;
|
|
134
|
+
keyHash: string;
|
|
135
|
+
fetch: () => void;
|
|
136
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
137
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
138
|
+
reset: () => void;
|
|
139
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
140
|
+
revert: () => void;
|
|
141
|
+
invalidate: () => void;
|
|
142
|
+
};
|
|
143
|
+
isWaiting: boolean;
|
|
144
|
+
isWaitingNextPage: boolean;
|
|
145
|
+
isRefetching: boolean;
|
|
146
|
+
isRefetchError: boolean;
|
|
147
|
+
isPreviousData: boolean;
|
|
148
|
+
isOptimisticData: boolean;
|
|
149
|
+
error: TError | undefined;
|
|
150
|
+
errorUpdatedAt: number | undefined;
|
|
151
|
+
retryCount: number;
|
|
152
|
+
isGoingToRetry: boolean;
|
|
153
|
+
pageParam: Maybe<TPageParam>;
|
|
154
|
+
pageParams: Maybe<TPageParam>[];
|
|
155
|
+
hasNextPage: boolean;
|
|
156
|
+
retryNextPageCount: number;
|
|
157
|
+
isGoingToRetryNextPage: boolean;
|
|
158
|
+
} & {
|
|
159
|
+
status: "success";
|
|
160
|
+
isLoading: false;
|
|
161
|
+
isSuccess: true;
|
|
162
|
+
isError: false;
|
|
163
|
+
data: TData;
|
|
164
|
+
response: TResponse;
|
|
165
|
+
responseUpdatedAt: number | undefined;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { hasValue } from '../utils';
|
|
2
|
+
import { createQuery } from './create-query';
|
|
3
|
+
export const createBiDirectionQuery = (queryFn, options) => {
|
|
4
|
+
const { getPrevPageParam, getNextPageParam, select, ...restOptions } = options;
|
|
5
|
+
const usePrevPagesQuery = createQuery((key, state) => queryFn(key, state, 'prev'), {
|
|
6
|
+
defaultDeps: (state) => [
|
|
7
|
+
state.isWaiting,
|
|
8
|
+
state.data,
|
|
9
|
+
state.error,
|
|
10
|
+
state.isWaitingNextPage,
|
|
11
|
+
state.hasNextPage,
|
|
12
|
+
],
|
|
13
|
+
fetchOnMount: false,
|
|
14
|
+
getNextPageParam: getPrevPageParam,
|
|
15
|
+
select: (response, state) => select(response, state, 'prev'),
|
|
16
|
+
...restOptions,
|
|
17
|
+
});
|
|
18
|
+
const useNextPagesQuery = createQuery(async (key, state) => {
|
|
19
|
+
const isInitialPage = state.pageParam === undefined;
|
|
20
|
+
const pQuery = usePrevPagesQuery.get(key);
|
|
21
|
+
try {
|
|
22
|
+
const response = await queryFn(key, state, 'next');
|
|
23
|
+
if (isInitialPage) {
|
|
24
|
+
const prevPageParam = getPrevPageParam(response, 1, pQuery);
|
|
25
|
+
usePrevPagesQuery.set(key, (prev) => ({
|
|
26
|
+
pageParams: [prevPageParam, ...prev.pageParams.slice(1)],
|
|
27
|
+
hasNextPage: prev.isLoading ? hasValue(prevPageParam) : prev.hasNextPage,
|
|
28
|
+
}));
|
|
29
|
+
if (!pQuery.isLoading)
|
|
30
|
+
pQuery.forceFetch();
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (isInitialPage && !pQuery.isLoading)
|
|
36
|
+
pQuery.reset();
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}, {
|
|
40
|
+
getNextPageParam: getNextPageParam,
|
|
41
|
+
select: (response, state) => select(response, state, 'next'),
|
|
42
|
+
...restOptions,
|
|
43
|
+
});
|
|
44
|
+
const useBiDirectionQuery = (...args) => {
|
|
45
|
+
const pQuery = usePrevPagesQuery(...args);
|
|
46
|
+
const nQuery = useNextPagesQuery(...args);
|
|
47
|
+
return {
|
|
48
|
+
...nQuery,
|
|
49
|
+
data: [...(pQuery.data || []), ...(nQuery.data || [])],
|
|
50
|
+
fetchPrevPage: pQuery.fetchNextPage,
|
|
51
|
+
hasPrevPage: pQuery.hasNextPage,
|
|
52
|
+
isWaitingPrevPage: pQuery.isWaitingNextPage || (pQuery.isLoading && pQuery.isWaiting),
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
useBiDirectionQuery.get = () => {
|
|
56
|
+
return {
|
|
57
|
+
prev: usePrevPagesQuery.get(),
|
|
58
|
+
next: useNextPagesQuery.get(),
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
useBiDirectionQuery.setInitialResponse = useNextPagesQuery.setInitialResponse;
|
|
62
|
+
useBiDirectionQuery.reset = () => {
|
|
63
|
+
usePrevPagesQuery.reset();
|
|
64
|
+
useNextPagesQuery.reset();
|
|
65
|
+
};
|
|
66
|
+
useBiDirectionQuery.resetSpecificKey = (key) => {
|
|
67
|
+
usePrevPagesQuery.resetSpecificKey(key);
|
|
68
|
+
useNextPagesQuery.resetSpecificKey(key);
|
|
69
|
+
};
|
|
70
|
+
useBiDirectionQuery.invalidate = useNextPagesQuery.invalidate;
|
|
71
|
+
useBiDirectionQuery.invalidateSpecificKey = useNextPagesQuery.invalidateSpecificKey;
|
|
72
|
+
useBiDirectionQuery.suspend = useNextPagesQuery.suspend;
|
|
73
|
+
return useBiDirectionQuery;
|
|
74
|
+
};
|
package/esm/preact/index.d.ts
CHANGED
package/esm/preact/index.js
CHANGED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Maybe } from '../utils';
|
|
2
|
+
import { SelectDeps } from '../vanilla';
|
|
3
|
+
import { CreateQueryOptions, QueryState } from './create-query';
|
|
4
|
+
import { StoreKey } from './create-stores';
|
|
5
|
+
export declare const createBiDirectionQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData extends any[] = any[], TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>, direction: 'prev' | 'next') => Promise<TResponse>, options: Omit<CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>, "select" | "getNextPageParam"> & {
|
|
6
|
+
getPrevPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
7
|
+
getNextPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
8
|
+
select: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, "data" | "key">, direction: 'prev' | 'next') => TData;
|
|
9
|
+
}) => {
|
|
10
|
+
(...args: [Maybe<TKey>, SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?] | [SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?]): {
|
|
11
|
+
data: (never[] | TData)[number][];
|
|
12
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
13
|
+
hasPrevPage: boolean;
|
|
14
|
+
isWaitingPrevPage: boolean;
|
|
15
|
+
key: TKey;
|
|
16
|
+
keyHash: string;
|
|
17
|
+
fetch: () => void;
|
|
18
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
19
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
20
|
+
reset: () => void;
|
|
21
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
22
|
+
revert: () => void;
|
|
23
|
+
invalidate: () => void;
|
|
24
|
+
};
|
|
25
|
+
isWaiting: boolean;
|
|
26
|
+
isWaitingNextPage: boolean;
|
|
27
|
+
isRefetching: boolean;
|
|
28
|
+
isRefetchError: boolean;
|
|
29
|
+
isPreviousData: boolean;
|
|
30
|
+
isOptimisticData: boolean;
|
|
31
|
+
error: TError | undefined;
|
|
32
|
+
errorUpdatedAt: number | undefined;
|
|
33
|
+
retryCount: number;
|
|
34
|
+
isGoingToRetry: boolean;
|
|
35
|
+
pageParam: Maybe<TPageParam>;
|
|
36
|
+
pageParams: Maybe<TPageParam>[];
|
|
37
|
+
hasNextPage: boolean;
|
|
38
|
+
retryNextPageCount: number;
|
|
39
|
+
isGoingToRetryNextPage: boolean;
|
|
40
|
+
status: "loading";
|
|
41
|
+
isLoading: true;
|
|
42
|
+
isSuccess: false;
|
|
43
|
+
isError: false;
|
|
44
|
+
response: undefined;
|
|
45
|
+
responseUpdatedAt: undefined;
|
|
46
|
+
} | {
|
|
47
|
+
data: (never[] | TData)[number][];
|
|
48
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
49
|
+
hasPrevPage: boolean;
|
|
50
|
+
isWaitingPrevPage: boolean;
|
|
51
|
+
key: TKey;
|
|
52
|
+
keyHash: string;
|
|
53
|
+
fetch: () => void;
|
|
54
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
55
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
56
|
+
reset: () => void;
|
|
57
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
58
|
+
revert: () => void;
|
|
59
|
+
invalidate: () => void;
|
|
60
|
+
};
|
|
61
|
+
isWaiting: boolean;
|
|
62
|
+
isWaitingNextPage: boolean;
|
|
63
|
+
isRefetching: boolean;
|
|
64
|
+
isRefetchError: boolean;
|
|
65
|
+
isPreviousData: boolean;
|
|
66
|
+
isOptimisticData: boolean;
|
|
67
|
+
error: TError | undefined;
|
|
68
|
+
errorUpdatedAt: number | undefined;
|
|
69
|
+
retryCount: number;
|
|
70
|
+
isGoingToRetry: boolean;
|
|
71
|
+
pageParam: Maybe<TPageParam>;
|
|
72
|
+
pageParams: Maybe<TPageParam>[];
|
|
73
|
+
hasNextPage: boolean;
|
|
74
|
+
retryNextPageCount: number;
|
|
75
|
+
isGoingToRetryNextPage: boolean;
|
|
76
|
+
status: "error";
|
|
77
|
+
isLoading: false;
|
|
78
|
+
isSuccess: false;
|
|
79
|
+
isError: true;
|
|
80
|
+
response: undefined;
|
|
81
|
+
responseUpdatedAt: undefined;
|
|
82
|
+
} | {
|
|
83
|
+
data: (never[] | TData)[number][];
|
|
84
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
85
|
+
hasPrevPage: boolean;
|
|
86
|
+
isWaitingPrevPage: boolean;
|
|
87
|
+
key: TKey;
|
|
88
|
+
keyHash: string;
|
|
89
|
+
fetch: () => void;
|
|
90
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
91
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
92
|
+
reset: () => void;
|
|
93
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
94
|
+
revert: () => void;
|
|
95
|
+
invalidate: () => void;
|
|
96
|
+
};
|
|
97
|
+
isWaiting: boolean;
|
|
98
|
+
isWaitingNextPage: boolean;
|
|
99
|
+
isRefetching: boolean;
|
|
100
|
+
isRefetchError: boolean;
|
|
101
|
+
isPreviousData: boolean;
|
|
102
|
+
isOptimisticData: boolean;
|
|
103
|
+
error: TError | undefined;
|
|
104
|
+
errorUpdatedAt: number | undefined;
|
|
105
|
+
retryCount: number;
|
|
106
|
+
isGoingToRetry: boolean;
|
|
107
|
+
pageParam: Maybe<TPageParam>;
|
|
108
|
+
pageParams: Maybe<TPageParam>[];
|
|
109
|
+
hasNextPage: boolean;
|
|
110
|
+
retryNextPageCount: number;
|
|
111
|
+
isGoingToRetryNextPage: boolean;
|
|
112
|
+
status: "success";
|
|
113
|
+
isLoading: false;
|
|
114
|
+
isSuccess: true;
|
|
115
|
+
isError: false;
|
|
116
|
+
response: TResponse;
|
|
117
|
+
responseUpdatedAt: number | undefined;
|
|
118
|
+
};
|
|
119
|
+
get(): {
|
|
120
|
+
prev: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
121
|
+
next: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
122
|
+
};
|
|
123
|
+
setInitialResponse: (options: {
|
|
124
|
+
key?: Maybe<TKey>;
|
|
125
|
+
response: TResponse;
|
|
126
|
+
skipRevalidation?: boolean | undefined;
|
|
127
|
+
}) => void;
|
|
128
|
+
reset(): void;
|
|
129
|
+
resetSpecificKey(key: Maybe<TKey>): void;
|
|
130
|
+
invalidate: () => void;
|
|
131
|
+
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
132
|
+
suspend: (key?: Maybe<TKey>) => {
|
|
133
|
+
key: TKey;
|
|
134
|
+
keyHash: string;
|
|
135
|
+
fetch: () => void;
|
|
136
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
137
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
138
|
+
reset: () => void;
|
|
139
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
140
|
+
revert: () => void;
|
|
141
|
+
invalidate: () => void;
|
|
142
|
+
};
|
|
143
|
+
isWaiting: boolean;
|
|
144
|
+
isWaitingNextPage: boolean;
|
|
145
|
+
isRefetching: boolean;
|
|
146
|
+
isRefetchError: boolean;
|
|
147
|
+
isPreviousData: boolean;
|
|
148
|
+
isOptimisticData: boolean;
|
|
149
|
+
error: TError | undefined;
|
|
150
|
+
errorUpdatedAt: number | undefined;
|
|
151
|
+
retryCount: number;
|
|
152
|
+
isGoingToRetry: boolean;
|
|
153
|
+
pageParam: Maybe<TPageParam>;
|
|
154
|
+
pageParams: Maybe<TPageParam>[];
|
|
155
|
+
hasNextPage: boolean;
|
|
156
|
+
retryNextPageCount: number;
|
|
157
|
+
isGoingToRetryNextPage: boolean;
|
|
158
|
+
} & {
|
|
159
|
+
status: "success";
|
|
160
|
+
isLoading: false;
|
|
161
|
+
isSuccess: true;
|
|
162
|
+
isError: false;
|
|
163
|
+
data: TData;
|
|
164
|
+
response: TResponse;
|
|
165
|
+
responseUpdatedAt: number | undefined;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { hasValue } from '../utils';
|
|
2
|
+
import { createQuery } from './create-query';
|
|
3
|
+
export const createBiDirectionQuery = (queryFn, options) => {
|
|
4
|
+
const { getPrevPageParam, getNextPageParam, select, ...restOptions } = options;
|
|
5
|
+
const usePrevPagesQuery = createQuery((key, state) => queryFn(key, state, 'prev'), {
|
|
6
|
+
defaultDeps: (state) => [
|
|
7
|
+
state.isWaiting,
|
|
8
|
+
state.data,
|
|
9
|
+
state.error,
|
|
10
|
+
state.isWaitingNextPage,
|
|
11
|
+
state.hasNextPage,
|
|
12
|
+
],
|
|
13
|
+
fetchOnMount: false,
|
|
14
|
+
getNextPageParam: getPrevPageParam,
|
|
15
|
+
select: (response, state) => select(response, state, 'prev'),
|
|
16
|
+
...restOptions,
|
|
17
|
+
});
|
|
18
|
+
const useNextPagesQuery = createQuery(async (key, state) => {
|
|
19
|
+
const isInitialPage = state.pageParam === undefined;
|
|
20
|
+
const pQuery = usePrevPagesQuery.get(key);
|
|
21
|
+
try {
|
|
22
|
+
const response = await queryFn(key, state, 'next');
|
|
23
|
+
if (isInitialPage) {
|
|
24
|
+
const prevPageParam = getPrevPageParam(response, 1, pQuery);
|
|
25
|
+
usePrevPagesQuery.set(key, (prev) => ({
|
|
26
|
+
pageParams: [prevPageParam, ...prev.pageParams.slice(1)],
|
|
27
|
+
hasNextPage: prev.isLoading ? hasValue(prevPageParam) : prev.hasNextPage,
|
|
28
|
+
}));
|
|
29
|
+
if (!pQuery.isLoading)
|
|
30
|
+
pQuery.forceFetch();
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (isInitialPage && !pQuery.isLoading)
|
|
36
|
+
pQuery.reset();
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}, {
|
|
40
|
+
getNextPageParam: getNextPageParam,
|
|
41
|
+
select: (response, state) => select(response, state, 'next'),
|
|
42
|
+
...restOptions,
|
|
43
|
+
});
|
|
44
|
+
const useBiDirectionQuery = (...args) => {
|
|
45
|
+
const pQuery = usePrevPagesQuery(...args);
|
|
46
|
+
const nQuery = useNextPagesQuery(...args);
|
|
47
|
+
return {
|
|
48
|
+
...nQuery,
|
|
49
|
+
data: [...(pQuery.data || []), ...(nQuery.data || [])],
|
|
50
|
+
fetchPrevPage: pQuery.fetchNextPage,
|
|
51
|
+
hasPrevPage: pQuery.hasNextPage,
|
|
52
|
+
isWaitingPrevPage: pQuery.isWaitingNextPage || (pQuery.isLoading && pQuery.isWaiting),
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
useBiDirectionQuery.get = () => {
|
|
56
|
+
return {
|
|
57
|
+
prev: usePrevPagesQuery.get(),
|
|
58
|
+
next: useNextPagesQuery.get(),
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
useBiDirectionQuery.setInitialResponse = useNextPagesQuery.setInitialResponse;
|
|
62
|
+
useBiDirectionQuery.reset = () => {
|
|
63
|
+
usePrevPagesQuery.reset();
|
|
64
|
+
useNextPagesQuery.reset();
|
|
65
|
+
};
|
|
66
|
+
useBiDirectionQuery.resetSpecificKey = (key) => {
|
|
67
|
+
usePrevPagesQuery.resetSpecificKey(key);
|
|
68
|
+
useNextPagesQuery.resetSpecificKey(key);
|
|
69
|
+
};
|
|
70
|
+
useBiDirectionQuery.invalidate = useNextPagesQuery.invalidate;
|
|
71
|
+
useBiDirectionQuery.invalidateSpecificKey = useNextPagesQuery.invalidateSpecificKey;
|
|
72
|
+
useBiDirectionQuery.suspend = useNextPagesQuery.suspend;
|
|
73
|
+
return useBiDirectionQuery;
|
|
74
|
+
};
|
package/esm/react/index.d.ts
CHANGED
package/esm/react/index.js
CHANGED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Maybe } from '../utils';
|
|
2
|
+
import { SelectDeps } from '../vanilla';
|
|
3
|
+
import { CreateQueryOptions, QueryState } from './create-query';
|
|
4
|
+
import { StoreKey } from './create-stores';
|
|
5
|
+
export declare const createBiDirectionQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData extends any[] = any[], TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>, direction: 'prev' | 'next') => Promise<TResponse>, options: Omit<CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>, "select" | "getNextPageParam"> & {
|
|
6
|
+
getPrevPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
7
|
+
getNextPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
8
|
+
select: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, "data" | "key">, direction: 'prev' | 'next') => TData;
|
|
9
|
+
}) => {
|
|
10
|
+
(...args: [Maybe<TKey>, SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?] | [SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?]): {
|
|
11
|
+
data: (never[] | TData)[number][];
|
|
12
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
13
|
+
hasPrevPage: boolean;
|
|
14
|
+
isWaitingPrevPage: boolean;
|
|
15
|
+
key: TKey;
|
|
16
|
+
keyHash: string;
|
|
17
|
+
fetch: () => void;
|
|
18
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
19
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
20
|
+
reset: () => void;
|
|
21
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
22
|
+
revert: () => void;
|
|
23
|
+
invalidate: () => void;
|
|
24
|
+
};
|
|
25
|
+
isWaiting: boolean;
|
|
26
|
+
isWaitingNextPage: boolean;
|
|
27
|
+
isRefetching: boolean;
|
|
28
|
+
isRefetchError: boolean;
|
|
29
|
+
isPreviousData: boolean;
|
|
30
|
+
isOptimisticData: boolean;
|
|
31
|
+
error: TError | undefined;
|
|
32
|
+
errorUpdatedAt: number | undefined;
|
|
33
|
+
retryCount: number;
|
|
34
|
+
isGoingToRetry: boolean;
|
|
35
|
+
pageParam: Maybe<TPageParam>;
|
|
36
|
+
pageParams: Maybe<TPageParam>[];
|
|
37
|
+
hasNextPage: boolean;
|
|
38
|
+
retryNextPageCount: number;
|
|
39
|
+
isGoingToRetryNextPage: boolean;
|
|
40
|
+
status: "loading";
|
|
41
|
+
isLoading: true;
|
|
42
|
+
isSuccess: false;
|
|
43
|
+
isError: false;
|
|
44
|
+
response: undefined;
|
|
45
|
+
responseUpdatedAt: undefined;
|
|
46
|
+
} | {
|
|
47
|
+
data: (never[] | TData)[number][];
|
|
48
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
49
|
+
hasPrevPage: boolean;
|
|
50
|
+
isWaitingPrevPage: boolean;
|
|
51
|
+
key: TKey;
|
|
52
|
+
keyHash: string;
|
|
53
|
+
fetch: () => void;
|
|
54
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
55
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
56
|
+
reset: () => void;
|
|
57
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
58
|
+
revert: () => void;
|
|
59
|
+
invalidate: () => void;
|
|
60
|
+
};
|
|
61
|
+
isWaiting: boolean;
|
|
62
|
+
isWaitingNextPage: boolean;
|
|
63
|
+
isRefetching: boolean;
|
|
64
|
+
isRefetchError: boolean;
|
|
65
|
+
isPreviousData: boolean;
|
|
66
|
+
isOptimisticData: boolean;
|
|
67
|
+
error: TError | undefined;
|
|
68
|
+
errorUpdatedAt: number | undefined;
|
|
69
|
+
retryCount: number;
|
|
70
|
+
isGoingToRetry: boolean;
|
|
71
|
+
pageParam: Maybe<TPageParam>;
|
|
72
|
+
pageParams: Maybe<TPageParam>[];
|
|
73
|
+
hasNextPage: boolean;
|
|
74
|
+
retryNextPageCount: number;
|
|
75
|
+
isGoingToRetryNextPage: boolean;
|
|
76
|
+
status: "error";
|
|
77
|
+
isLoading: false;
|
|
78
|
+
isSuccess: false;
|
|
79
|
+
isError: true;
|
|
80
|
+
response: undefined;
|
|
81
|
+
responseUpdatedAt: undefined;
|
|
82
|
+
} | {
|
|
83
|
+
data: (never[] | TData)[number][];
|
|
84
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
85
|
+
hasPrevPage: boolean;
|
|
86
|
+
isWaitingPrevPage: boolean;
|
|
87
|
+
key: TKey;
|
|
88
|
+
keyHash: string;
|
|
89
|
+
fetch: () => void;
|
|
90
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
91
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
92
|
+
reset: () => void;
|
|
93
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
94
|
+
revert: () => void;
|
|
95
|
+
invalidate: () => void;
|
|
96
|
+
};
|
|
97
|
+
isWaiting: boolean;
|
|
98
|
+
isWaitingNextPage: boolean;
|
|
99
|
+
isRefetching: boolean;
|
|
100
|
+
isRefetchError: boolean;
|
|
101
|
+
isPreviousData: boolean;
|
|
102
|
+
isOptimisticData: boolean;
|
|
103
|
+
error: TError | undefined;
|
|
104
|
+
errorUpdatedAt: number | undefined;
|
|
105
|
+
retryCount: number;
|
|
106
|
+
isGoingToRetry: boolean;
|
|
107
|
+
pageParam: Maybe<TPageParam>;
|
|
108
|
+
pageParams: Maybe<TPageParam>[];
|
|
109
|
+
hasNextPage: boolean;
|
|
110
|
+
retryNextPageCount: number;
|
|
111
|
+
isGoingToRetryNextPage: boolean;
|
|
112
|
+
status: "success";
|
|
113
|
+
isLoading: false;
|
|
114
|
+
isSuccess: true;
|
|
115
|
+
isError: false;
|
|
116
|
+
response: TResponse;
|
|
117
|
+
responseUpdatedAt: number | undefined;
|
|
118
|
+
};
|
|
119
|
+
get(): {
|
|
120
|
+
prev: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
121
|
+
next: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
122
|
+
};
|
|
123
|
+
setInitialResponse: (options: {
|
|
124
|
+
key?: Maybe<TKey>;
|
|
125
|
+
response: TResponse;
|
|
126
|
+
skipRevalidation?: boolean | undefined;
|
|
127
|
+
}) => void;
|
|
128
|
+
reset(): void;
|
|
129
|
+
resetSpecificKey(key: Maybe<TKey>): void;
|
|
130
|
+
invalidate: () => void;
|
|
131
|
+
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
132
|
+
suspend: (key?: Maybe<TKey>) => {
|
|
133
|
+
key: TKey;
|
|
134
|
+
keyHash: string;
|
|
135
|
+
fetch: () => void;
|
|
136
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
137
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
138
|
+
reset: () => void;
|
|
139
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
140
|
+
revert: () => void;
|
|
141
|
+
invalidate: () => void;
|
|
142
|
+
};
|
|
143
|
+
isWaiting: boolean;
|
|
144
|
+
isWaitingNextPage: boolean;
|
|
145
|
+
isRefetching: boolean;
|
|
146
|
+
isRefetchError: boolean;
|
|
147
|
+
isPreviousData: boolean;
|
|
148
|
+
isOptimisticData: boolean;
|
|
149
|
+
error: TError | undefined;
|
|
150
|
+
errorUpdatedAt: number | undefined;
|
|
151
|
+
retryCount: number;
|
|
152
|
+
isGoingToRetry: boolean;
|
|
153
|
+
pageParam: Maybe<TPageParam>;
|
|
154
|
+
pageParams: Maybe<TPageParam>[];
|
|
155
|
+
hasNextPage: boolean;
|
|
156
|
+
retryNextPageCount: number;
|
|
157
|
+
isGoingToRetryNextPage: boolean;
|
|
158
|
+
} & {
|
|
159
|
+
status: "success";
|
|
160
|
+
isLoading: false;
|
|
161
|
+
isSuccess: true;
|
|
162
|
+
isError: false;
|
|
163
|
+
data: TData;
|
|
164
|
+
response: TResponse;
|
|
165
|
+
responseUpdatedAt: number | undefined;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createBiDirectionQuery = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const create_query_1 = require("./create-query");
|
|
6
|
+
const createBiDirectionQuery = (queryFn, options) => {
|
|
7
|
+
const { getPrevPageParam, getNextPageParam, select, ...restOptions } = options;
|
|
8
|
+
const usePrevPagesQuery = (0, create_query_1.createQuery)((key, state) => queryFn(key, state, 'prev'), {
|
|
9
|
+
defaultDeps: (state) => [
|
|
10
|
+
state.isWaiting,
|
|
11
|
+
state.data,
|
|
12
|
+
state.error,
|
|
13
|
+
state.isWaitingNextPage,
|
|
14
|
+
state.hasNextPage,
|
|
15
|
+
],
|
|
16
|
+
fetchOnMount: false,
|
|
17
|
+
getNextPageParam: getPrevPageParam,
|
|
18
|
+
select: (response, state) => select(response, state, 'prev'),
|
|
19
|
+
...restOptions,
|
|
20
|
+
});
|
|
21
|
+
const useNextPagesQuery = (0, create_query_1.createQuery)(async (key, state) => {
|
|
22
|
+
const isInitialPage = state.pageParam === undefined;
|
|
23
|
+
const pQuery = usePrevPagesQuery.get(key);
|
|
24
|
+
try {
|
|
25
|
+
const response = await queryFn(key, state, 'next');
|
|
26
|
+
if (isInitialPage) {
|
|
27
|
+
const prevPageParam = getPrevPageParam(response, 1, pQuery);
|
|
28
|
+
usePrevPagesQuery.set(key, (prev) => ({
|
|
29
|
+
pageParams: [prevPageParam, ...prev.pageParams.slice(1)],
|
|
30
|
+
hasNextPage: prev.isLoading ? (0, utils_1.hasValue)(prevPageParam) : prev.hasNextPage,
|
|
31
|
+
}));
|
|
32
|
+
if (!pQuery.isLoading)
|
|
33
|
+
pQuery.forceFetch();
|
|
34
|
+
}
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (isInitialPage && !pQuery.isLoading)
|
|
39
|
+
pQuery.reset();
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}, {
|
|
43
|
+
getNextPageParam: getNextPageParam,
|
|
44
|
+
select: (response, state) => select(response, state, 'next'),
|
|
45
|
+
...restOptions,
|
|
46
|
+
});
|
|
47
|
+
const useBiDirectionQuery = (...args) => {
|
|
48
|
+
const pQuery = usePrevPagesQuery(...args);
|
|
49
|
+
const nQuery = useNextPagesQuery(...args);
|
|
50
|
+
return {
|
|
51
|
+
...nQuery,
|
|
52
|
+
data: [...(pQuery.data || []), ...(nQuery.data || [])],
|
|
53
|
+
fetchPrevPage: pQuery.fetchNextPage,
|
|
54
|
+
hasPrevPage: pQuery.hasNextPage,
|
|
55
|
+
isWaitingPrevPage: pQuery.isWaitingNextPage || (pQuery.isLoading && pQuery.isWaiting),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
useBiDirectionQuery.get = () => {
|
|
59
|
+
return {
|
|
60
|
+
prev: usePrevPagesQuery.get(),
|
|
61
|
+
next: useNextPagesQuery.get(),
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
useBiDirectionQuery.setInitialResponse = useNextPagesQuery.setInitialResponse;
|
|
65
|
+
useBiDirectionQuery.reset = () => {
|
|
66
|
+
usePrevPagesQuery.reset();
|
|
67
|
+
useNextPagesQuery.reset();
|
|
68
|
+
};
|
|
69
|
+
useBiDirectionQuery.resetSpecificKey = (key) => {
|
|
70
|
+
usePrevPagesQuery.resetSpecificKey(key);
|
|
71
|
+
useNextPagesQuery.resetSpecificKey(key);
|
|
72
|
+
};
|
|
73
|
+
useBiDirectionQuery.invalidate = useNextPagesQuery.invalidate;
|
|
74
|
+
useBiDirectionQuery.invalidateSpecificKey = useNextPagesQuery.invalidateSpecificKey;
|
|
75
|
+
useBiDirectionQuery.suspend = useNextPagesQuery.suspend;
|
|
76
|
+
return useBiDirectionQuery;
|
|
77
|
+
};
|
|
78
|
+
exports.createBiDirectionQuery = createBiDirectionQuery;
|
package/lib/preact/index.d.ts
CHANGED
package/lib/preact/index.js
CHANGED
|
@@ -4,5 +4,6 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
tslib_1.__exportStar(require("./create-store"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./create-stores"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./create-query"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./create-bi-direction-query"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./create-mutation"), exports);
|
|
8
9
|
tslib_1.__exportStar(require("./with-context"), exports);
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Maybe } from '../utils';
|
|
2
|
+
import { SelectDeps } from '../vanilla';
|
|
3
|
+
import { CreateQueryOptions, QueryState } from './create-query';
|
|
4
|
+
import { StoreKey } from './create-stores';
|
|
5
|
+
export declare const createBiDirectionQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData extends any[] = any[], TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>, direction: 'prev' | 'next') => Promise<TResponse>, options: Omit<CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>, "select" | "getNextPageParam"> & {
|
|
6
|
+
getPrevPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
7
|
+
getNextPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
8
|
+
select: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, "data" | "key">, direction: 'prev' | 'next') => TData;
|
|
9
|
+
}) => {
|
|
10
|
+
(...args: [Maybe<TKey>, SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?] | [SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?]): {
|
|
11
|
+
data: (never[] | TData)[number][];
|
|
12
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
13
|
+
hasPrevPage: boolean;
|
|
14
|
+
isWaitingPrevPage: boolean;
|
|
15
|
+
key: TKey;
|
|
16
|
+
keyHash: string;
|
|
17
|
+
fetch: () => void;
|
|
18
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
19
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
20
|
+
reset: () => void;
|
|
21
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
22
|
+
revert: () => void;
|
|
23
|
+
invalidate: () => void;
|
|
24
|
+
};
|
|
25
|
+
isWaiting: boolean;
|
|
26
|
+
isWaitingNextPage: boolean;
|
|
27
|
+
isRefetching: boolean;
|
|
28
|
+
isRefetchError: boolean;
|
|
29
|
+
isPreviousData: boolean;
|
|
30
|
+
isOptimisticData: boolean;
|
|
31
|
+
error: TError | undefined;
|
|
32
|
+
errorUpdatedAt: number | undefined;
|
|
33
|
+
retryCount: number;
|
|
34
|
+
isGoingToRetry: boolean;
|
|
35
|
+
pageParam: Maybe<TPageParam>;
|
|
36
|
+
pageParams: Maybe<TPageParam>[];
|
|
37
|
+
hasNextPage: boolean;
|
|
38
|
+
retryNextPageCount: number;
|
|
39
|
+
isGoingToRetryNextPage: boolean;
|
|
40
|
+
status: "loading";
|
|
41
|
+
isLoading: true;
|
|
42
|
+
isSuccess: false;
|
|
43
|
+
isError: false;
|
|
44
|
+
response: undefined;
|
|
45
|
+
responseUpdatedAt: undefined;
|
|
46
|
+
} | {
|
|
47
|
+
data: (never[] | TData)[number][];
|
|
48
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
49
|
+
hasPrevPage: boolean;
|
|
50
|
+
isWaitingPrevPage: boolean;
|
|
51
|
+
key: TKey;
|
|
52
|
+
keyHash: string;
|
|
53
|
+
fetch: () => void;
|
|
54
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
55
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
56
|
+
reset: () => void;
|
|
57
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
58
|
+
revert: () => void;
|
|
59
|
+
invalidate: () => void;
|
|
60
|
+
};
|
|
61
|
+
isWaiting: boolean;
|
|
62
|
+
isWaitingNextPage: boolean;
|
|
63
|
+
isRefetching: boolean;
|
|
64
|
+
isRefetchError: boolean;
|
|
65
|
+
isPreviousData: boolean;
|
|
66
|
+
isOptimisticData: boolean;
|
|
67
|
+
error: TError | undefined;
|
|
68
|
+
errorUpdatedAt: number | undefined;
|
|
69
|
+
retryCount: number;
|
|
70
|
+
isGoingToRetry: boolean;
|
|
71
|
+
pageParam: Maybe<TPageParam>;
|
|
72
|
+
pageParams: Maybe<TPageParam>[];
|
|
73
|
+
hasNextPage: boolean;
|
|
74
|
+
retryNextPageCount: number;
|
|
75
|
+
isGoingToRetryNextPage: boolean;
|
|
76
|
+
status: "error";
|
|
77
|
+
isLoading: false;
|
|
78
|
+
isSuccess: false;
|
|
79
|
+
isError: true;
|
|
80
|
+
response: undefined;
|
|
81
|
+
responseUpdatedAt: undefined;
|
|
82
|
+
} | {
|
|
83
|
+
data: (never[] | TData)[number][];
|
|
84
|
+
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
85
|
+
hasPrevPage: boolean;
|
|
86
|
+
isWaitingPrevPage: boolean;
|
|
87
|
+
key: TKey;
|
|
88
|
+
keyHash: string;
|
|
89
|
+
fetch: () => void;
|
|
90
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
91
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
92
|
+
reset: () => void;
|
|
93
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
94
|
+
revert: () => void;
|
|
95
|
+
invalidate: () => void;
|
|
96
|
+
};
|
|
97
|
+
isWaiting: boolean;
|
|
98
|
+
isWaitingNextPage: boolean;
|
|
99
|
+
isRefetching: boolean;
|
|
100
|
+
isRefetchError: boolean;
|
|
101
|
+
isPreviousData: boolean;
|
|
102
|
+
isOptimisticData: boolean;
|
|
103
|
+
error: TError | undefined;
|
|
104
|
+
errorUpdatedAt: number | undefined;
|
|
105
|
+
retryCount: number;
|
|
106
|
+
isGoingToRetry: boolean;
|
|
107
|
+
pageParam: Maybe<TPageParam>;
|
|
108
|
+
pageParams: Maybe<TPageParam>[];
|
|
109
|
+
hasNextPage: boolean;
|
|
110
|
+
retryNextPageCount: number;
|
|
111
|
+
isGoingToRetryNextPage: boolean;
|
|
112
|
+
status: "success";
|
|
113
|
+
isLoading: false;
|
|
114
|
+
isSuccess: true;
|
|
115
|
+
isError: false;
|
|
116
|
+
response: TResponse;
|
|
117
|
+
responseUpdatedAt: number | undefined;
|
|
118
|
+
};
|
|
119
|
+
get(): {
|
|
120
|
+
prev: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
121
|
+
next: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
122
|
+
};
|
|
123
|
+
setInitialResponse: (options: {
|
|
124
|
+
key?: Maybe<TKey>;
|
|
125
|
+
response: TResponse;
|
|
126
|
+
skipRevalidation?: boolean | undefined;
|
|
127
|
+
}) => void;
|
|
128
|
+
reset(): void;
|
|
129
|
+
resetSpecificKey(key: Maybe<TKey>): void;
|
|
130
|
+
invalidate: () => void;
|
|
131
|
+
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
132
|
+
suspend: (key?: Maybe<TKey>) => {
|
|
133
|
+
key: TKey;
|
|
134
|
+
keyHash: string;
|
|
135
|
+
fetch: () => void;
|
|
136
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
137
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
138
|
+
reset: () => void;
|
|
139
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
140
|
+
revert: () => void;
|
|
141
|
+
invalidate: () => void;
|
|
142
|
+
};
|
|
143
|
+
isWaiting: boolean;
|
|
144
|
+
isWaitingNextPage: boolean;
|
|
145
|
+
isRefetching: boolean;
|
|
146
|
+
isRefetchError: boolean;
|
|
147
|
+
isPreviousData: boolean;
|
|
148
|
+
isOptimisticData: boolean;
|
|
149
|
+
error: TError | undefined;
|
|
150
|
+
errorUpdatedAt: number | undefined;
|
|
151
|
+
retryCount: number;
|
|
152
|
+
isGoingToRetry: boolean;
|
|
153
|
+
pageParam: Maybe<TPageParam>;
|
|
154
|
+
pageParams: Maybe<TPageParam>[];
|
|
155
|
+
hasNextPage: boolean;
|
|
156
|
+
retryNextPageCount: number;
|
|
157
|
+
isGoingToRetryNextPage: boolean;
|
|
158
|
+
} & {
|
|
159
|
+
status: "success";
|
|
160
|
+
isLoading: false;
|
|
161
|
+
isSuccess: true;
|
|
162
|
+
isError: false;
|
|
163
|
+
data: TData;
|
|
164
|
+
response: TResponse;
|
|
165
|
+
responseUpdatedAt: number | undefined;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createBiDirectionQuery = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const create_query_1 = require("./create-query");
|
|
6
|
+
const createBiDirectionQuery = (queryFn, options) => {
|
|
7
|
+
const { getPrevPageParam, getNextPageParam, select, ...restOptions } = options;
|
|
8
|
+
const usePrevPagesQuery = (0, create_query_1.createQuery)((key, state) => queryFn(key, state, 'prev'), {
|
|
9
|
+
defaultDeps: (state) => [
|
|
10
|
+
state.isWaiting,
|
|
11
|
+
state.data,
|
|
12
|
+
state.error,
|
|
13
|
+
state.isWaitingNextPage,
|
|
14
|
+
state.hasNextPage,
|
|
15
|
+
],
|
|
16
|
+
fetchOnMount: false,
|
|
17
|
+
getNextPageParam: getPrevPageParam,
|
|
18
|
+
select: (response, state) => select(response, state, 'prev'),
|
|
19
|
+
...restOptions,
|
|
20
|
+
});
|
|
21
|
+
const useNextPagesQuery = (0, create_query_1.createQuery)(async (key, state) => {
|
|
22
|
+
const isInitialPage = state.pageParam === undefined;
|
|
23
|
+
const pQuery = usePrevPagesQuery.get(key);
|
|
24
|
+
try {
|
|
25
|
+
const response = await queryFn(key, state, 'next');
|
|
26
|
+
if (isInitialPage) {
|
|
27
|
+
const prevPageParam = getPrevPageParam(response, 1, pQuery);
|
|
28
|
+
usePrevPagesQuery.set(key, (prev) => ({
|
|
29
|
+
pageParams: [prevPageParam, ...prev.pageParams.slice(1)],
|
|
30
|
+
hasNextPage: prev.isLoading ? (0, utils_1.hasValue)(prevPageParam) : prev.hasNextPage,
|
|
31
|
+
}));
|
|
32
|
+
if (!pQuery.isLoading)
|
|
33
|
+
pQuery.forceFetch();
|
|
34
|
+
}
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (isInitialPage && !pQuery.isLoading)
|
|
39
|
+
pQuery.reset();
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}, {
|
|
43
|
+
getNextPageParam: getNextPageParam,
|
|
44
|
+
select: (response, state) => select(response, state, 'next'),
|
|
45
|
+
...restOptions,
|
|
46
|
+
});
|
|
47
|
+
const useBiDirectionQuery = (...args) => {
|
|
48
|
+
const pQuery = usePrevPagesQuery(...args);
|
|
49
|
+
const nQuery = useNextPagesQuery(...args);
|
|
50
|
+
return {
|
|
51
|
+
...nQuery,
|
|
52
|
+
data: [...(pQuery.data || []), ...(nQuery.data || [])],
|
|
53
|
+
fetchPrevPage: pQuery.fetchNextPage,
|
|
54
|
+
hasPrevPage: pQuery.hasNextPage,
|
|
55
|
+
isWaitingPrevPage: pQuery.isWaitingNextPage || (pQuery.isLoading && pQuery.isWaiting),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
useBiDirectionQuery.get = () => {
|
|
59
|
+
return {
|
|
60
|
+
prev: usePrevPagesQuery.get(),
|
|
61
|
+
next: useNextPagesQuery.get(),
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
useBiDirectionQuery.setInitialResponse = useNextPagesQuery.setInitialResponse;
|
|
65
|
+
useBiDirectionQuery.reset = () => {
|
|
66
|
+
usePrevPagesQuery.reset();
|
|
67
|
+
useNextPagesQuery.reset();
|
|
68
|
+
};
|
|
69
|
+
useBiDirectionQuery.resetSpecificKey = (key) => {
|
|
70
|
+
usePrevPagesQuery.resetSpecificKey(key);
|
|
71
|
+
useNextPagesQuery.resetSpecificKey(key);
|
|
72
|
+
};
|
|
73
|
+
useBiDirectionQuery.invalidate = useNextPagesQuery.invalidate;
|
|
74
|
+
useBiDirectionQuery.invalidateSpecificKey = useNextPagesQuery.invalidateSpecificKey;
|
|
75
|
+
useBiDirectionQuery.suspend = useNextPagesQuery.suspend;
|
|
76
|
+
return useBiDirectionQuery;
|
|
77
|
+
};
|
|
78
|
+
exports.createBiDirectionQuery = createBiDirectionQuery;
|
package/lib/react/index.d.ts
CHANGED
package/lib/react/index.js
CHANGED
|
@@ -4,5 +4,6 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
tslib_1.__exportStar(require("./create-store"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./create-stores"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./create-query"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./create-bi-direction-query"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./create-mutation"), exports);
|
|
8
9
|
tslib_1.__exportStar(require("./with-context"), exports);
|