@lunora/react 0.0.0 → 1.0.0-alpha.2

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.
Files changed (31) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +150 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/index.d.mts +499 -0
  5. package/dist/index.d.ts +499 -0
  6. package/dist/index.mjs +17 -0
  7. package/dist/packem_shared/Authenticated-DtKgZT2Z.mjs +33 -0
  8. package/dist/packem_shared/CheckoutButton-CVSry8U1.mjs +185 -0
  9. package/dist/packem_shared/LunoraProvider-D38Xp16l.mjs +80 -0
  10. package/dist/packem_shared/cache-CItk3fgN.mjs +75 -0
  11. package/dist/packem_shared/hydratePreloaded-BlFL9FGq.mjs +46 -0
  12. package/dist/packem_shared/lunoraQueryOptions-CsuWzjg1.mjs +16 -0
  13. package/dist/packem_shared/query-key-C5rufkEE.mjs +21 -0
  14. package/dist/packem_shared/query-options.d-D4okOpO8.d.mts +38 -0
  15. package/dist/packem_shared/query-options.d-D4okOpO8.d.ts +38 -0
  16. package/dist/packem_shared/use-paginated-core-CoOfcc-p.mjs +161 -0
  17. package/dist/packem_shared/useAuth-CNUKtOOp.mjs +129 -0
  18. package/dist/packem_shared/useAuthState-BiGhtSCs.mjs +36 -0
  19. package/dist/packem_shared/useConnectionStatus-DRSY9ldm.mjs +30 -0
  20. package/dist/packem_shared/useInfiniteQuery-MH0x4l8h.mjs +97 -0
  21. package/dist/packem_shared/useMutation-CrvMXRsk.mjs +67 -0
  22. package/dist/packem_shared/usePaginatedQuery-D3PTDRGS.mjs +46 -0
  23. package/dist/packem_shared/usePresence-D7jLuxj0.mjs +108 -0
  24. package/dist/packem_shared/useQuery-C5S0W-7K.mjs +41 -0
  25. package/dist/packem_shared/useRateLimit-DTEffQEi.mjs +64 -0
  26. package/dist/packem_shared/useStream-BRY9nemd.mjs +125 -0
  27. package/dist/packem_shared/useSubscription-CHMCjyQg.mjs +139 -0
  28. package/dist/server.d.mts +51 -0
  29. package/dist/server.d.ts +51 -0
  30. package/dist/server.mjs +31 -0
  31. package/package.json +60 -17
