@instantdb/react-common 0.22.164 → 0.22.165
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/dist/commonjs/InstantReactAbstractDatabase.d.ts +25 -0
- package/dist/commonjs/InstantReactAbstractDatabase.d.ts.map +1 -1
- package/dist/commonjs/InstantReactAbstractDatabase.js +32 -0
- package/dist/commonjs/InstantReactAbstractDatabase.js.map +1 -1
- package/dist/commonjs/useInfiniteQuerySubscription.d.ts +28 -0
- package/dist/commonjs/useInfiniteQuerySubscription.d.ts.map +1 -0
- package/dist/commonjs/useInfiniteQuerySubscription.js +58 -0
- package/dist/commonjs/useInfiniteQuerySubscription.js.map +1 -0
- package/dist/esm/InstantReactAbstractDatabase.d.ts +25 -0
- package/dist/esm/InstantReactAbstractDatabase.d.ts.map +1 -1
- package/dist/esm/InstantReactAbstractDatabase.js +32 -0
- package/dist/esm/InstantReactAbstractDatabase.js.map +1 -1
- package/dist/esm/useInfiniteQuerySubscription.d.ts +28 -0
- package/dist/esm/useInfiniteQuerySubscription.d.ts.map +1 -0
- package/dist/esm/useInfiniteQuerySubscription.js +55 -0
- package/dist/esm/useInfiniteQuerySubscription.js.map +1 -0
- package/dist/standalone/index.js +2402 -2038
- package/dist/standalone/index.umd.cjs +12 -12
- package/package.json +3 -3
- package/src/InstantReactAbstractDatabase.tsx +39 -0
- package/src/useInfiniteQuerySubscription.ts +127 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Auth, Storage, type AuthState, type User, type ConnectionStatus, type TransactionChunk, type RoomSchemaShape, type InstaQLOptions, type InstantConfig, type PageInfoResponse, InstantCoreDatabase, InstaQLLifecycleState, InstaQLResponse, RoomsOf, InstantSchemaDef, IInstantDatabase, ValidQuery, Streams } from '@instantdb/core';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
+
import { type InfiniteQueryResult } from './useInfiniteQuerySubscription.ts';
|
|
3
4
|
import { InstantReactRoom } from './InstantReactRoom.ts';
|
|
4
5
|
export default abstract class InstantReactAbstractDatabase<Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean = false, Config extends InstantConfig<Schema, boolean> = InstantConfig<Schema, UseDates>, Rooms extends RoomSchemaShape = RoomsOf<Schema>> implements IInstantDatabase<Schema> {
|
|
5
6
|
tx: import("@instantdb/core").TxChunk<Schema>;
|
|
@@ -215,6 +216,30 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
215
216
|
data: InstaQLResponse<Schema, Q, UseDates>;
|
|
216
217
|
pageInfo: PageInfoResponse<Q>;
|
|
217
218
|
}>;
|
|
219
|
+
/**
|
|
220
|
+
* Subscribe to a query and incrementally load more items
|
|
221
|
+
*
|
|
222
|
+
* Only one top level namespace in the query is allowed.
|
|
223
|
+
*
|
|
224
|
+
* Changing the query or options while the subscription is active will
|
|
225
|
+
* reset the subscription and start over with new data.
|
|
226
|
+
* @example
|
|
227
|
+
* const {
|
|
228
|
+
* data,
|
|
229
|
+
* loadNextPage,
|
|
230
|
+
* canLoadNextPage,
|
|
231
|
+
* } = db.useInfiniteQuery({
|
|
232
|
+
* posts: {
|
|
233
|
+
* $: {
|
|
234
|
+
* limit: 20, // Load 20 posts at a time
|
|
235
|
+
* order: {
|
|
236
|
+
* createdAt: 'desc',
|
|
237
|
+
* },
|
|
238
|
+
* },
|
|
239
|
+
* },
|
|
240
|
+
* });
|
|
241
|
+
*/
|
|
242
|
+
useInfiniteQuery: <Q extends ValidQuery<Q, Schema>>(query: Q, opts?: InstaQLOptions) => InfiniteQueryResult<Schema, Q, UseDates>;
|
|
218
243
|
/**
|
|
219
244
|
* Only render children if the user is signed in.
|
|
220
245
|
* @see https://instantdb.com/docs/auth
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InstantReactAbstractDatabase.d.ts","sourceRoot":"","sources":["../../src/InstantReactAbstractDatabase.tsx"],"names":[],"mappings":"AACA,OAAO,EAEL,IAAI,EACJ,OAAO,EAEP,KA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AAC;QAClB,QAAQ,EAAE,SAAS,CAAC;KACrB,CAAC,CAIA;CACH"}
|
|
@@ -5,6 +5,7 @@ const jsx_runtime_1 = require("react/jsx-runtime");
|
|
|
5
5
|
const core_1 = require("@instantdb/core");
|
|
6
6
|
const react_1 = require("react");
|
|
7
7
|
const useQuery_ts_1 = require("./useQuery.js");
|
|
8
|
+
const useInfiniteQuerySubscription_ts_1 = require("./useInfiniteQuerySubscription.js");
|
|
8
9
|
const InstantReactRoom_ts_1 = require("./InstantReactRoom.js");
|
|
9
10
|
const defaultAuthState = {
|
|
10
11
|
isLoading: true,
|
|
@@ -291,6 +292,37 @@ class InstantReactAbstractDatabase {
|
|
|
291
292
|
queryOnce = (query, opts) => {
|
|
292
293
|
return this.core.queryOnce(query, opts);
|
|
293
294
|
};
|
|
295
|
+
/**
|
|
296
|
+
* Subscribe to a query and incrementally load more items
|
|
297
|
+
*
|
|
298
|
+
* Only one top level namespace in the query is allowed.
|
|
299
|
+
*
|
|
300
|
+
* Changing the query or options while the subscription is active will
|
|
301
|
+
* reset the subscription and start over with new data.
|
|
302
|
+
* @example
|
|
303
|
+
* const {
|
|
304
|
+
* data,
|
|
305
|
+
* loadNextPage,
|
|
306
|
+
* canLoadNextPage,
|
|
307
|
+
* } = db.useInfiniteQuery({
|
|
308
|
+
* posts: {
|
|
309
|
+
* $: {
|
|
310
|
+
* limit: 20, // Load 20 posts at a time
|
|
311
|
+
* order: {
|
|
312
|
+
* createdAt: 'desc',
|
|
313
|
+
* },
|
|
314
|
+
* },
|
|
315
|
+
* },
|
|
316
|
+
* });
|
|
317
|
+
*/
|
|
318
|
+
useInfiniteQuery = (query, opts) => {
|
|
319
|
+
const result = (0, useInfiniteQuerySubscription_ts_1.useInfiniteQuerySubscription)({
|
|
320
|
+
core: this.core,
|
|
321
|
+
query: query,
|
|
322
|
+
opts,
|
|
323
|
+
});
|
|
324
|
+
return result;
|
|
325
|
+
};
|
|
294
326
|
/**
|
|
295
327
|
* Only render children if the user is signed in.
|
|
296
328
|
* @see https://instantdb.com/docs/auth
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InstantReactAbstractDatabase.js","sourceRoot":"","sources":["../../src/InstantReactAbstractDatabase.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;AACb,0CAuByB;AACzB,iCAOe;AACf,+CAAiD;AACjD,+DAAgE;AAEhE,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,SAAS;CACjB,CAAC;AAEF,MAA8B,4BAA4B;IAWjD,EAAE,GAAG,IAAA,aAAM,GAAU,CAAC;IAEtB,IAAI,CAAO;IACX,OAAO,CAAU;IACjB,OAAO,CAAU;IACjB,IAAI,CAAwC;IAEnD,qCAAqC;IAC9B,KAAK,CAAwC;IAEpD,MAAM,CAAC,KAAK,CAAO;IACnB,MAAM,CAAC,eAAe,CAAO;IAC7B,MAAM,CAAC,eAAe,CAAO;IAE7B,YACE,MAEC,EACD,QAAoC;QAEpC,IAAI,CAAC,IAAI,GAAG,IAAA,WAAS,EACnB,MAAM;QACN,6DAA6D;QAC7D,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK;QACtC,6DAA6D;QAC7D,IAAI,CAAC,WAAW,CAAC,eAAe,EAChC,QAAQ;QACR,6DAA6D;QAC7D,IAAI,CAAC,WAAW,CAAC,eAAe,CACjC,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IACnC,CAAC;IAED;;;;;;;;OAQG;IACH,UAAU,GAAG,CAAC,IAAY,EAAmB,EAAE;QAC7C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;;;;;;;OASG;IACH,SAAS,GAEJ,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAC3D,OAAO,2DAAG,QAAQ,GAAI,CAAC;IACzB,CAAC,CAAC;CACH;AAlYD,+CAkYC","sourcesContent":["'use client';\nimport {\n // types\n Auth,\n Storage,\n txInit,\n type AuthState,\n type User,\n type ConnectionStatus,\n type TransactionChunk,\n type RoomSchemaShape,\n type InstaQLOptions,\n type InstantConfig,\n type PageInfoResponse,\n InstantCoreDatabase,\n init as core_init,\n InstaQLLifecycleState,\n InstaQLResponse,\n RoomsOf,\n InstantSchemaDef,\n IInstantDatabase,\n InstantError,\n ValidQuery,\n Streams,\n} from '@instantdb/core';\nimport {\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\nimport { useQueryInternal } from './useQuery.ts';\nimport { InstantReactRoom, rooms } from './InstantReactRoom.ts';\n\nconst defaultAuthState = {\n isLoading: true,\n user: undefined,\n error: undefined,\n};\n\nexport default abstract class InstantReactAbstractDatabase<\n // need to pull this schema out to another generic for query params, not sure why\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n Config extends InstantConfig<Schema, boolean> = InstantConfig<\n Schema,\n UseDates\n >,\n Rooms extends RoomSchemaShape = RoomsOf<Schema>,\n> implements IInstantDatabase<Schema>\n{\n public tx = txInit<Schema>();\n\n public auth: Auth;\n public storage: Storage;\n public streams: Streams;\n public core: InstantCoreDatabase<Schema, UseDates>;\n\n /** @deprecated use `core` instead */\n public _core: InstantCoreDatabase<Schema, UseDates>;\n\n static Store?: any;\n static NetworkListener?: any;\n static EventSourceImpl?: any;\n\n constructor(\n config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {\n useDateObjects?: UseDates;\n },\n versions?: { [key: string]: string },\n ) {\n this.core = core_init<Schema, UseDates>(\n config,\n // @ts-expect-error because TS can't resolve subclass statics\n config.Store || this.constructor.Store,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.NetworkListener,\n versions,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.EventSourceImpl,\n );\n this._core = this.core;\n this.auth = this.core.auth;\n this.storage = this.core.storage;\n this.streams = this.core.streams;\n }\n\n /**\n * Returns a unique ID for a given `name`. It's stored in local storage,\n * so you will get the same ID across sessions.\n *\n * This is useful for generating IDs that could identify a local device or user.\n *\n * @example\n * const deviceId = await db.getLocalId('device');\n */\n getLocalId = (name: string): Promise<string> => {\n return this.core.getLocalId(name);\n };\n\n /**\n * A hook that returns a unique ID for a given `name`. localIds are\n * stored in local storage, so you will get the same ID across sessions.\n *\n * Initially returns `null`, and then loads the localId.\n *\n * @example\n * const deviceId = db.useLocalId('device');\n * if (!deviceId) return null; // loading\n * console.log('Device ID:', deviceId)\n */\n useLocalId = (name: string): string | null => {\n const [localId, setLocalId] = useState<string | null>(null);\n\n useEffect(() => {\n let mounted = true;\n const f = async () => {\n const id = await this.getLocalId(name);\n if (!mounted) return;\n setLocalId(id);\n };\n f();\n return;\n }, [name]);\n\n return localId;\n };\n\n /**\n * Obtain a handle to a room, which allows you to listen to topics and presence data\n *\n * If you don't provide a `type` or `id`, Instant will default to `_defaultRoomType` and `_defaultRoomId`\n * as the room type and id, respectively.\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n */\n room<RoomType extends string & keyof Rooms>(\n type: RoomType = '_defaultRoomType' as RoomType,\n id: string = '_defaultRoomId',\n ) {\n return new InstantReactRoom<Schema, Rooms, RoomType>(this.core, type, id);\n }\n\n /**\n * Hooks for working with rooms\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n * const publish = db.rooms.usePublishTopic(room, 'emoji');\n * // ...\n */\n rooms = rooms;\n\n /**\n * Use this to write data! You can create, update, delete, and link objects\n *\n * @see https://instantdb.com/docs/instaml\n *\n * @example\n * // Create a new object in the `goals` namespace\n * const goalId = id();\n * db.transact(db.tx.goals[goalId].update({title: \"Get fit\"}))\n *\n * // Update the title\n * db.transact(db.tx.goals[goalId].update({title: \"Get super fit\"}))\n *\n * // Delete it\n * db.transact(db.tx.goals[goalId].delete())\n *\n * // Or create an association:\n * todoId = id();\n * db.transact([\n * db.tx.todos[todoId].update({ title: 'Go on a run' }),\n * db.tx.goals[goalId].link({todos: todoId}),\n * ])\n */\n transact = (\n chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],\n ) => {\n return this.core.transact(chunks);\n };\n\n /**\n * Use this to query your data!\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * // listen to all goals\n * const { isLoading, error, data } = db.useQuery({ goals: {} });\n *\n * // goals where the title is \"Get Fit\"\n * const { isLoading, error, data } = db.useQuery({\n * goals: { $: { where: { title: 'Get Fit' } } },\n * });\n *\n * // all goals, _alongside_ their todos\n * const { isLoading, error, data } = db.useQuery({\n * goals: { todos: {} },\n * });\n *\n * // skip if `user` is not logged in\n * const { isLoading, error, data } = db.useQuery(\n * auth.user ? { goals: {} } : null,\n * );\n */\n useQuery = <Q extends ValidQuery<Q, Schema>>(\n query: null | Q,\n opts?: InstaQLOptions,\n ): InstaQLLifecycleState<Schema, Q, UseDates> => {\n return useQueryInternal<Q, Schema, UseDates>(this.core, query, opts).state;\n };\n\n /**\n * Listen for the logged in state. This is useful\n * for deciding when to show a login screen.\n *\n * Check out the docs for an example `Login` component too!\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function App() {\n * const { isLoading, user, error } = db.useAuth()\n * if (isLoading) {\n * return <div>Loading...</div>\n * }\n * if (error) {\n * return <div>Uh oh! {error.message}</div>\n * }\n * if (user) {\n * return <Main user={user} />\n * }\n * return <Login />\n * }\n *\n */\n useAuth = (): AuthState => {\n return this._useAuth();\n };\n\n protected _useAuth(): AuthState {\n // We use a ref to store the result of the query.\n // This is becuase `useSyncExternalStore` uses `Object.is`\n // to compare the previous and next state.\n // If we don't use a ref, the state will always be considered different, so\n // the component will always re-render.\n const resultCacheRef = useRef<AuthState>(\n this.core._reactor._currentUserCached,\n );\n\n // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe\n // if `subscribe` changes, so we use `useCallback` to memoize the function.\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeAuth((auth) => {\n resultCacheRef.current = { isLoading: false, ...auth };\n cb();\n });\n\n return unsubscribe;\n }, []);\n\n const state = useSyncExternalStore<AuthState>(\n subscribe,\n () => resultCacheRef.current,\n () => defaultAuthState,\n );\n return state;\n }\n\n /**\n * Subscribe to the currently logged in user.\n * If the user is not logged in, this hook with throw an Error.\n * You will want to protect any calls of this hook with a\n * <db.SignedIn> component, or your own logic based on db.useAuth()\n *\n * @see https://instantdb.com/docs/auth\n * @throws Error indicating user not signed in\n * @example\n * function UserDisplay() {\n * const user = db.useUser()\n * return <div>Logged in as: {user.email}</div>\n * }\n *\n * <db.SignedIn>\n * <UserDisplay />\n * </db.SignedIn>\n *\n */\n useUser = (): User => {\n const { user } = this.useAuth();\n if (!user) {\n throw new InstantError(\n 'useUser must be used within an auth-protected route',\n );\n }\n return user;\n };\n\n /**\n * One time query for the logged in state. This is useful\n * for scenarios where you want to know the current auth\n * state without subscribing to changes.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * const user = await db.getAuth();\n * console.log('logged in as', user.email)\n */\n getAuth(): Promise<User | null> {\n return this.core.getAuth();\n }\n\n /**\n * Listen for connection status changes to Instant. Use this for things like\n * showing connection state to users\n *\n * @see https://www.instantdb.com/docs/patterns#connection-status\n * @example\n * function App() {\n * const status = db.useConnectionStatus()\n * const connectionState =\n * status === 'connecting' || status === 'opened'\n * ? 'authenticating'\n * : status === 'authenticated'\n * ? 'connected'\n * : status === 'closed'\n * ? 'closed'\n * : status === 'errored'\n * ? 'errored'\n * : 'unexpected state';\n *\n * return <div>Connection state: {connectionState}</div>\n * }\n */\n useConnectionStatus = (): ConnectionStatus => {\n const statusRef = useRef<ConnectionStatus>(\n this.core._reactor.status as ConnectionStatus,\n );\n\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeConnectionStatus((newStatus) => {\n if (newStatus !== statusRef.current) {\n statusRef.current = newStatus;\n cb();\n }\n });\n\n return unsubscribe;\n }, []);\n\n const status = useSyncExternalStore<ConnectionStatus>(\n subscribe,\n () => statusRef.current,\n // For SSR, always return 'connecting' as the initial state\n () => 'connecting',\n );\n\n return status;\n };\n\n /**\n * Use this for one-off queries.\n * Returns local data if available, otherwise fetches from the server.\n * Because we want to avoid stale data, this method will throw an error\n * if the user is offline or there is no active connection to the server.\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n *\n * const resp = await db.queryOnce({ goals: {} });\n * console.log(resp.data.goals)\n */\n queryOnce = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): Promise<{\n data: InstaQLResponse<Schema, Q, UseDates>;\n pageInfo: PageInfoResponse<Q>;\n }> => {\n return this.core.queryOnce(query, opts);\n };\n\n /**\n * Only render children if the user is signed in.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedIn>\n * <MyComponent />\n * </db.SignedIn>\n *\n */\n SignedIn: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || !auth.user) return null;\n\n return <>{children}</>;\n };\n\n /**\n * Only render children if the user is signed out.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedOut>\n * <MyComponent />\n * </db.SignedOut>\n *\n */\n SignedOut: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || auth.user) return null;\n return <>{children}</>;\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"InstantReactAbstractDatabase.js","sourceRoot":"","sources":["../../src/InstantReactAbstractDatabase.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;AACb,0CAuByB;AACzB,iCAOe;AACf,+CAAiD;AACjD,uFAG2C;AAC3C,+DAAgE;AAEhE,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,SAAS;CACjB,CAAC;AAEF,MAA8B,4BAA4B;IAWjD,EAAE,GAAG,IAAA,aAAM,GAAU,CAAC;IAEtB,IAAI,CAAO;IACX,OAAO,CAAU;IACjB,OAAO,CAAU;IACjB,IAAI,CAAwC;IAEnD,qCAAqC;IAC9B,KAAK,CAAwC;IAEpD,MAAM,CAAC,KAAK,CAAO;IACnB,MAAM,CAAC,eAAe,CAAO;IAC7B,MAAM,CAAC,eAAe,CAAO;IAE7B,YACE,MAEC,EACD,QAAoC;QAEpC,IAAI,CAAC,IAAI,GAAG,IAAA,WAAS,EACnB,MAAM;QACN,6DAA6D;QAC7D,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK;QACtC,6DAA6D;QAC7D,IAAI,CAAC,WAAW,CAAC,eAAe,EAChC,QAAQ;QACR,6DAA6D;QAC7D,IAAI,CAAC,WAAW,CAAC,eAAe,CACjC,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IACnC,CAAC;IAED;;;;;;;;OAQG;IACH,UAAU,GAAG,CAAC,IAAY,EAAmB,EAAE;QAC7C,OAAO,IAAI,CAAC,IAAI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,EAAE,EAAE,EAAE;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAE5D,OAAO,2DAAG,QAAQ,GAAI,CAAC;IACzB,CAAC,CAAC;IAEF;;;;;;;;;OASG;IACH,SAAS,GAEJ,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAC3D,OAAO,2DAAG,QAAQ,GAAI,CAAC;IACzB,CAAC,CAAC;CACH;AAraD,+CAqaC","sourcesContent":["'use client';\nimport {\n // types\n Auth,\n Storage,\n txInit,\n type AuthState,\n type User,\n type ConnectionStatus,\n type TransactionChunk,\n type RoomSchemaShape,\n type InstaQLOptions,\n type InstantConfig,\n type PageInfoResponse,\n InstantCoreDatabase,\n init as core_init,\n InstaQLLifecycleState,\n InstaQLResponse,\n RoomsOf,\n InstantSchemaDef,\n IInstantDatabase,\n InstantError,\n ValidQuery,\n Streams,\n} from '@instantdb/core';\nimport {\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\nimport { useQueryInternal } from './useQuery.ts';\nimport {\n type InfiniteQueryResult,\n useInfiniteQuerySubscription,\n} from './useInfiniteQuerySubscription.ts';\nimport { InstantReactRoom, rooms } from './InstantReactRoom.ts';\n\nconst defaultAuthState = {\n isLoading: true,\n user: undefined,\n error: undefined,\n};\n\nexport default abstract class InstantReactAbstractDatabase<\n // need to pull this schema out to another generic for query params, not sure why\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n Config extends InstantConfig<Schema, boolean> = InstantConfig<\n Schema,\n UseDates\n >,\n Rooms extends RoomSchemaShape = RoomsOf<Schema>,\n> implements IInstantDatabase<Schema>\n{\n public tx = txInit<Schema>();\n\n public auth: Auth;\n public storage: Storage;\n public streams: Streams;\n public core: InstantCoreDatabase<Schema, UseDates>;\n\n /** @deprecated use `core` instead */\n public _core: InstantCoreDatabase<Schema, UseDates>;\n\n static Store?: any;\n static NetworkListener?: any;\n static EventSourceImpl?: any;\n\n constructor(\n config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {\n useDateObjects?: UseDates;\n },\n versions?: { [key: string]: string },\n ) {\n this.core = core_init<Schema, UseDates>(\n config,\n // @ts-expect-error because TS can't resolve subclass statics\n config.Store || this.constructor.Store,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.NetworkListener,\n versions,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.EventSourceImpl,\n );\n this._core = this.core;\n this.auth = this.core.auth;\n this.storage = this.core.storage;\n this.streams = this.core.streams;\n }\n\n /**\n * Returns a unique ID for a given `name`. It's stored in local storage,\n * so you will get the same ID across sessions.\n *\n * This is useful for generating IDs that could identify a local device or user.\n *\n * @example\n * const deviceId = await db.getLocalId('device');\n */\n getLocalId = (name: string): Promise<string> => {\n return this.core.getLocalId(name);\n };\n\n /**\n * A hook that returns a unique ID for a given `name`. localIds are\n * stored in local storage, so you will get the same ID across sessions.\n *\n * Initially returns `null`, and then loads the localId.\n *\n * @example\n * const deviceId = db.useLocalId('device');\n * if (!deviceId) return null; // loading\n * console.log('Device ID:', deviceId)\n */\n useLocalId = (name: string): string | null => {\n const [localId, setLocalId] = useState<string | null>(null);\n\n useEffect(() => {\n let mounted = true;\n const f = async () => {\n const id = await this.getLocalId(name);\n if (!mounted) return;\n setLocalId(id);\n };\n f();\n return;\n }, [name]);\n\n return localId;\n };\n\n /**\n * Obtain a handle to a room, which allows you to listen to topics and presence data\n *\n * If you don't provide a `type` or `id`, Instant will default to `_defaultRoomType` and `_defaultRoomId`\n * as the room type and id, respectively.\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n */\n room<RoomType extends string & keyof Rooms>(\n type: RoomType = '_defaultRoomType' as RoomType,\n id: string = '_defaultRoomId',\n ) {\n return new InstantReactRoom<Schema, Rooms, RoomType>(this.core, type, id);\n }\n\n /**\n * Hooks for working with rooms\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n * const publish = db.rooms.usePublishTopic(room, 'emoji');\n * // ...\n */\n rooms = rooms;\n\n /**\n * Use this to write data! You can create, update, delete, and link objects\n *\n * @see https://instantdb.com/docs/instaml\n *\n * @example\n * // Create a new object in the `goals` namespace\n * const goalId = id();\n * db.transact(db.tx.goals[goalId].update({title: \"Get fit\"}))\n *\n * // Update the title\n * db.transact(db.tx.goals[goalId].update({title: \"Get super fit\"}))\n *\n * // Delete it\n * db.transact(db.tx.goals[goalId].delete())\n *\n * // Or create an association:\n * todoId = id();\n * db.transact([\n * db.tx.todos[todoId].update({ title: 'Go on a run' }),\n * db.tx.goals[goalId].link({todos: todoId}),\n * ])\n */\n transact = (\n chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],\n ) => {\n return this.core.transact(chunks);\n };\n\n /**\n * Use this to query your data!\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * // listen to all goals\n * const { isLoading, error, data } = db.useQuery({ goals: {} });\n *\n * // goals where the title is \"Get Fit\"\n * const { isLoading, error, data } = db.useQuery({\n * goals: { $: { where: { title: 'Get Fit' } } },\n * });\n *\n * // all goals, _alongside_ their todos\n * const { isLoading, error, data } = db.useQuery({\n * goals: { todos: {} },\n * });\n *\n * // skip if `user` is not logged in\n * const { isLoading, error, data } = db.useQuery(\n * auth.user ? { goals: {} } : null,\n * );\n */\n useQuery = <Q extends ValidQuery<Q, Schema>>(\n query: null | Q,\n opts?: InstaQLOptions,\n ): InstaQLLifecycleState<Schema, Q, UseDates> => {\n return useQueryInternal<Q, Schema, UseDates>(this.core, query, opts).state;\n };\n\n /**\n * Listen for the logged in state. This is useful\n * for deciding when to show a login screen.\n *\n * Check out the docs for an example `Login` component too!\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function App() {\n * const { isLoading, user, error } = db.useAuth()\n * if (isLoading) {\n * return <div>Loading...</div>\n * }\n * if (error) {\n * return <div>Uh oh! {error.message}</div>\n * }\n * if (user) {\n * return <Main user={user} />\n * }\n * return <Login />\n * }\n *\n */\n useAuth = (): AuthState => {\n return this._useAuth();\n };\n\n protected _useAuth(): AuthState {\n // We use a ref to store the result of the query.\n // This is becuase `useSyncExternalStore` uses `Object.is`\n // to compare the previous and next state.\n // If we don't use a ref, the state will always be considered different, so\n // the component will always re-render.\n const resultCacheRef = useRef<AuthState>(\n this.core._reactor._currentUserCached,\n );\n\n // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe\n // if `subscribe` changes, so we use `useCallback` to memoize the function.\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeAuth((auth) => {\n resultCacheRef.current = { isLoading: false, ...auth };\n cb();\n });\n\n return unsubscribe;\n }, []);\n\n const state = useSyncExternalStore<AuthState>(\n subscribe,\n () => resultCacheRef.current,\n () => defaultAuthState,\n );\n return state;\n }\n\n /**\n * Subscribe to the currently logged in user.\n * If the user is not logged in, this hook with throw an Error.\n * You will want to protect any calls of this hook with a\n * <db.SignedIn> component, or your own logic based on db.useAuth()\n *\n * @see https://instantdb.com/docs/auth\n * @throws Error indicating user not signed in\n * @example\n * function UserDisplay() {\n * const user = db.useUser()\n * return <div>Logged in as: {user.email}</div>\n * }\n *\n * <db.SignedIn>\n * <UserDisplay />\n * </db.SignedIn>\n *\n */\n useUser = (): User => {\n const { user } = this.useAuth();\n if (!user) {\n throw new InstantError(\n 'useUser must be used within an auth-protected route',\n );\n }\n return user;\n };\n\n /**\n * One time query for the logged in state. This is useful\n * for scenarios where you want to know the current auth\n * state without subscribing to changes.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * const user = await db.getAuth();\n * console.log('logged in as', user.email)\n */\n getAuth(): Promise<User | null> {\n return this.core.getAuth();\n }\n\n /**\n * Listen for connection status changes to Instant. Use this for things like\n * showing connection state to users\n *\n * @see https://www.instantdb.com/docs/patterns#connection-status\n * @example\n * function App() {\n * const status = db.useConnectionStatus()\n * const connectionState =\n * status === 'connecting' || status === 'opened'\n * ? 'authenticating'\n * : status === 'authenticated'\n * ? 'connected'\n * : status === 'closed'\n * ? 'closed'\n * : status === 'errored'\n * ? 'errored'\n * : 'unexpected state';\n *\n * return <div>Connection state: {connectionState}</div>\n * }\n */\n useConnectionStatus = (): ConnectionStatus => {\n const statusRef = useRef<ConnectionStatus>(\n this.core._reactor.status as ConnectionStatus,\n );\n\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeConnectionStatus((newStatus) => {\n if (newStatus !== statusRef.current) {\n statusRef.current = newStatus;\n cb();\n }\n });\n\n return unsubscribe;\n }, []);\n\n const status = useSyncExternalStore<ConnectionStatus>(\n subscribe,\n () => statusRef.current,\n // For SSR, always return 'connecting' as the initial state\n () => 'connecting',\n );\n\n return status;\n };\n\n /**\n * Use this for one-off queries.\n * Returns local data if available, otherwise fetches from the server.\n * Because we want to avoid stale data, this method will throw an error\n * if the user is offline or there is no active connection to the server.\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n *\n * const resp = await db.queryOnce({ goals: {} });\n * console.log(resp.data.goals)\n */\n queryOnce = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): Promise<{\n data: InstaQLResponse<Schema, Q, UseDates>;\n pageInfo: PageInfoResponse<Q>;\n }> => {\n return this.core.queryOnce(query, opts);\n };\n\n /**\n * Subscribe to a query and incrementally load more items\n *\n * Only one top level namespace in the query is allowed.\n *\n * Changing the query or options while the subscription is active will\n * reset the subscription and start over with new data.\n * @example\n * const {\n * data,\n * loadNextPage,\n * canLoadNextPage,\n * } = db.useInfiniteQuery({\n * posts: {\n * $: {\n * limit: 20, // Load 20 posts at a time\n * order: {\n * createdAt: 'desc',\n * },\n * },\n * },\n * });\n */\n useInfiniteQuery = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): InfiniteQueryResult<Schema, Q, UseDates> => {\n const result = useInfiniteQuerySubscription<Schema, Q, UseDates>({\n core: this.core,\n query: query,\n opts,\n });\n return result;\n };\n\n /**\n * Only render children if the user is signed in.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedIn>\n * <MyComponent />\n * </db.SignedIn>\n *\n */\n SignedIn: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || !auth.user) return null;\n\n return <>{children}</>;\n };\n\n /**\n * Only render children if the user is signed out.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedOut>\n * <MyComponent />\n * </db.SignedOut>\n *\n */\n SignedOut: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || auth.user) return null;\n return <>{children}</>;\n };\n}\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type InstantCoreDatabase, type InstantSchemaDef, type InstaQLOptions, InstaQLResponse, ValidQuery } from '@instantdb/core';
|
|
2
|
+
export type InfiniteQueryResult<Schema extends InstantSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = {
|
|
3
|
+
error: {
|
|
4
|
+
message: string;
|
|
5
|
+
};
|
|
6
|
+
data: undefined;
|
|
7
|
+
isLoading: false;
|
|
8
|
+
canLoadNextPage: boolean;
|
|
9
|
+
loadNextPage: () => void;
|
|
10
|
+
} | {
|
|
11
|
+
error: undefined;
|
|
12
|
+
data: undefined;
|
|
13
|
+
isLoading: true;
|
|
14
|
+
canLoadNextPage: boolean;
|
|
15
|
+
loadNextPage: () => void;
|
|
16
|
+
} | {
|
|
17
|
+
error: undefined;
|
|
18
|
+
data: InstaQLResponse<Schema, Q, UseDates>;
|
|
19
|
+
isLoading: false;
|
|
20
|
+
canLoadNextPage: boolean;
|
|
21
|
+
loadNextPage: () => void;
|
|
22
|
+
};
|
|
23
|
+
export declare function useInfiniteQuerySubscription<Schema extends InstantSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean>({ core, query, opts, }: {
|
|
24
|
+
core: InstantCoreDatabase<Schema, UseDates>;
|
|
25
|
+
query: Q | null;
|
|
26
|
+
opts?: InstaQLOptions;
|
|
27
|
+
}): InfiniteQueryResult<Schema, Q, UseDates>;
|
|
28
|
+
//# sourceMappingURL=useInfiniteQuerySubscription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInfiniteQuerySubscription.d.ts","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,eAAe,EACf,UAAU,EAGX,MAAM,iBAAiB,CAAC;AAGzB,MAAM,MAAM,mBAAmB,CAC7B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,QAAQ,SAAS,OAAO,IAEtB;IACE,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3B,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,KAAK,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,GACD;IACE,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,GACD;IACE,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC3C,SAAS,EAAE,KAAK,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,CAAC;AASN,wBAAgB,4BAA4B,CAC1C,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,QAAQ,SAAS,OAAO,EACxB,EACA,IAAI,EACJ,KAAK,EACL,IAAI,GACL,EAAE;IACD,IAAI,EAAE,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC5C,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAChB,IAAI,CAAC,EAAE,cAAc,CAAC;CACvB,GAAG,mBAAmB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAoE3C"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useInfiniteQuerySubscription = useInfiniteQuerySubscription;
|
|
4
|
+
const core_1 = require("@instantdb/core");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const defaultState = {
|
|
7
|
+
error: undefined,
|
|
8
|
+
data: undefined,
|
|
9
|
+
isLoading: true,
|
|
10
|
+
canLoadNextPage: false,
|
|
11
|
+
};
|
|
12
|
+
function useInfiniteQuerySubscription({ core, query, opts, }) {
|
|
13
|
+
const subRef = (0, react_1.useRef)(null);
|
|
14
|
+
const queryHash = (0, core_1.weakHash)(query);
|
|
15
|
+
const optsHash = (0, core_1.weakHash)(opts);
|
|
16
|
+
const snapshot = (0, core_1.getInfiniteQueryInitialSnapshot)(core, query, opts);
|
|
17
|
+
const stateCacheRef = (0, react_1.useRef)({
|
|
18
|
+
...snapshot,
|
|
19
|
+
isLoading: !snapshot.data && !snapshot.error,
|
|
20
|
+
});
|
|
21
|
+
const subscribe = (0, react_1.useCallback)((cb) => {
|
|
22
|
+
subRef.current = null;
|
|
23
|
+
stateCacheRef.current = {
|
|
24
|
+
error: undefined,
|
|
25
|
+
data: undefined,
|
|
26
|
+
isLoading: true,
|
|
27
|
+
canLoadNextPage: false,
|
|
28
|
+
};
|
|
29
|
+
cb();
|
|
30
|
+
if (!query) {
|
|
31
|
+
return () => { };
|
|
32
|
+
}
|
|
33
|
+
const sub = core.subscribeInfiniteQuery(query, (resp) => {
|
|
34
|
+
stateCacheRef.current = {
|
|
35
|
+
...resp,
|
|
36
|
+
isLoading: false,
|
|
37
|
+
};
|
|
38
|
+
cb();
|
|
39
|
+
}, opts);
|
|
40
|
+
subRef.current = sub;
|
|
41
|
+
return () => {
|
|
42
|
+
sub.unsubscribe();
|
|
43
|
+
if (subRef.current === sub) {
|
|
44
|
+
subRef.current = null;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}, [queryHash, optsHash]);
|
|
48
|
+
const state = (0, react_1.useSyncExternalStore)(subscribe, () => stateCacheRef.current, () => defaultState);
|
|
49
|
+
const loadNextPage = () => {
|
|
50
|
+
subRef.current?.loadNextPage();
|
|
51
|
+
};
|
|
52
|
+
// @ts-expect-error union type
|
|
53
|
+
return {
|
|
54
|
+
...state,
|
|
55
|
+
loadNextPage,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=useInfiniteQuerySubscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInfiniteQuerySubscription.js","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":";;AA8CA,oEAgFC;AA9HD,0CASyB;AACzB,iCAAkE;AA6BlE,MAAM,YAAY,GAAG;IACnB,KAAK,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,EAAE;YACV,GAAG,CAAC,WAAW,EAAE,CAAC;YAClB,IAAI,MAAM,CAAC,OAAO,KAAK,GAAG,EAAE,CAAC;gBAC3B,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;YACxB,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,EACD,CAAC,SAAS,EAAE,QAAQ,CAAC,CACtB,CAAC;IAEF,MAAM,KAAK,GAAG,IAAA,4BAAoB,EAChC,SAAS,EACT,GAAG,EAAE,CAAC,aAAa,CAAC,OAAO,EAC3B,GAAG,EAAE,CAAC,YAAY,CACnB,CAAC;IAEF,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC;IACjC,CAAC,CAAC;IAEF,8BAA8B;IAC9B,OAAO;QACL,GAAG,KAAK;QACR,YAAY;KACb,CAAC;AACJ,CAAC","sourcesContent":["import {\n type InfiniteQuerySubscription,\n type InstantCoreDatabase,\n type InstantSchemaDef,\n type InstaQLOptions,\n InstaQLResponse,\n ValidQuery,\n weakHash,\n getInfiniteQueryInitialSnapshot,\n} from '@instantdb/core';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\n\nexport type InfiniteQueryResult<\n Schema extends InstantSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n> =\n | {\n error: { message: string };\n data: undefined;\n isLoading: false;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n }\n | {\n error: undefined;\n data: undefined;\n isLoading: true;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n }\n | {\n error: undefined;\n data: InstaQLResponse<Schema, Q, UseDates>;\n isLoading: false;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n };\n\nconst defaultState = {\n error: undefined,\n data: undefined,\n isLoading: true,\n canLoadNextPage: false,\n};\n\nexport function useInfiniteQuerySubscription<\n Schema extends InstantSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n>({\n core,\n query,\n opts,\n}: {\n core: InstantCoreDatabase<Schema, UseDates>;\n query: Q | null;\n opts?: InstaQLOptions;\n}): InfiniteQueryResult<Schema, Q, UseDates> {\n const subRef = useRef<InfiniteQuerySubscription | null>(null);\n\n const queryHash = weakHash(query);\n const optsHash = weakHash(opts);\n const snapshot = getInfiniteQueryInitialSnapshot(core, query, opts);\n\n const stateCacheRef = useRef<\n Omit<InfiniteQueryResult<Schema, Q, UseDates>, 'loadNextPage'>\n >({\n ...snapshot,\n isLoading: !snapshot.data && !snapshot.error,\n });\n\n const subscribe = useCallback(\n (cb: () => void) => {\n subRef.current = null;\n stateCacheRef.current = {\n error: undefined,\n data: undefined,\n isLoading: true,\n canLoadNextPage: false,\n };\n cb();\n\n if (!query) {\n return () => {};\n }\n\n const sub = core.subscribeInfiniteQuery(\n query,\n (resp) => {\n stateCacheRef.current = {\n ...resp,\n isLoading: false,\n };\n cb();\n },\n opts,\n );\n\n subRef.current = sub;\n\n return () => {\n sub.unsubscribe();\n if (subRef.current === sub) {\n subRef.current = null;\n }\n };\n },\n [queryHash, optsHash],\n );\n\n const state = useSyncExternalStore(\n subscribe,\n () => stateCacheRef.current,\n () => defaultState,\n );\n\n const loadNextPage = () => {\n subRef.current?.loadNextPage();\n };\n\n // @ts-expect-error union type\n return {\n ...state,\n loadNextPage,\n };\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Auth, Storage, type AuthState, type User, type ConnectionStatus, type TransactionChunk, type RoomSchemaShape, type InstaQLOptions, type InstantConfig, type PageInfoResponse, InstantCoreDatabase, InstaQLLifecycleState, InstaQLResponse, RoomsOf, InstantSchemaDef, IInstantDatabase, ValidQuery, Streams } from '@instantdb/core';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
+
import { type InfiniteQueryResult } from './useInfiniteQuerySubscription.ts';
|
|
3
4
|
import { InstantReactRoom } from './InstantReactRoom.ts';
|
|
4
5
|
export default abstract class InstantReactAbstractDatabase<Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean = false, Config extends InstantConfig<Schema, boolean> = InstantConfig<Schema, UseDates>, Rooms extends RoomSchemaShape = RoomsOf<Schema>> implements IInstantDatabase<Schema> {
|
|
5
6
|
tx: import("@instantdb/core").TxChunk<Schema>;
|
|
@@ -215,6 +216,30 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
215
216
|
data: InstaQLResponse<Schema, Q, UseDates>;
|
|
216
217
|
pageInfo: PageInfoResponse<Q>;
|
|
217
218
|
}>;
|
|
219
|
+
/**
|
|
220
|
+
* Subscribe to a query and incrementally load more items
|
|
221
|
+
*
|
|
222
|
+
* Only one top level namespace in the query is allowed.
|
|
223
|
+
*
|
|
224
|
+
* Changing the query or options while the subscription is active will
|
|
225
|
+
* reset the subscription and start over with new data.
|
|
226
|
+
* @example
|
|
227
|
+
* const {
|
|
228
|
+
* data,
|
|
229
|
+
* loadNextPage,
|
|
230
|
+
* canLoadNextPage,
|
|
231
|
+
* } = db.useInfiniteQuery({
|
|
232
|
+
* posts: {
|
|
233
|
+
* $: {
|
|
234
|
+
* limit: 20, // Load 20 posts at a time
|
|
235
|
+
* order: {
|
|
236
|
+
* createdAt: 'desc',
|
|
237
|
+
* },
|
|
238
|
+
* },
|
|
239
|
+
* },
|
|
240
|
+
* });
|
|
241
|
+
*/
|
|
242
|
+
useInfiniteQuery: <Q extends ValidQuery<Q, Schema>>(query: Q, opts?: InstaQLOptions) => InfiniteQueryResult<Schema, Q, UseDates>;
|
|
218
243
|
/**
|
|
219
244
|
* Only render children if the user is signed in.
|
|
220
245
|
* @see https://instantdb.com/docs/auth
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InstantReactAbstractDatabase.d.ts","sourceRoot":"","sources":["../../src/InstantReactAbstractDatabase.tsx"],"names":[],"mappings":"AACA,OAAO,EAEL,IAAI,EACJ,OAAO,EAEP,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,gBAAgB,EACrB,KAA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ACH"}
|
|
@@ -3,6 +3,7 @@ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
|
3
3
|
import { txInit, init as core_init, InstantError, } from '@instantdb/core';
|
|
4
4
|
import { useCallback, useEffect, useRef, useState, useSyncExternalStore, } from 'react';
|
|
5
5
|
import { useQueryInternal } from "./useQuery.js";
|
|
6
|
+
import { useInfiniteQuerySubscription, } from "./useInfiniteQuerySubscription.js";
|
|
6
7
|
import { InstantReactRoom, rooms } from "./InstantReactRoom.js";
|
|
7
8
|
const defaultAuthState = {
|
|
8
9
|
isLoading: true,
|
|
@@ -289,6 +290,37 @@ export default class InstantReactAbstractDatabase {
|
|
|
289
290
|
queryOnce = (query, opts) => {
|
|
290
291
|
return this.core.queryOnce(query, opts);
|
|
291
292
|
};
|
|
293
|
+
/**
|
|
294
|
+
* Subscribe to a query and incrementally load more items
|
|
295
|
+
*
|
|
296
|
+
* Only one top level namespace in the query is allowed.
|
|
297
|
+
*
|
|
298
|
+
* Changing the query or options while the subscription is active will
|
|
299
|
+
* reset the subscription and start over with new data.
|
|
300
|
+
* @example
|
|
301
|
+
* const {
|
|
302
|
+
* data,
|
|
303
|
+
* loadNextPage,
|
|
304
|
+
* canLoadNextPage,
|
|
305
|
+
* } = db.useInfiniteQuery({
|
|
306
|
+
* posts: {
|
|
307
|
+
* $: {
|
|
308
|
+
* limit: 20, // Load 20 posts at a time
|
|
309
|
+
* order: {
|
|
310
|
+
* createdAt: 'desc',
|
|
311
|
+
* },
|
|
312
|
+
* },
|
|
313
|
+
* },
|
|
314
|
+
* });
|
|
315
|
+
*/
|
|
316
|
+
useInfiniteQuery = (query, opts) => {
|
|
317
|
+
const result = useInfiniteQuerySubscription({
|
|
318
|
+
core: this.core,
|
|
319
|
+
query: query,
|
|
320
|
+
opts,
|
|
321
|
+
});
|
|
322
|
+
return result;
|
|
323
|
+
};
|
|
292
324
|
/**
|
|
293
325
|
* Only render children if the user is signed in.
|
|
294
326
|
* @see https://instantdb.com/docs/auth
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InstantReactAbstractDatabase.js","sourceRoot":"","sources":["../../src/InstantReactAbstractDatabase.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AACb,OAAO,EAIL,MAAM,EAUN,IAAI,IAAI,SAAS,EAMjB,YAAY,GAGb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAEL,WAAW,EACX,SAAS,EACT,MAAM,EACN,QAAQ,EACR,oBAAoB,GACrB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEhE,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,SAAS;CACjB,CAAC;AAEF,MAAM,CAAC,OAAO,OAAgB,4BAA4B;IAWjD,EAAE,GAAG,MAAM,EAAU,CAAC;IAEtB,IAAI,CAAO;IACX,OAAO,CAAU;IACjB,OAAO,CAAU;IACjB,IAAI,CAAwC;IAEnD,qCAAqC;IAC9B,KAAK,CAAwC;IAEpD,MAAM,CAAC,KAAK,CAAO;IACnB,MAAM,CAAC,eAAe,CAAO;IAC7B,MAAM,CAAC,eAAe,CAAO;IAE7B,YACE,MAEC,EACD,QAAoC;QAEpC,IAAI,CAAC,IAAI,GAAG,SAAS,CACnB,MAAM;QACN,6DAA6D;QAC7D,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK;QACtC,6DAA6D;QAC7D,IAAI,CAAC,WAAW,CAAC,eAAe,EAChC,QAAQ;QACR,6DAA6D;QAC7D,IAAI,CAAC,WAAW,CAAC,eAAe,CACjC,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;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C,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAE5D,OAAO,4BAAG,QAAQ,GAAI,CAAC;IACzB,CAAC,CAAC;IAEF;;;;;;;;;OASG;IACH,SAAS,GAEJ,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAC3D,OAAO,4BAAG,QAAQ,GAAI,CAAC;IACzB,CAAC,CAAC;CACH","sourcesContent":["'use client';\nimport {\n // types\n Auth,\n Storage,\n txInit,\n type AuthState,\n type User,\n type ConnectionStatus,\n type TransactionChunk,\n type RoomSchemaShape,\n type InstaQLOptions,\n type InstantConfig,\n type PageInfoResponse,\n InstantCoreDatabase,\n init as core_init,\n InstaQLLifecycleState,\n InstaQLResponse,\n RoomsOf,\n InstantSchemaDef,\n IInstantDatabase,\n InstantError,\n ValidQuery,\n Streams,\n} from '@instantdb/core';\nimport {\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\nimport { useQueryInternal } from './useQuery.ts';\nimport { InstantReactRoom, rooms } from './InstantReactRoom.ts';\n\nconst defaultAuthState = {\n isLoading: true,\n user: undefined,\n error: undefined,\n};\n\nexport default abstract class InstantReactAbstractDatabase<\n // need to pull this schema out to another generic for query params, not sure why\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n Config extends InstantConfig<Schema, boolean> = InstantConfig<\n Schema,\n UseDates\n >,\n Rooms extends RoomSchemaShape = RoomsOf<Schema>,\n> implements IInstantDatabase<Schema>\n{\n public tx = txInit<Schema>();\n\n public auth: Auth;\n public storage: Storage;\n public streams: Streams;\n public core: InstantCoreDatabase<Schema, UseDates>;\n\n /** @deprecated use `core` instead */\n public _core: InstantCoreDatabase<Schema, UseDates>;\n\n static Store?: any;\n static NetworkListener?: any;\n static EventSourceImpl?: any;\n\n constructor(\n config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {\n useDateObjects?: UseDates;\n },\n versions?: { [key: string]: string },\n ) {\n this.core = core_init<Schema, UseDates>(\n config,\n // @ts-expect-error because TS can't resolve subclass statics\n config.Store || this.constructor.Store,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.NetworkListener,\n versions,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.EventSourceImpl,\n );\n this._core = this.core;\n this.auth = this.core.auth;\n this.storage = this.core.storage;\n this.streams = this.core.streams;\n }\n\n /**\n * Returns a unique ID for a given `name`. It's stored in local storage,\n * so you will get the same ID across sessions.\n *\n * This is useful for generating IDs that could identify a local device or user.\n *\n * @example\n * const deviceId = await db.getLocalId('device');\n */\n getLocalId = (name: string): Promise<string> => {\n return this.core.getLocalId(name);\n };\n\n /**\n * A hook that returns a unique ID for a given `name`. localIds are\n * stored in local storage, so you will get the same ID across sessions.\n *\n * Initially returns `null`, and then loads the localId.\n *\n * @example\n * const deviceId = db.useLocalId('device');\n * if (!deviceId) return null; // loading\n * console.log('Device ID:', deviceId)\n */\n useLocalId = (name: string): string | null => {\n const [localId, setLocalId] = useState<string | null>(null);\n\n useEffect(() => {\n let mounted = true;\n const f = async () => {\n const id = await this.getLocalId(name);\n if (!mounted) return;\n setLocalId(id);\n };\n f();\n return;\n }, [name]);\n\n return localId;\n };\n\n /**\n * Obtain a handle to a room, which allows you to listen to topics and presence data\n *\n * If you don't provide a `type` or `id`, Instant will default to `_defaultRoomType` and `_defaultRoomId`\n * as the room type and id, respectively.\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n */\n room<RoomType extends string & keyof Rooms>(\n type: RoomType = '_defaultRoomType' as RoomType,\n id: string = '_defaultRoomId',\n ) {\n return new InstantReactRoom<Schema, Rooms, RoomType>(this.core, type, id);\n }\n\n /**\n * Hooks for working with rooms\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n * const publish = db.rooms.usePublishTopic(room, 'emoji');\n * // ...\n */\n rooms = rooms;\n\n /**\n * Use this to write data! You can create, update, delete, and link objects\n *\n * @see https://instantdb.com/docs/instaml\n *\n * @example\n * // Create a new object in the `goals` namespace\n * const goalId = id();\n * db.transact(db.tx.goals[goalId].update({title: \"Get fit\"}))\n *\n * // Update the title\n * db.transact(db.tx.goals[goalId].update({title: \"Get super fit\"}))\n *\n * // Delete it\n * db.transact(db.tx.goals[goalId].delete())\n *\n * // Or create an association:\n * todoId = id();\n * db.transact([\n * db.tx.todos[todoId].update({ title: 'Go on a run' }),\n * db.tx.goals[goalId].link({todos: todoId}),\n * ])\n */\n transact = (\n chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],\n ) => {\n return this.core.transact(chunks);\n };\n\n /**\n * Use this to query your data!\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * // listen to all goals\n * const { isLoading, error, data } = db.useQuery({ goals: {} });\n *\n * // goals where the title is \"Get Fit\"\n * const { isLoading, error, data } = db.useQuery({\n * goals: { $: { where: { title: 'Get Fit' } } },\n * });\n *\n * // all goals, _alongside_ their todos\n * const { isLoading, error, data } = db.useQuery({\n * goals: { todos: {} },\n * });\n *\n * // skip if `user` is not logged in\n * const { isLoading, error, data } = db.useQuery(\n * auth.user ? { goals: {} } : null,\n * );\n */\n useQuery = <Q extends ValidQuery<Q, Schema>>(\n query: null | Q,\n opts?: InstaQLOptions,\n ): InstaQLLifecycleState<Schema, Q, UseDates> => {\n return useQueryInternal<Q, Schema, UseDates>(this.core, query, opts).state;\n };\n\n /**\n * Listen for the logged in state. This is useful\n * for deciding when to show a login screen.\n *\n * Check out the docs for an example `Login` component too!\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function App() {\n * const { isLoading, user, error } = db.useAuth()\n * if (isLoading) {\n * return <div>Loading...</div>\n * }\n * if (error) {\n * return <div>Uh oh! {error.message}</div>\n * }\n * if (user) {\n * return <Main user={user} />\n * }\n * return <Login />\n * }\n *\n */\n useAuth = (): AuthState => {\n return this._useAuth();\n };\n\n protected _useAuth(): AuthState {\n // We use a ref to store the result of the query.\n // This is becuase `useSyncExternalStore` uses `Object.is`\n // to compare the previous and next state.\n // If we don't use a ref, the state will always be considered different, so\n // the component will always re-render.\n const resultCacheRef = useRef<AuthState>(\n this.core._reactor._currentUserCached,\n );\n\n // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe\n // if `subscribe` changes, so we use `useCallback` to memoize the function.\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeAuth((auth) => {\n resultCacheRef.current = { isLoading: false, ...auth };\n cb();\n });\n\n return unsubscribe;\n }, []);\n\n const state = useSyncExternalStore<AuthState>(\n subscribe,\n () => resultCacheRef.current,\n () => defaultAuthState,\n );\n return state;\n }\n\n /**\n * Subscribe to the currently logged in user.\n * If the user is not logged in, this hook with throw an Error.\n * You will want to protect any calls of this hook with a\n * <db.SignedIn> component, or your own logic based on db.useAuth()\n *\n * @see https://instantdb.com/docs/auth\n * @throws Error indicating user not signed in\n * @example\n * function UserDisplay() {\n * const user = db.useUser()\n * return <div>Logged in as: {user.email}</div>\n * }\n *\n * <db.SignedIn>\n * <UserDisplay />\n * </db.SignedIn>\n *\n */\n useUser = (): User => {\n const { user } = this.useAuth();\n if (!user) {\n throw new InstantError(\n 'useUser must be used within an auth-protected route',\n );\n }\n return user;\n };\n\n /**\n * One time query for the logged in state. This is useful\n * for scenarios where you want to know the current auth\n * state without subscribing to changes.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * const user = await db.getAuth();\n * console.log('logged in as', user.email)\n */\n getAuth(): Promise<User | null> {\n return this.core.getAuth();\n }\n\n /**\n * Listen for connection status changes to Instant. Use this for things like\n * showing connection state to users\n *\n * @see https://www.instantdb.com/docs/patterns#connection-status\n * @example\n * function App() {\n * const status = db.useConnectionStatus()\n * const connectionState =\n * status === 'connecting' || status === 'opened'\n * ? 'authenticating'\n * : status === 'authenticated'\n * ? 'connected'\n * : status === 'closed'\n * ? 'closed'\n * : status === 'errored'\n * ? 'errored'\n * : 'unexpected state';\n *\n * return <div>Connection state: {connectionState}</div>\n * }\n */\n useConnectionStatus = (): ConnectionStatus => {\n const statusRef = useRef<ConnectionStatus>(\n this.core._reactor.status as ConnectionStatus,\n );\n\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeConnectionStatus((newStatus) => {\n if (newStatus !== statusRef.current) {\n statusRef.current = newStatus;\n cb();\n }\n });\n\n return unsubscribe;\n }, []);\n\n const status = useSyncExternalStore<ConnectionStatus>(\n subscribe,\n () => statusRef.current,\n // For SSR, always return 'connecting' as the initial state\n () => 'connecting',\n );\n\n return status;\n };\n\n /**\n * Use this for one-off queries.\n * Returns local data if available, otherwise fetches from the server.\n * Because we want to avoid stale data, this method will throw an error\n * if the user is offline or there is no active connection to the server.\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n *\n * const resp = await db.queryOnce({ goals: {} });\n * console.log(resp.data.goals)\n */\n queryOnce = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): Promise<{\n data: InstaQLResponse<Schema, Q, UseDates>;\n pageInfo: PageInfoResponse<Q>;\n }> => {\n return this.core.queryOnce(query, opts);\n };\n\n /**\n * Only render children if the user is signed in.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedIn>\n * <MyComponent />\n * </db.SignedIn>\n *\n */\n SignedIn: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || !auth.user) return null;\n\n return <>{children}</>;\n };\n\n /**\n * Only render children if the user is signed out.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedOut>\n * <MyComponent />\n * </db.SignedOut>\n *\n */\n SignedOut: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || auth.user) return null;\n return <>{children}</>;\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"InstantReactAbstractDatabase.js","sourceRoot":"","sources":["../../src/InstantReactAbstractDatabase.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AACb,OAAO,EAIL,MAAM,EAUN,IAAI,IAAI,SAAS,EAMjB,YAAY,GAGb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAEL,WAAW,EACX,SAAS,EACT,MAAM,EACN,QAAQ,EACR,oBAAoB,GACrB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAEL,4BAA4B,GAC7B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEhE,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,SAAS;CACjB,CAAC;AAEF,MAAM,CAAC,OAAO,OAAgB,4BAA4B;IAWjD,EAAE,GAAG,MAAM,EAAU,CAAC;IAEtB,IAAI,CAAO;IACX,OAAO,CAAU;IACjB,OAAO,CAAU;IACjB,IAAI,CAAwC;IAEnD,qCAAqC;IAC9B,KAAK,CAAwC;IAEpD,MAAM,CAAC,KAAK,CAAO;IACnB,MAAM,CAAC,eAAe,CAAO;IAC7B,MAAM,CAAC,eAAe,CAAO;IAE7B,YACE,MAEC,EACD,QAAoC;QAEpC,IAAI,CAAC,IAAI,GAAG,SAAS,CACnB,MAAM;QACN,6DAA6D;QAC7D,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK;QACtC,6DAA6D;QAC7D,IAAI,CAAC,WAAW,CAAC,eAAe,EAChC,QAAQ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CAAC,CAAC;IAC1C,CAAC,CAAC;IAEF;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,gBAAgB,GAAG,CACjB,KAAQ,EACR,IAAqB,EACqB,EAAE;QAC5C,MAAM,MAAM,GAAG,4BAA4B,CAAsB;YAC/D,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,KAAK,EAAE,KAAK;YACZ,IAAI;SACL,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF;;;;;;;;;OASG;IACH,QAAQ,GAEH,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAE5D,OAAO,4BAAG,QAAQ,GAAI,CAAC;IACzB,CAAC,CAAC;IAEF;;;;;;;;;OASG;IACH,SAAS,GAEJ,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAC3D,OAAO,4BAAG,QAAQ,GAAI,CAAC;IACzB,CAAC,CAAC;CACH","sourcesContent":["'use client';\nimport {\n // types\n Auth,\n Storage,\n txInit,\n type AuthState,\n type User,\n type ConnectionStatus,\n type TransactionChunk,\n type RoomSchemaShape,\n type InstaQLOptions,\n type InstantConfig,\n type PageInfoResponse,\n InstantCoreDatabase,\n init as core_init,\n InstaQLLifecycleState,\n InstaQLResponse,\n RoomsOf,\n InstantSchemaDef,\n IInstantDatabase,\n InstantError,\n ValidQuery,\n Streams,\n} from '@instantdb/core';\nimport {\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\nimport { useQueryInternal } from './useQuery.ts';\nimport {\n type InfiniteQueryResult,\n useInfiniteQuerySubscription,\n} from './useInfiniteQuerySubscription.ts';\nimport { InstantReactRoom, rooms } from './InstantReactRoom.ts';\n\nconst defaultAuthState = {\n isLoading: true,\n user: undefined,\n error: undefined,\n};\n\nexport default abstract class InstantReactAbstractDatabase<\n // need to pull this schema out to another generic for query params, not sure why\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n Config extends InstantConfig<Schema, boolean> = InstantConfig<\n Schema,\n UseDates\n >,\n Rooms extends RoomSchemaShape = RoomsOf<Schema>,\n> implements IInstantDatabase<Schema>\n{\n public tx = txInit<Schema>();\n\n public auth: Auth;\n public storage: Storage;\n public streams: Streams;\n public core: InstantCoreDatabase<Schema, UseDates>;\n\n /** @deprecated use `core` instead */\n public _core: InstantCoreDatabase<Schema, UseDates>;\n\n static Store?: any;\n static NetworkListener?: any;\n static EventSourceImpl?: any;\n\n constructor(\n config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {\n useDateObjects?: UseDates;\n },\n versions?: { [key: string]: string },\n ) {\n this.core = core_init<Schema, UseDates>(\n config,\n // @ts-expect-error because TS can't resolve subclass statics\n config.Store || this.constructor.Store,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.NetworkListener,\n versions,\n // @ts-expect-error because TS can't resolve subclass statics\n this.constructor.EventSourceImpl,\n );\n this._core = this.core;\n this.auth = this.core.auth;\n this.storage = this.core.storage;\n this.streams = this.core.streams;\n }\n\n /**\n * Returns a unique ID for a given `name`. It's stored in local storage,\n * so you will get the same ID across sessions.\n *\n * This is useful for generating IDs that could identify a local device or user.\n *\n * @example\n * const deviceId = await db.getLocalId('device');\n */\n getLocalId = (name: string): Promise<string> => {\n return this.core.getLocalId(name);\n };\n\n /**\n * A hook that returns a unique ID for a given `name`. localIds are\n * stored in local storage, so you will get the same ID across sessions.\n *\n * Initially returns `null`, and then loads the localId.\n *\n * @example\n * const deviceId = db.useLocalId('device');\n * if (!deviceId) return null; // loading\n * console.log('Device ID:', deviceId)\n */\n useLocalId = (name: string): string | null => {\n const [localId, setLocalId] = useState<string | null>(null);\n\n useEffect(() => {\n let mounted = true;\n const f = async () => {\n const id = await this.getLocalId(name);\n if (!mounted) return;\n setLocalId(id);\n };\n f();\n return;\n }, [name]);\n\n return localId;\n };\n\n /**\n * Obtain a handle to a room, which allows you to listen to topics and presence data\n *\n * If you don't provide a `type` or `id`, Instant will default to `_defaultRoomType` and `_defaultRoomId`\n * as the room type and id, respectively.\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n */\n room<RoomType extends string & keyof Rooms>(\n type: RoomType = '_defaultRoomType' as RoomType,\n id: string = '_defaultRoomId',\n ) {\n return new InstantReactRoom<Schema, Rooms, RoomType>(this.core, type, id);\n }\n\n /**\n * Hooks for working with rooms\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const { peers } = db.rooms.usePresence(room);\n * const publish = db.rooms.usePublishTopic(room, 'emoji');\n * // ...\n */\n rooms = rooms;\n\n /**\n * Use this to write data! You can create, update, delete, and link objects\n *\n * @see https://instantdb.com/docs/instaml\n *\n * @example\n * // Create a new object in the `goals` namespace\n * const goalId = id();\n * db.transact(db.tx.goals[goalId].update({title: \"Get fit\"}))\n *\n * // Update the title\n * db.transact(db.tx.goals[goalId].update({title: \"Get super fit\"}))\n *\n * // Delete it\n * db.transact(db.tx.goals[goalId].delete())\n *\n * // Or create an association:\n * todoId = id();\n * db.transact([\n * db.tx.todos[todoId].update({ title: 'Go on a run' }),\n * db.tx.goals[goalId].link({todos: todoId}),\n * ])\n */\n transact = (\n chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],\n ) => {\n return this.core.transact(chunks);\n };\n\n /**\n * Use this to query your data!\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * // listen to all goals\n * const { isLoading, error, data } = db.useQuery({ goals: {} });\n *\n * // goals where the title is \"Get Fit\"\n * const { isLoading, error, data } = db.useQuery({\n * goals: { $: { where: { title: 'Get Fit' } } },\n * });\n *\n * // all goals, _alongside_ their todos\n * const { isLoading, error, data } = db.useQuery({\n * goals: { todos: {} },\n * });\n *\n * // skip if `user` is not logged in\n * const { isLoading, error, data } = db.useQuery(\n * auth.user ? { goals: {} } : null,\n * );\n */\n useQuery = <Q extends ValidQuery<Q, Schema>>(\n query: null | Q,\n opts?: InstaQLOptions,\n ): InstaQLLifecycleState<Schema, Q, UseDates> => {\n return useQueryInternal<Q, Schema, UseDates>(this.core, query, opts).state;\n };\n\n /**\n * Listen for the logged in state. This is useful\n * for deciding when to show a login screen.\n *\n * Check out the docs for an example `Login` component too!\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function App() {\n * const { isLoading, user, error } = db.useAuth()\n * if (isLoading) {\n * return <div>Loading...</div>\n * }\n * if (error) {\n * return <div>Uh oh! {error.message}</div>\n * }\n * if (user) {\n * return <Main user={user} />\n * }\n * return <Login />\n * }\n *\n */\n useAuth = (): AuthState => {\n return this._useAuth();\n };\n\n protected _useAuth(): AuthState {\n // We use a ref to store the result of the query.\n // This is becuase `useSyncExternalStore` uses `Object.is`\n // to compare the previous and next state.\n // If we don't use a ref, the state will always be considered different, so\n // the component will always re-render.\n const resultCacheRef = useRef<AuthState>(\n this.core._reactor._currentUserCached,\n );\n\n // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe\n // if `subscribe` changes, so we use `useCallback` to memoize the function.\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeAuth((auth) => {\n resultCacheRef.current = { isLoading: false, ...auth };\n cb();\n });\n\n return unsubscribe;\n }, []);\n\n const state = useSyncExternalStore<AuthState>(\n subscribe,\n () => resultCacheRef.current,\n () => defaultAuthState,\n );\n return state;\n }\n\n /**\n * Subscribe to the currently logged in user.\n * If the user is not logged in, this hook with throw an Error.\n * You will want to protect any calls of this hook with a\n * <db.SignedIn> component, or your own logic based on db.useAuth()\n *\n * @see https://instantdb.com/docs/auth\n * @throws Error indicating user not signed in\n * @example\n * function UserDisplay() {\n * const user = db.useUser()\n * return <div>Logged in as: {user.email}</div>\n * }\n *\n * <db.SignedIn>\n * <UserDisplay />\n * </db.SignedIn>\n *\n */\n useUser = (): User => {\n const { user } = this.useAuth();\n if (!user) {\n throw new InstantError(\n 'useUser must be used within an auth-protected route',\n );\n }\n return user;\n };\n\n /**\n * One time query for the logged in state. This is useful\n * for scenarios where you want to know the current auth\n * state without subscribing to changes.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * const user = await db.getAuth();\n * console.log('logged in as', user.email)\n */\n getAuth(): Promise<User | null> {\n return this.core.getAuth();\n }\n\n /**\n * Listen for connection status changes to Instant. Use this for things like\n * showing connection state to users\n *\n * @see https://www.instantdb.com/docs/patterns#connection-status\n * @example\n * function App() {\n * const status = db.useConnectionStatus()\n * const connectionState =\n * status === 'connecting' || status === 'opened'\n * ? 'authenticating'\n * : status === 'authenticated'\n * ? 'connected'\n * : status === 'closed'\n * ? 'closed'\n * : status === 'errored'\n * ? 'errored'\n * : 'unexpected state';\n *\n * return <div>Connection state: {connectionState}</div>\n * }\n */\n useConnectionStatus = (): ConnectionStatus => {\n const statusRef = useRef<ConnectionStatus>(\n this.core._reactor.status as ConnectionStatus,\n );\n\n const subscribe = useCallback((cb: Function) => {\n const unsubscribe = this.core.subscribeConnectionStatus((newStatus) => {\n if (newStatus !== statusRef.current) {\n statusRef.current = newStatus;\n cb();\n }\n });\n\n return unsubscribe;\n }, []);\n\n const status = useSyncExternalStore<ConnectionStatus>(\n subscribe,\n () => statusRef.current,\n // For SSR, always return 'connecting' as the initial state\n () => 'connecting',\n );\n\n return status;\n };\n\n /**\n * Use this for one-off queries.\n * Returns local data if available, otherwise fetches from the server.\n * Because we want to avoid stale data, this method will throw an error\n * if the user is offline or there is no active connection to the server.\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n *\n * const resp = await db.queryOnce({ goals: {} });\n * console.log(resp.data.goals)\n */\n queryOnce = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): Promise<{\n data: InstaQLResponse<Schema, Q, UseDates>;\n pageInfo: PageInfoResponse<Q>;\n }> => {\n return this.core.queryOnce(query, opts);\n };\n\n /**\n * Subscribe to a query and incrementally load more items\n *\n * Only one top level namespace in the query is allowed.\n *\n * Changing the query or options while the subscription is active will\n * reset the subscription and start over with new data.\n * @example\n * const {\n * data,\n * loadNextPage,\n * canLoadNextPage,\n * } = db.useInfiniteQuery({\n * posts: {\n * $: {\n * limit: 20, // Load 20 posts at a time\n * order: {\n * createdAt: 'desc',\n * },\n * },\n * },\n * });\n */\n useInfiniteQuery = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): InfiniteQueryResult<Schema, Q, UseDates> => {\n const result = useInfiniteQuerySubscription<Schema, Q, UseDates>({\n core: this.core,\n query: query,\n opts,\n });\n return result;\n };\n\n /**\n * Only render children if the user is signed in.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedIn>\n * <MyComponent />\n * </db.SignedIn>\n *\n */\n SignedIn: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || !auth.user) return null;\n\n return <>{children}</>;\n };\n\n /**\n * Only render children if the user is signed out.\n * @see https://instantdb.com/docs/auth\n *\n * @example\n * <db.SignedOut>\n * <MyComponent />\n * </db.SignedOut>\n *\n */\n SignedOut: React.FC<{\n children: ReactNode;\n }> = ({ children }) => {\n const auth = this.useAuth();\n if (auth.isLoading || auth.error || auth.user) return null;\n return <>{children}</>;\n };\n}\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type InstantCoreDatabase, type InstantSchemaDef, type InstaQLOptions, InstaQLResponse, ValidQuery } from '@instantdb/core';
|
|
2
|
+
export type InfiniteQueryResult<Schema extends InstantSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = {
|
|
3
|
+
error: {
|
|
4
|
+
message: string;
|
|
5
|
+
};
|
|
6
|
+
data: undefined;
|
|
7
|
+
isLoading: false;
|
|
8
|
+
canLoadNextPage: boolean;
|
|
9
|
+
loadNextPage: () => void;
|
|
10
|
+
} | {
|
|
11
|
+
error: undefined;
|
|
12
|
+
data: undefined;
|
|
13
|
+
isLoading: true;
|
|
14
|
+
canLoadNextPage: boolean;
|
|
15
|
+
loadNextPage: () => void;
|
|
16
|
+
} | {
|
|
17
|
+
error: undefined;
|
|
18
|
+
data: InstaQLResponse<Schema, Q, UseDates>;
|
|
19
|
+
isLoading: false;
|
|
20
|
+
canLoadNextPage: boolean;
|
|
21
|
+
loadNextPage: () => void;
|
|
22
|
+
};
|
|
23
|
+
export declare function useInfiniteQuerySubscription<Schema extends InstantSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean>({ core, query, opts, }: {
|
|
24
|
+
core: InstantCoreDatabase<Schema, UseDates>;
|
|
25
|
+
query: Q | null;
|
|
26
|
+
opts?: InstaQLOptions;
|
|
27
|
+
}): InfiniteQueryResult<Schema, Q, UseDates>;
|
|
28
|
+
//# sourceMappingURL=useInfiniteQuerySubscription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInfiniteQuerySubscription.d.ts","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,eAAe,EACf,UAAU,EAGX,MAAM,iBAAiB,CAAC;AAGzB,MAAM,MAAM,mBAAmB,CAC7B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,QAAQ,SAAS,OAAO,IAEtB;IACE,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3B,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,KAAK,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,GACD;IACE,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,GACD;IACE,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC3C,SAAS,EAAE,KAAK,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,CAAC;AASN,wBAAgB,4BAA4B,CAC1C,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,QAAQ,SAAS,OAAO,EACxB,EACA,IAAI,EACJ,KAAK,EACL,IAAI,GACL,EAAE;IACD,IAAI,EAAE,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC5C,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAChB,IAAI,CAAC,EAAE,cAAc,CAAC;CACvB,GAAG,mBAAmB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAoE3C"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { weakHash, getInfiniteQueryInitialSnapshot, } from '@instantdb/core';
|
|
2
|
+
import { useCallback, useRef, useSyncExternalStore } from 'react';
|
|
3
|
+
const defaultState = {
|
|
4
|
+
error: undefined,
|
|
5
|
+
data: undefined,
|
|
6
|
+
isLoading: true,
|
|
7
|
+
canLoadNextPage: false,
|
|
8
|
+
};
|
|
9
|
+
export function useInfiniteQuerySubscription({ core, query, opts, }) {
|
|
10
|
+
const subRef = useRef(null);
|
|
11
|
+
const queryHash = weakHash(query);
|
|
12
|
+
const optsHash = weakHash(opts);
|
|
13
|
+
const snapshot = getInfiniteQueryInitialSnapshot(core, query, opts);
|
|
14
|
+
const stateCacheRef = useRef({
|
|
15
|
+
...snapshot,
|
|
16
|
+
isLoading: !snapshot.data && !snapshot.error,
|
|
17
|
+
});
|
|
18
|
+
const subscribe = useCallback((cb) => {
|
|
19
|
+
subRef.current = null;
|
|
20
|
+
stateCacheRef.current = {
|
|
21
|
+
error: undefined,
|
|
22
|
+
data: undefined,
|
|
23
|
+
isLoading: true,
|
|
24
|
+
canLoadNextPage: false,
|
|
25
|
+
};
|
|
26
|
+
cb();
|
|
27
|
+
if (!query) {
|
|
28
|
+
return () => { };
|
|
29
|
+
}
|
|
30
|
+
const sub = core.subscribeInfiniteQuery(query, (resp) => {
|
|
31
|
+
stateCacheRef.current = {
|
|
32
|
+
...resp,
|
|
33
|
+
isLoading: false,
|
|
34
|
+
};
|
|
35
|
+
cb();
|
|
36
|
+
}, opts);
|
|
37
|
+
subRef.current = sub;
|
|
38
|
+
return () => {
|
|
39
|
+
sub.unsubscribe();
|
|
40
|
+
if (subRef.current === sub) {
|
|
41
|
+
subRef.current = null;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}, [queryHash, optsHash]);
|
|
45
|
+
const state = useSyncExternalStore(subscribe, () => stateCacheRef.current, () => defaultState);
|
|
46
|
+
const loadNextPage = () => {
|
|
47
|
+
subRef.current?.loadNextPage();
|
|
48
|
+
};
|
|
49
|
+
// @ts-expect-error union type
|
|
50
|
+
return {
|
|
51
|
+
...state,
|
|
52
|
+
loadNextPage,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=useInfiniteQuerySubscription.js.map
|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,GAAG,IAAI,CAAC,sBAAsB,CACrC,KAAK,EACL,CAAC,IAAI,EAAE,EAAE;YACP,aAAa,CAAC,OAAO,GAAG;gBACtB,GAAG,IAAI;gBACP,SAAS,EAAE,KAAK;aACjB,CAAC;YACF,EAAE,EAAE,CAAC;QACP,CAAC,EACD,IAAI,CACL,CAAC;QAEF,MAAM,CAAC,OAAO,GAAG,GAAG,CAAC;QAErB,OAAO,GAAG,EAAE;YACV,GAAG,CAAC,WAAW,EAAE,CAAC;YAClB,IAAI,MAAM,CAAC,OAAO,KAAK,GAAG,EAAE,CAAC;gBAC3B,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;YACxB,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,EACD,CAAC,SAAS,EAAE,QAAQ,CAAC,CACtB,CAAC;IAEF,MAAM,KAAK,GAAG,oBAAoB,CAChC,SAAS,EACT,GAAG,EAAE,CAAC,aAAa,CAAC,OAAO,EAC3B,GAAG,EAAE,CAAC,YAAY,CACnB,CAAC;IAEF,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC;IACjC,CAAC,CAAC;IAEF,8BAA8B;IAC9B,OAAO;QACL,GAAG,KAAK;QACR,YAAY;KACb,CAAC;AACJ,CAAC","sourcesContent":["import {\n type InfiniteQuerySubscription,\n type InstantCoreDatabase,\n type InstantSchemaDef,\n type InstaQLOptions,\n InstaQLResponse,\n ValidQuery,\n weakHash,\n getInfiniteQueryInitialSnapshot,\n} from '@instantdb/core';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\n\nexport type InfiniteQueryResult<\n Schema extends InstantSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n> =\n | {\n error: { message: string };\n data: undefined;\n isLoading: false;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n }\n | {\n error: undefined;\n data: undefined;\n isLoading: true;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n }\n | {\n error: undefined;\n data: InstaQLResponse<Schema, Q, UseDates>;\n isLoading: false;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n };\n\nconst defaultState = {\n error: undefined,\n data: undefined,\n isLoading: true,\n canLoadNextPage: false,\n};\n\nexport function useInfiniteQuerySubscription<\n Schema extends InstantSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n>({\n core,\n query,\n opts,\n}: {\n core: InstantCoreDatabase<Schema, UseDates>;\n query: Q | null;\n opts?: InstaQLOptions;\n}): InfiniteQueryResult<Schema, Q, UseDates> {\n const subRef = useRef<InfiniteQuerySubscription | null>(null);\n\n const queryHash = weakHash(query);\n const optsHash = weakHash(opts);\n const snapshot = getInfiniteQueryInitialSnapshot(core, query, opts);\n\n const stateCacheRef = useRef<\n Omit<InfiniteQueryResult<Schema, Q, UseDates>, 'loadNextPage'>\n >({\n ...snapshot,\n isLoading: !snapshot.data && !snapshot.error,\n });\n\n const subscribe = useCallback(\n (cb: () => void) => {\n subRef.current = null;\n stateCacheRef.current = {\n error: undefined,\n data: undefined,\n isLoading: true,\n canLoadNextPage: false,\n };\n cb();\n\n if (!query) {\n return () => {};\n }\n\n const sub = core.subscribeInfiniteQuery(\n query,\n (resp) => {\n stateCacheRef.current = {\n ...resp,\n isLoading: false,\n };\n cb();\n },\n opts,\n );\n\n subRef.current = sub;\n\n return () => {\n sub.unsubscribe();\n if (subRef.current === sub) {\n subRef.current = null;\n }\n };\n },\n [queryHash, optsHash],\n );\n\n const state = useSyncExternalStore(\n subscribe,\n () => stateCacheRef.current,\n () => defaultState,\n );\n\n const loadNextPage = () => {\n subRef.current?.loadNextPage();\n };\n\n // @ts-expect-error union type\n return {\n ...state,\n loadNextPage,\n };\n}\n"]}
|