@@ -0,0 +1,185 @@
1
+ 'use client';
2
+ import { c } from 'react/compiler-runtime';
3
+ import { useState, useCallback } from 'react';
4
+ import { jsxDEV } from 'react/jsx-dev-runtime';
5
+
6
+ const useCheckout = (trigger) => {
7
+ const [pending, setPending] = useState(false);
8
+ const [error, setError] = useState(void 0);
9
+ const checkout = useCallback(async () => {
10
+ setPending(true);
11
+ setError(void 0);
12
+ try {
13
+ const target = await trigger();
14
+ const {
15
+ url
16
+ } = target;
17
+ globalThis.location.assign(url);
18
+ } catch (error_) {
19
+ const normalized = error_ instanceof Error ? error_ : new Error(String(error_));
20
+ setError(normalized);
21
+ throw normalized;
22
+ } finally {
23
+ setPending(false);
24
+ }
25
+ }, [trigger]);
26
+ return {
27
+ checkout,
28
+ error,
29
+ pending
30
+ };
31
+ };
32
+ const RedirectButton = (t0) => {
33
+ const $ = c(11);
34
+ const {
35
+ "aria-label": ariaLabel,
36
+ children,
37
+ className,
38
+ disabled,
39
+ onError,
40
+ title,
41
+ trigger
42
+ } = t0;
43
+ const {
44
+ checkout,
45
+ pending
46
+ } = useCheckout(trigger);
47
+ let t1;
48
+ if ($[0] !== checkout || $[1] !== onError) {
49
+ t1 = () => {
50
+ checkout().catch((error) => {
51
+ const normalized = error instanceof Error ? error : new Error(String(error));
52
+ onError?.(normalized);
53
+ });
54
+ };
55
+ $[0] = checkout;
56
+ $[1] = onError;
57
+ $[2] = t1;
58
+ } else {
59
+ t1 = $[2];
60
+ }
61
+ const handleClick = t1;
62
+ const t2 = disabled === true || pending;
63
+ let t3;
64
+ if ($[3] !== ariaLabel || $[4] !== children || $[5] !== className || $[6] !== handleClick || $[7] !== pending || $[8] !== t2 || $[9] !== title) {
65
+ t3 = /* @__PURE__ */ jsxDEV("button", {
66
+ "aria-busy": pending,
67
+ "aria-label": ariaLabel,
68
+ className,
69
+ disabled: t2,
70
+ onClick: handleClick,
71
+ title,
72
+ type: "button",
73
+ children
74
+ }, void 0, false);
75
+ $[3] = ariaLabel;
76
+ $[4] = children;
77
+ $[5] = className;
78
+ $[6] = handleClick;
79
+ $[7] = pending;
80
+ $[8] = t2;
81
+ $[9] = title;
82
+ $[10] = t3;
83
+ } else {
84
+ t3 = $[10];
85
+ }
86
+ return t3;
87
+ };
88
+ const CheckoutButton = (t0) => {
89
+ const $ = c(11);
90
+ let onCheckout;
91
+ let rest;
92
+ if ($[0] !== t0) {
93
+ ({
94
+ onCheckout,
95
+ ...rest
96
+ } = t0);
97
+ $[0] = t0;
98
+ $[1] = onCheckout;
99
+ $[2] = rest;
100
+ } else {
101
+ onCheckout = $[1];
102
+ rest = $[2];
103
+ }
104
+ const {
105
+ "aria-label": ariaLabel,
106
+ children,
107
+ className,
108
+ disabled,
109
+ onError,
110
+ title
111
+ } = rest;
112
+ let t1;
113
+ if ($[3] !== ariaLabel || $[4] !== children || $[5] !== className || $[6] !== disabled || $[7] !== onCheckout || $[8] !== onError || $[9] !== title) {
114
+ t1 = /* @__PURE__ */ jsxDEV(RedirectButton, {
115
+ "aria-label": ariaLabel,
116
+ className,
117
+ disabled,
118
+ onError,
119
+ title,
120
+ trigger: onCheckout,
121
+ children
122
+ }, void 0, false);
123
+ $[3] = ariaLabel;
124
+ $[4] = children;
125
+ $[5] = className;
126
+ $[6] = disabled;
127
+ $[7] = onCheckout;
128
+ $[8] = onError;
129
+ $[9] = title;
130
+ $[10] = t1;
131
+ } else {
132
+ t1 = $[10];
133
+ }
134
+ return t1;
135
+ };
136
+ const CustomerPortalButton = (t0) => {
137
+ const $ = c(11);
138
+ let onPortal;
139
+ let rest;
140
+ if ($[0] !== t0) {
141
+ ({
142
+ onPortal,
143
+ ...rest
144
+ } = t0);
145
+ $[0] = t0;
146
+ $[1] = onPortal;
147
+ $[2] = rest;
148
+ } else {
149
+ onPortal = $[1];
150
+ rest = $[2];
151
+ }
152
+ const {
153
+ "aria-label": ariaLabel,
154
+ children,
155
+ className,
156
+ disabled,
157
+ onError,
158
+ title
159
+ } = rest;
160
+ let t1;
161
+ if ($[3] !== ariaLabel || $[4] !== children || $[5] !== className || $[6] !== disabled || $[7] !== onError || $[8] !== onPortal || $[9] !== title) {
162
+ t1 = /* @__PURE__ */ jsxDEV(RedirectButton, {
163
+ "aria-label": ariaLabel,
164
+ className,
165
+ disabled,
166
+ onError,
167
+ title,
168
+ trigger: onPortal,
169
+ children
170
+ }, void 0, false);
171
+ $[3] = ariaLabel;
172
+ $[4] = children;
173
+ $[5] = className;
174
+ $[6] = disabled;
175
+ $[7] = onError;
176
+ $[8] = onPortal;
177
+ $[9] = title;
178
+ $[10] = t1;
179
+ } else {
180
+ t1 = $[10];
181
+ }
182
+ return t1;
183
+ };
184
+
185
+ export { CheckoutButton, CustomerPortalButton, useCheckout };
@@ -0,0 +1,80 @@
1
+ 'use client';
2
+ import { c } from 'react/compiler-runtime';
3
+ import { QueryClientContext, QueryClientProvider, QueryClient } from '@tanstack/react-query';
4
+ import { use, useState, createContext } from 'react';
5
+ import { jsxDEV } from 'react/jsx-dev-runtime';
6
+
7
+ const LunoraContext = /* @__PURE__ */ createContext(null);
8
+ const createDefaultQueryClient = () => new QueryClient({
9
+ defaultOptions: {
10
+ mutations: {
11
+ retry: 0
12
+ },
13
+ queries: {
14
+ gcTime: 5 * 6e4,
15
+ retry: 0,
16
+ staleTime: Number.POSITIVE_INFINITY
17
+ }
18
+ }
19
+ });
20
+ const LunoraProvider = (t0) => {
21
+ const $ = c(9);
22
+ const {
23
+ children,
24
+ client,
25
+ queryClient
26
+ } = t0;
27
+ const parentQueryClient = use(QueryClientContext);
28
+ let t1;
29
+ if ($[0] !== parentQueryClient || $[1] !== queryClient) {
30
+ t1 = () => queryClient ?? parentQueryClient ?? createDefaultQueryClient();
31
+ $[0] = parentQueryClient;
32
+ $[1] = queryClient;
33
+ $[2] = t1;
34
+ } else {
35
+ t1 = $[2];
36
+ }
37
+ const [internalClient] = useState(t1);
38
+ const effectiveClient = queryClient ?? parentQueryClient ?? internalClient;
39
+ if (!effectiveClient) {
40
+ throw new Error("LunoraProvider: failed to resolve a QueryClient");
41
+ }
42
+ let t2;
43
+ if ($[3] !== children || $[4] !== client) {
44
+ t2 = /* @__PURE__ */ jsxDEV(LunoraContext, {
45
+ value: client,
46
+ children
47
+ }, void 0, false);
48
+ $[3] = children;
49
+ $[4] = client;
50
+ $[5] = t2;
51
+ } else {
52
+ t2 = $[5];
53
+ }
54
+ const content = t2;
55
+ if (parentQueryClient === effectiveClient) {
56
+ return content;
57
+ }
58
+ let t3;
59
+ if ($[6] !== content || $[7] !== effectiveClient) {
60
+ t3 = /* @__PURE__ */ jsxDEV(QueryClientProvider, {
61
+ client: effectiveClient,
62
+ children: content
63
+ }, void 0, false);
64
+ $[6] = content;
65
+ $[7] = effectiveClient;
66
+ $[8] = t3;
67
+ } else {
68
+ t3 = $[8];
69
+ }
70
+ return t3;
71
+ };
72
+ const useLunora = () => {
73
+ const client = use(LunoraContext);
74
+ if (!client) {
75
+ throw new Error("useLunora must be used inside <LunoraProvider />");
76
+ }
77
+ return client;
78
+ };
79
+
80
+ export { LunoraProvider, useLunora };
@@ -0,0 +1,75 @@
1
+ import { k as keyHash } from './query-key-C5rufkEE.mjs';
2
+
3
+ class LunoraSubscriptionRegistry {
4
+ constructor(client) {
5
+ this.client = client;
6
+ }
7
+ client;
8
+ entries = /* @__PURE__ */ new Map();
9
+ /**
10
+ * Hash a TanStack `queryKey` to the internal registry index. Exposed so a
11
+ * hook can look up the registry without re-implementing the hash.
12
+ */
13
+ // eslint-disable-next-line class-methods-use-this -- instance method by design: callers reach the hash through a registry handle rather than importing the module-level helper.
14
+ keyOf(queryKey) {
15
+ return keyHash(queryKey);
16
+ }
17
+ /**
18
+ * Attach a consumer to the live subscription for `queryKey`. The first
19
+ * attach opens the underlying WS subscription; subsequent attaches reuse
20
+ * it (refcount-bumped). Returns the detach function — call it exactly once
21
+ * per attach.
22
+ */
23
+ attach(queryClient, queryKey, function_, args, shardKey, options = {}) {
24
+ const key = keyHash(queryKey);
25
+ let entry = this.entries.get(key);
26
+ if (!entry) {
27
+ entry = {
28
+ pollTimer: void 0,
29
+ refCount: 0,
30
+ unsubscribe: void 0
31
+ };
32
+ this.entries.set(key, entry);
33
+ try {
34
+ entry.unsubscribe = this.client.subscribe(function_, args, (value) => {
35
+ queryClient.setQueryData(queryKey, value);
36
+ }, {
37
+ shardKey
38
+ });
39
+ } catch {
40
+ entry.pollTimer = setInterval(() => {
41
+ queryClient.invalidateQueries({
42
+ queryKey
43
+ }).catch(() => {
44
+ });
45
+ }, options.pollIntervalMs ?? 5e3);
46
+ }
47
+ }
48
+ entry.refCount += 1;
49
+ return () => {
50
+ const current = this.entries.get(key);
51
+ if (!current) {
52
+ return;
53
+ }
54
+ current.refCount -= 1;
55
+ if (current.refCount <= 0) {
56
+ current.unsubscribe?.();
57
+ if (current.pollTimer) {
58
+ clearInterval(current.pollTimer);
59
+ }
60
+ this.entries.delete(key);
61
+ }
62
+ };
63
+ }
64
+ }
65
+ const registryByClient = /* @__PURE__ */ new WeakMap();
66
+ const getSubscriptionRegistry = (client) => {
67
+ let registry = registryByClient.get(client);
68
+ if (!registry) {
69
+ registry = new LunoraSubscriptionRegistry(client);
70
+ registryByClient.set(client, registry);
71
+ }
72
+ return registry;
73
+ };
74
+
75
+ export { getSubscriptionRegistry as g };
@@ -0,0 +1,46 @@
1
+ 'use client';
2
+ import { useQueryClient, useQuery } from '@tanstack/react-query';
3
+ import { useMemo, useEffect } from 'react';
4
+ import { g as getSubscriptionRegistry } from './cache-CItk3fgN.mjs';
5
+ import { useLunora } from './LunoraProvider-D38Xp16l.mjs';
6
+ import { l as lunoraQueryKey, s as serializeQueryKey } from './query-key-C5rufkEE.mjs';
7
+
8
+ const usePreloadedQuery = function(preloaded) {
9
+ const client = useLunora();
10
+ const queryClient = useQueryClient();
11
+ const {
12
+ args,
13
+ functionPath,
14
+ shardKey,
15
+ value
16
+ } = preloaded;
17
+ const functionRef = useMemo(() => {
18
+ return {
19
+ __lunoraRef: functionPath
20
+ };
21
+ }, [functionPath]);
22
+ const queryKey = useMemo(() => lunoraQueryKey(functionRef, args, shardKey), [functionRef.__lunoraRef, JSON.stringify(args), shardKey]);
23
+ const {
24
+ data
25
+ } = useQuery({
26
+ // Seed the cache with the server value so the first paint doesn't
27
+ // re-fetch. TanStack treats `initialData` as fresh — the WS push from
28
+ // the registry is what supplies subsequent updates.
29
+ initialData: value,
30
+ queryFn: () => client.query(functionRef, args, {
31
+ shardKey
32
+ }),
33
+ queryKey,
34
+ staleTime: Number.POSITIVE_INFINITY
35
+ });
36
+ useEffect(() => {
37
+ const registry = getSubscriptionRegistry(client);
38
+ return registry.attach(queryClient, queryKey, functionRef, args, shardKey);
39
+ }, [client, queryClient, serializeQueryKey(queryKey)]);
40
+ return data ?? value;
41
+ };
42
+ const hydratePreloaded = function(preloaded) {
43
+ return usePreloadedQuery(preloaded);
44
+ };
45
+
46
+ export { usePreloadedQuery as default, hydratePreloaded };
@@ -0,0 +1,16 @@
1
+ import { l as lunoraQueryKey } from './query-key-C5rufkEE.mjs';
2
+
3
+ const lunoraQueryOptions = (client, function_, args, options = {}) => {
4
+ const argsRecord = args ?? {};
5
+ return {
6
+ queryFn: () => client.query(function_, argsRecord, {
7
+ shardKey: options.shardKey
8
+ }),
9
+ queryKey: lunoraQueryKey(function_, argsRecord, options.shardKey),
10
+ // Lunora is push-driven: the value never goes stale on a timer. For
11
+ // reactivity use `useQuery`; this adapter is a one-shot/suspense read.
12
+ staleTime: Number.POSITIVE_INFINITY
13
+ };
14
+ };
15
+
16
+ export { lunoraQueryOptions };
@@ -0,0 +1,21 @@
1
+ const keyHash = (queryKey) => JSON.stringify(queryKey);
2
+ const stableStringify = (value) => {
3
+ if (value === null || typeof value !== "object") {
4
+ return JSON.stringify(value);
5
+ }
6
+ if (Array.isArray(value)) {
7
+ return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
8
+ }
9
+ const entries = Object.entries(value).toSorted(([a], [b]) => a.localeCompare(b));
10
+ return `{${entries.map(([key, value_]) => `${JSON.stringify(key)}:${stableStringify(value_)}`).join(",")}}`;
11
+ };
12
+ const lunoraQueryKey = (function_, args, shardKey) => [
13
+ "lunora",
14
+ function_.__lunoraRef,
15
+ args,
16
+ // eslint-disable-next-line unicorn/no-null -- this literal is part of the JSON-serialized query key TanStack hashes for dedup; `null` keeps a stable, distinct slot from an absent shardKey across renders.
17
+ shardKey ?? null
18
+ ];
19
+ const serializeQueryKey = (queryKey) => keyHash(queryKey);
20
+
21
+ export { stableStringify as a, keyHash as k, lunoraQueryKey as l, serializeQueryKey as s };
@@ -0,0 +1,38 @@
1
+ import { FunctionReference, ReturnOf, LunoraClient, ArgsOf } from '@lunora/client';
2
+ import { QueryKey } from '@tanstack/react-query';
3
+ /**
4
+ * Pure, transport-free adapter between a Lunora function reference and a
5
+ * TanStack Query options object. No `"use client"` directive and only a
6
+ * type-level `@lunora/client` import, so it is safe to use on either side of an
7
+ * RSC boundary (the runtime transport is the `LunoraClient` you pass in).
8
+ *
9
+ * Use it when you want to drive a Lunora query through TanStack's own hooks —
10
+ * `useSuspenseQuery`, `useQueries`, or the server-side
11
+ * `queryClient.ensureQueryData` / `prefetchQuery` — rather than the first-class
12
+ * `useQuery` from `@lunora/react`.
13
+ */
14
+ /** Shape returned by `lunoraQueryOptions`, spread into a TanStack hook. */
15
+ interface LunoraQueryOptions<F extends FunctionReference> {
16
+ queryFn: () => Promise<ReturnOf<F>>;
17
+ queryKey: QueryKey;
18
+ staleTime: number;
19
+ }
20
+ /**
21
+ * Build a TanStack Query options object for a Lunora query, keyed identically
22
+ * to the first-class hooks (see `lunoraQueryKey`) so a value fetched through
23
+ * this adapter shares cache identity with anything `useQuery` /
24
+ * `prefetchQuery` reads or writes for the same `(fn, args, shardKey)` triple.
25
+ *
26
+ * ```ts
27
+ * const { data } = useSuspenseQuery(lunoraQueryOptions(client, api.posts.list, {}));
28
+ * ```
29
+ *
30
+ * This is a one-shot fetch: it resolves once and `staleTime` is infinite, so
31
+ * TanStack never refetches on its own. It does not open a WebSocket — for live
32
+ * updates that re-render on every server push, use `useQuery` from
33
+ * `@lunora/react`, which attaches a shared subscription to the same cache key.
34
+ */
35
+ declare const lunoraQueryOptions: <F extends FunctionReference>(client: LunoraClient, function_: F, args: ArgsOf<F>, options?: {
36
+ shardKey?: string;
37
+ }) => LunoraQueryOptions<F>;
38
+ export { LunoraQueryOptions as L, lunoraQueryOptions as l };
@@ -0,0 +1,38 @@
1
+ import { FunctionReference, ReturnOf, LunoraClient, ArgsOf } from '@lunora/client';
2
+ import { QueryKey } from '@tanstack/react-query';
3
+ /**
4
+ * Pure, transport-free adapter between a Lunora function reference and a
5
+ * TanStack Query options object. No `"use client"` directive and only a
6
+ * type-level `@lunora/client` import, so it is safe to use on either side of an
7
+ * RSC boundary (the runtime transport is the `LunoraClient` you pass in).
8
+ *
9
+ * Use it when you want to drive a Lunora query through TanStack's own hooks —
10
+ * `useSuspenseQuery`, `useQueries`, or the server-side
11
+ * `queryClient.ensureQueryData` / `prefetchQuery` — rather than the first-class
12
+ * `useQuery` from `@lunora/react`.
13
+ */
14
+ /** Shape returned by `lunoraQueryOptions`, spread into a TanStack hook. */
15
+ interface LunoraQueryOptions<F extends FunctionReference> {
16
+ queryFn: () => Promise<ReturnOf<F>>;
17
+ queryKey: QueryKey;
18
+ staleTime: number;
19
+ }
20
+ /**
21
+ * Build a TanStack Query options object for a Lunora query, keyed identically
22
+ * to the first-class hooks (see `lunoraQueryKey`) so a value fetched through
23
+ * this adapter shares cache identity with anything `useQuery` /
24
+ * `prefetchQuery` reads or writes for the same `(fn, args, shardKey)` triple.
25
+ *
26
+ * ```ts
27
+ * const { data } = useSuspenseQuery(lunoraQueryOptions(client, api.posts.list, {}));
28
+ * ```
29
+ *
30
+ * This is a one-shot fetch: it resolves once and `staleTime` is infinite, so
31
+ * TanStack never refetches on its own. It does not open a WebSocket — for live
32
+ * updates that re-render on every server push, use `useQuery` from
33
+ * `@lunora/react`, which attaches a shared subscription to the same cache key.
34
+ */
35
+ declare const lunoraQueryOptions: <F extends FunctionReference>(client: LunoraClient, function_: F, args: ArgsOf<F>, options?: {
36
+ shardKey?: string;
37
+ }) => LunoraQueryOptions<F>;
38
+ export { LunoraQueryOptions as L, lunoraQueryOptions as l };
@@ -0,0 +1,161 @@
1
+ 'use client';
2
+ import { initialPages, rebalance, derivePaginationStatus, applyLoadMore } from '@lunora/client/pagination';
3
+ import { useQueryClient } from '@tanstack/react-query';
4
+ import { useRef, useReducer, useState, useEffect, useCallback } from 'react';
5
+ import { g as getSubscriptionRegistry } from './cache-CItk3fgN.mjs';
6
+ import { useLunora } from './LunoraProvider-D38Xp16l.mjs';
7
+ import { s as serializeQueryKey, l as lunoraQueryKey } from './query-key-C5rufkEE.mjs';
8
+
9
+ const useLazyRef = function(create) {
10
+ const reference = useRef(void 0);
11
+ reference.current ??= create();
12
+ return reference;
13
+ };
14
+
15
+ const usePaginatedCore = function(function_, args, options) {
16
+ const client = useLunora();
17
+ const queryClient = useQueryClient();
18
+ const {
19
+ initialNumItems,
20
+ shardKey
21
+ } = options;
22
+ const skipped = args === "skip";
23
+ const baseArgs = skipped ? {} : args;
24
+ const baseArgsKey = JSON.stringify(baseArgs);
25
+ const [, forceRender] = useReducer((tick) => tick + 1, 0);
26
+ const [pages, setPages] = useState(() => initialPages(initialNumItems));
27
+ const resetKey = `${function_.__lunoraRef}::${baseArgsKey}::${String(initialNumItems)}::${shardKey ?? ""}`;
28
+ const resetKeyRef = useRef(resetKey);
29
+ if (resetKeyRef.current !== resetKey) {
30
+ resetKeyRef.current = resetKey;
31
+ setPages(initialPages(initialNumItems));
32
+ }
33
+ const pageEntries = pages.map((page) => {
34
+ const pageArgs = {
35
+ ...baseArgs,
36
+ paginationOpts: {
37
+ cursor: page.lower,
38
+ endCursor: page.upper,
39
+ numItems: page.numItems
40
+ }
41
+ };
42
+ const key = lunoraQueryKey(function_, pageArgs, shardKey);
43
+ return {
44
+ args: pageArgs,
45
+ key
46
+ };
47
+ });
48
+ const pageKeysHash = pageEntries.map(({
49
+ key: key_0
50
+ }) => serializeQueryKey(key_0)).join("|");
51
+ const desiredRef = useRef({
52
+ entries: [],
53
+ fn: function_,
54
+ shardKey
55
+ });
56
+ useEffect(() => {
57
+ desiredRef.current = {
58
+ entries: pageEntries,
59
+ fn: function_,
60
+ shardKey
61
+ };
62
+ });
63
+ const detachesRef = useLazyRef(() => /* @__PURE__ */ new Map());
64
+ const detachClientRef = useRef(client);
65
+ useEffect(() => {
66
+ const detaches = detachesRef.current;
67
+ if (detachClientRef.current !== client) {
68
+ for (const detach of detaches.values()) {
69
+ detach();
70
+ }
71
+ detaches.clear();
72
+ detachClientRef.current = client;
73
+ }
74
+ if (skipped) {
75
+ for (const detach_0 of detaches.values()) {
76
+ detach_0();
77
+ }
78
+ detaches.clear();
79
+ return;
80
+ }
81
+ const desired = desiredRef.current;
82
+ const registry = getSubscriptionRegistry(client);
83
+ const wanted = new Set(desired.entries.map(({
84
+ key: key_1
85
+ }) => serializeQueryKey(key_1)));
86
+ for (const [hash, detach_1] of detaches) {
87
+ if (!wanted.has(hash)) {
88
+ detach_1();
89
+ detaches.delete(hash);
90
+ }
91
+ }
92
+ for (const entry of desired.entries) {
93
+ const hash_0 = serializeQueryKey(entry.key);
94
+ if (detaches.has(hash_0)) {
95
+ continue;
96
+ }
97
+ const initialFetch = queryClient.fetchQuery({
98
+ queryFn: () => client.query(desired.fn, entry.args, {
99
+ shardKey: desired.shardKey
100
+ }),
101
+ queryKey: entry.key,
102
+ staleTime: 0
103
+ });
104
+ initialFetch.catch(() => {
105
+ });
106
+ detaches.set(hash_0, registry.attach(queryClient, entry.key, desired.fn, entry.args, desired.shardKey));
107
+ }
108
+ }, [client, queryClient, pageKeysHash, skipped]);
109
+ useEffect(() => () => {
110
+ for (const detach_2 of detachesRef.current.values()) {
111
+ detach_2();
112
+ }
113
+ detachesRef.current.clear();
114
+ }, []);
115
+ useEffect(() => {
116
+ const cache = queryClient.getQueryCache();
117
+ const unsubscribe = cache.subscribe((event) => {
118
+ if (event.type !== "updated") {
119
+ return;
120
+ }
121
+ const hash_1 = serializeQueryKey(event.query.queryKey);
122
+ if (pageEntries.some(({
123
+ key: key_2
124
+ }) => serializeQueryKey(key_2) === hash_1)) {
125
+ forceRender();
126
+ }
127
+ });
128
+ return unsubscribe;
129
+ }, [queryClient, pageKeysHash]);
130
+ const pageResults = skipped ? [] : pageEntries.map(({
131
+ key: key_3
132
+ }) => queryClient.getQueryData(key_3));
133
+ useEffect(() => {
134
+ if (skipped) {
135
+ return;
136
+ }
137
+ const next = rebalance(pages, pageResults);
138
+ if (next) {
139
+ setPages(next);
140
+ }
141
+ });
142
+ const {
143
+ nextCursor,
144
+ status
145
+ } = derivePaginationStatus(skipped, pageResults);
146
+ const nextCursorRef = useRef(void 0);
147
+ useEffect(() => {
148
+ nextCursorRef.current = status === "CanLoadMore" ? nextCursor : void 0;
149
+ });
150
+ const loadMore = useCallback((numberItems) => {
151
+ const cursor = nextCursorRef.current;
152
+ setPages((current) => applyLoadMore(current, cursor, numberItems) ?? current);
153
+ }, []);
154
+ return {
155
+ loadMore,
156
+ pageResults,
157
+ status
158
+ };
159
+ };
160
+
161
+ export { usePaginatedCore as u };