@sidekickdb/react-common 2.0.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/commonjs/{InstantReactAbstractDatabase.d.ts → SidekickReactAbstractDatabase.d.ts} +23 -23
- package/dist/commonjs/SidekickReactAbstractDatabase.d.ts.map +1 -0
- package/dist/commonjs/{InstantReactAbstractDatabase.js → SidekickReactAbstractDatabase.js} +13 -13
- package/dist/commonjs/SidekickReactAbstractDatabase.js.map +1 -0
- package/dist/{esm/InstantReactRoom.d.ts → commonjs/SidekickReactRoom.d.ts} +11 -11
- package/dist/commonjs/SidekickReactRoom.d.ts.map +1 -0
- package/dist/commonjs/{InstantReactRoom.js → SidekickReactRoom.js} +4 -4
- package/dist/commonjs/SidekickReactRoom.js.map +1 -0
- package/dist/commonjs/index.d.ts +3 -3
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +3 -3
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/useInfiniteQuerySubscription.d.ts +6 -6
- package/dist/commonjs/useInfiniteQuerySubscription.d.ts.map +1 -1
- package/dist/commonjs/useInfiniteQuerySubscription.js.map +1 -1
- package/dist/commonjs/useQuery.d.ts +3 -3
- package/dist/commonjs/useQuery.d.ts.map +1 -1
- package/dist/commonjs/useQuery.js.map +1 -1
- package/dist/esm/{InstantReactAbstractDatabase.d.ts → SidekickReactAbstractDatabase.d.ts} +23 -23
- package/dist/esm/SidekickReactAbstractDatabase.d.ts.map +1 -0
- package/dist/esm/{InstantReactAbstractDatabase.js → SidekickReactAbstractDatabase.js} +12 -12
- package/dist/esm/SidekickReactAbstractDatabase.js.map +1 -0
- package/dist/{commonjs/InstantReactRoom.d.ts → esm/SidekickReactRoom.d.ts} +11 -11
- package/dist/esm/SidekickReactRoom.d.ts.map +1 -0
- package/dist/esm/{InstantReactRoom.js → SidekickReactRoom.js} +2 -2
- package/dist/esm/SidekickReactRoom.js.map +1 -0
- package/dist/esm/index.d.ts +3 -3
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/useInfiniteQuerySubscription.d.ts +6 -6
- package/dist/esm/useInfiniteQuerySubscription.d.ts.map +1 -1
- package/dist/esm/useInfiniteQuerySubscription.js.map +1 -1
- package/dist/esm/useQuery.d.ts +3 -3
- package/dist/esm/useQuery.d.ts.map +1 -1
- package/dist/esm/useQuery.js.map +1 -1
- package/dist/standalone/index.js +1782 -1882
- package/dist/standalone/index.umd.cjs +17 -14
- package/package.json +3 -3
- package/src/{InstantReactAbstractDatabase.tsx → SidekickReactAbstractDatabase.tsx} +29 -29
- package/src/{InstantReactRoom.ts → SidekickReactRoom.ts} +12 -12
- package/src/index.ts +4 -4
- package/src/useInfiniteQuerySubscription.ts +9 -9
- package/src/useQuery.ts +12 -14
- package/vite.config.ts +1 -1
- package/dist/commonjs/InstantReactAbstractDatabase.d.ts.map +0 -1
- package/dist/commonjs/InstantReactAbstractDatabase.js.map +0 -1
- package/dist/commonjs/InstantReactRoom.d.ts.map +0 -1
- package/dist/commonjs/InstantReactRoom.js.map +0 -1
- package/dist/esm/InstantReactAbstractDatabase.d.ts.map +0 -1
- package/dist/esm/InstantReactAbstractDatabase.js.map +0 -1
- package/dist/esm/InstantReactRoom.d.ts.map +0 -1
- package/dist/esm/InstantReactRoom.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AA8Bb,
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AA8Bb,4CAmGC;AA/HD,2CAQ0B;AAC1B,iCAAkE;AAElE,MAAM,YAAY,GAAG;IACnB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE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AE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AACrC,CAAC","sourcesContent":["'use client';\n\nimport {\n weakHash,\n coerceQuery,\n type SideQLOptions,\n SidekickCoreDatabase,\n SideQLLifecycleState,\n SidekickSchemaDef,\n ValidQuery,\n} from '@sidekickdb/core';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\n\nconst defaultState = {\n isLoading: true,\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n} as const;\n\nfunction stateForResult(result: any) {\n return {\n isLoading: !Boolean(result),\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n ...(result ? result : {}),\n };\n}\n\nexport function useQueryInternal<\n Q extends ValidQuery<Q, Schema>,\n Schema extends SidekickSchemaDef<any, any, any>,\n UseDates extends boolean,\n>(\n _core: SidekickCoreDatabase<Schema, UseDates>,\n _query: null | Q,\n _opts?: SideQLOptions,\n serverSnapshot?: () => SideQLLifecycleState<Schema, Q, UseDates> | undefined,\n): {\n state: SideQLLifecycleState<Schema, Q, UseDates>;\n query: any;\n queryHash: string;\n} {\n if (_query && _opts && 'ruleParams' in _opts) {\n _query = { $$ruleParams: _opts['ruleParams'], ..._query };\n }\n const query = _query ? coerceQuery(_query) : null;\n const queryHash = weakHash(query);\n\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 //\n // Initialised lazily: useRef's argument is evaluated on every render, and\n // getPreviousResult runs the query against the local store.\n const resultCacheRef = useRef<SideQLLifecycleState<Schema, Q, UseDates>>(\n null!,\n );\n if (resultCacheRef.current === null) {\n resultCacheRef.current = stateForResult(\n _core._reactor.getPreviousResult(query),\n );\n }\n\n // Cache the server snapshot to satisfy useSyncExternalStore's requirement\n // that getServerSnapshot returns the same reference across calls.\n const serverSnapshotCacheRef = useRef<\n SideQLLifecycleState<Schema, Q, UseDates>\n >(defaultState as any);\n\n // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe\n // if `subscribe` changes, so we use `useCallback` to memoize the function.\n const subscribe = useCallback(\n (cb) => {\n // Update the ref when the query changes to avoid showing stale data\n resultCacheRef.current = stateForResult(\n _core._reactor.getPreviousResult(query),\n );\n // before the subscribeQuery is connected and calls it's own callback,\n // we might have data in the store via SSR, we need to notify useSyncExternalStore that\n // the data has changed.\n cb();\n\n // Don't subscribe if query is null\n if (!query) {\n const unsubscribe = () => {};\n return unsubscribe;\n }\n\n const unsubscribe = _core.subscribeQuery<Q, UseDates>(query, (result) => {\n resultCacheRef.current = {\n isLoading: !Boolean(result),\n // @ts-expect-error: ts thinks this will always be overwritten\n data: undefined,\n // @ts-expect-error: ts thinks this will always be overwritten\n pageInfo: undefined,\n // @ts-expect-error: ts thinks this will always be overwritten\n error: undefined,\n ...result,\n };\n\n cb();\n });\n\n return unsubscribe;\n },\n // Build a new subscribe function if the query changes\n [queryHash],\n );\n\n const state = useSyncExternalStore<SideQLLifecycleState<Schema, Q, UseDates>>(\n subscribe,\n () => resultCacheRef.current,\n () => {\n if (serverSnapshot && serverSnapshotCacheRef.current === defaultState) {\n const result = serverSnapshot();\n if (result) {\n serverSnapshotCacheRef.current = result;\n }\n }\n\n return serverSnapshotCacheRef.current;\n },\n );\n\n return { state, query, queryHash };\n}\n"]}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { Auth, Storage, type AuthState, type User, type ConnectionStatus, type AppStatusState, type TransactionChunk, type RoomSchemaShape, type
|
|
1
|
+
import { Auth, Storage, type AuthState, type User, type ConnectionStatus, type AppStatusState, type TransactionChunk, type RoomSchemaShape, type SideQLOptions, type SidekickConfig, type PageInfoResponse, SidekickCoreDatabase, SideQLLifecycleState, SideQLResponse, RoomsOf, SidekickSchemaDef, ISidekickDatabase, ValidQuery, Streams } from '@sidekickdb/core';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
3
|
import { type InfiniteQueryResult } from './useInfiniteQuerySubscription.ts';
|
|
4
|
-
import {
|
|
5
|
-
export default abstract class
|
|
4
|
+
import { SidekickReactRoom } from './SidekickReactRoom.ts';
|
|
5
|
+
export default abstract class SidekickReactAbstractDatabase<Schema extends SidekickSchemaDef<any, any, any>, UseDates extends boolean = false, Config extends SidekickConfig<Schema, boolean> = SidekickConfig<Schema, UseDates>, Rooms extends RoomSchemaShape = RoomsOf<Schema>> implements ISidekickDatabase<Schema> {
|
|
6
6
|
tx: import("@sidekickdb/core").TxChunk<Schema>;
|
|
7
7
|
auth: Auth;
|
|
8
8
|
storage: Storage;
|
|
9
9
|
streams: Streams;
|
|
10
|
-
core:
|
|
10
|
+
core: SidekickCoreDatabase<Schema, UseDates>;
|
|
11
11
|
/** @deprecated use `core` instead */
|
|
12
|
-
_core:
|
|
12
|
+
_core: SidekickCoreDatabase<Schema, UseDates>;
|
|
13
13
|
static Store?: any;
|
|
14
14
|
static NetworkListener?: any;
|
|
15
15
|
static EventSourceImpl?: any;
|
|
16
|
-
constructor(config: Omit<
|
|
16
|
+
constructor(config: Omit<SidekickConfig<Schema, UseDates>, 'useDateObjects'> & {
|
|
17
17
|
useDateObjects?: UseDates;
|
|
18
18
|
}, versions?: {
|
|
19
19
|
[key: string]: string;
|
|
@@ -43,7 +43,7 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
43
43
|
/**
|
|
44
44
|
* Obtain a handle to a room, which allows you to listen to topics and presence data
|
|
45
45
|
*
|
|
46
|
-
* If you don't provide a `type` or `id`,
|
|
46
|
+
* If you don't provide a `type` or `id`, SidekickDB will default to `_defaultRoomType` and `_defaultRoomId`
|
|
47
47
|
* as the room type and id, respectively.
|
|
48
48
|
*
|
|
49
49
|
* @see https://sidekickdb.co/docs/presence-and-topics
|
|
@@ -52,7 +52,7 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
52
52
|
* const room = db.room('chat', roomId);
|
|
53
53
|
* const { peers } = db.rooms.usePresence(room);
|
|
54
54
|
*/
|
|
55
|
-
room<RoomType extends string & keyof Rooms>(type?: RoomType, id?: string):
|
|
55
|
+
room<RoomType extends string & keyof Rooms>(type?: RoomType, id?: string): SidekickReactRoom<Schema, Rooms, RoomType>;
|
|
56
56
|
/**
|
|
57
57
|
* Hooks for working with rooms
|
|
58
58
|
*
|
|
@@ -65,16 +65,16 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
65
65
|
* // ...
|
|
66
66
|
*/
|
|
67
67
|
rooms: {
|
|
68
|
-
useTopicEffect: typeof import("./
|
|
69
|
-
usePublishTopic: typeof import("./
|
|
70
|
-
usePresence: typeof import("./
|
|
71
|
-
useSyncPresence: typeof import("./
|
|
72
|
-
useTypingIndicator: typeof import("./
|
|
68
|
+
useTopicEffect: typeof import("./SidekickReactRoom.ts").useTopicEffect;
|
|
69
|
+
usePublishTopic: typeof import("./SidekickReactRoom.ts").usePublishTopic;
|
|
70
|
+
usePresence: typeof import("./SidekickReactRoom.ts").usePresence;
|
|
71
|
+
useSyncPresence: typeof import("./SidekickReactRoom.ts").useSyncPresence;
|
|
72
|
+
useTypingIndicator: typeof import("./SidekickReactRoom.ts").useTypingIndicator;
|
|
73
73
|
};
|
|
74
74
|
/**
|
|
75
75
|
* Use this to write data! You can create, update, delete, and link objects
|
|
76
76
|
*
|
|
77
|
-
* @see https://sidekickdb.co/docs/
|
|
77
|
+
* @see https://sidekickdb.co/docs/sideml
|
|
78
78
|
*
|
|
79
79
|
* @example
|
|
80
80
|
* // Create a new object in the `goals` namespace
|
|
@@ -98,7 +98,7 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
98
98
|
/**
|
|
99
99
|
* Use this to query your data!
|
|
100
100
|
*
|
|
101
|
-
* @see https://sidekickdb.co/docs/
|
|
101
|
+
* @see https://sidekickdb.co/docs/sideql
|
|
102
102
|
*
|
|
103
103
|
* @example
|
|
104
104
|
* // listen to all goals
|
|
@@ -119,7 +119,7 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
119
119
|
* auth.user ? { goals: {} } : null,
|
|
120
120
|
* );
|
|
121
121
|
*/
|
|
122
|
-
useQuery: <Q extends ValidQuery<Q, Schema>>(query: null | Q, opts?:
|
|
122
|
+
useQuery: <Q extends ValidQuery<Q, Schema>>(query: null | Q, opts?: SideQLOptions) => SideQLLifecycleState<Schema, Q, UseDates>;
|
|
123
123
|
/**
|
|
124
124
|
* Listen for the logged in state. This is useful
|
|
125
125
|
* for deciding when to show a login screen.
|
|
@@ -177,10 +177,10 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
177
177
|
*/
|
|
178
178
|
getAuth(): Promise<User | null>;
|
|
179
179
|
/**
|
|
180
|
-
* Listen for connection status changes to
|
|
180
|
+
* Listen for connection status changes to SidekickDB. Use this for things like
|
|
181
181
|
* showing connection state to users
|
|
182
182
|
*
|
|
183
|
-
* @see https://sidekickdb.co/docs/patterns#connection-status
|
|
183
|
+
* @see https://sidekickdb.co/docs/patterns#listen-to-sidekick-db-connection-status
|
|
184
184
|
* @example
|
|
185
185
|
* function App() {
|
|
186
186
|
* const status = db.useConnectionStatus()
|
|
@@ -218,15 +218,15 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
218
218
|
* Because we want to avoid stale data, this method will throw an error
|
|
219
219
|
* if the user is offline or there is no active connection to the server.
|
|
220
220
|
*
|
|
221
|
-
* @see https://sidekickdb.co/docs/
|
|
221
|
+
* @see https://sidekickdb.co/docs/sideql
|
|
222
222
|
*
|
|
223
223
|
* @example
|
|
224
224
|
*
|
|
225
225
|
* const resp = await db.queryOnce({ goals: {} });
|
|
226
226
|
* console.log(resp.data.goals)
|
|
227
227
|
*/
|
|
228
|
-
queryOnce: <Q extends ValidQuery<Q, Schema>>(query: Q, opts?:
|
|
229
|
-
data:
|
|
228
|
+
queryOnce: <Q extends ValidQuery<Q, Schema>>(query: Q, opts?: SideQLOptions) => Promise<{
|
|
229
|
+
data: SideQLResponse<Schema, Q, UseDates>;
|
|
230
230
|
pageInfo: PageInfoResponse<Q>;
|
|
231
231
|
}>;
|
|
232
232
|
/**
|
|
@@ -252,7 +252,7 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
252
252
|
* },
|
|
253
253
|
* });
|
|
254
254
|
*/
|
|
255
|
-
useInfiniteQuery: <Q extends ValidQuery<Q, Schema>>(query: Q, opts?:
|
|
255
|
+
useInfiniteQuery: <Q extends ValidQuery<Q, Schema>>(query: Q, opts?: SideQLOptions) => InfiniteQueryResult<Schema, Q, UseDates>;
|
|
256
256
|
/**
|
|
257
257
|
* Only render children if the user is signed in.
|
|
258
258
|
* @see https://sidekickdb.co/docs/auth
|
|
@@ -280,4 +280,4 @@ export default abstract class InstantReactAbstractDatabase<Schema extends Instan
|
|
|
280
280
|
children: ReactNode;
|
|
281
281
|
}>;
|
|
282
282
|
}
|
|
283
|
-
//# sourceMappingURL=
|
|
283
|
+
//# sourceMappingURL=SidekickReactAbstractDatabase.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SidekickReactAbstractDatabase.d.ts","sourceRoot":"","sources":["../../src/SidekickReactAbstractDatabase.tsx"],"names":[],"mappings":"AACA,OAAO,EAEL,IAAI,EACJ,OAAO,EAEP,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,oBAAoB,EAEpB,oBAAoB,EACpB,cAAc,EACd,OAAO,EACP,iBAAiB,EACjB,iBAAiB,EAEjB,UAAU,EACV,OAAO,EACR,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,SAAS,EAMV,MAAM,OAAO,CAAC;AAEf,OAAO,EACL,KAAK,mBAAmB,EAEzB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAS,MAAM,wBAAwB,CAAC;AAalE,MAAM,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CAAC,EACR,OAAO,aAAa,KACnB,mBAAmB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAOzC;IAEF;;;;;;;;;OASG;IACH,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC;QACjB,QAAQ,EAAE,SAAS,CAAC;KACrB,CAAC,CAKA;IAEF;;;;;;;;;OASG;IACH,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC;QAClB,QAAQ,EAAE,SAAS,CAAC;KACrB,CAAC,CAIA;CACH"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import { txInit, init as core_init,
|
|
3
|
+
import { txInit, init as core_init, SidekickError, } from '@sidekickdb/core';
|
|
4
4
|
import { useCallback, useEffect, useRef, useState, useSyncExternalStore, } from 'react';
|
|
5
5
|
import { useQueryInternal } from "./useQuery.js";
|
|
6
6
|
import { useInfiniteQuerySubscription, } from "./useInfiniteQuerySubscription.js";
|
|
7
|
-
import {
|
|
7
|
+
import { SidekickReactRoom, rooms } from "./SidekickReactRoom.js";
|
|
8
8
|
const defaultAuthState = {
|
|
9
9
|
isLoading: true,
|
|
10
10
|
user: undefined,
|
|
@@ -14,7 +14,7 @@ const defaultAppStatusState = {
|
|
|
14
14
|
isLoading: true,
|
|
15
15
|
isReadOnly: undefined,
|
|
16
16
|
};
|
|
17
|
-
export default class
|
|
17
|
+
export default class SidekickReactAbstractDatabase {
|
|
18
18
|
tx = txInit();
|
|
19
19
|
auth;
|
|
20
20
|
storage;
|
|
@@ -79,7 +79,7 @@ export default class InstantReactAbstractDatabase {
|
|
|
79
79
|
/**
|
|
80
80
|
* Obtain a handle to a room, which allows you to listen to topics and presence data
|
|
81
81
|
*
|
|
82
|
-
* If you don't provide a `type` or `id`,
|
|
82
|
+
* If you don't provide a `type` or `id`, SidekickDB will default to `_defaultRoomType` and `_defaultRoomId`
|
|
83
83
|
* as the room type and id, respectively.
|
|
84
84
|
*
|
|
85
85
|
* @see https://sidekickdb.co/docs/presence-and-topics
|
|
@@ -89,7 +89,7 @@ export default class InstantReactAbstractDatabase {
|
|
|
89
89
|
* const { peers } = db.rooms.usePresence(room);
|
|
90
90
|
*/
|
|
91
91
|
room(type = '_defaultRoomType', id = '_defaultRoomId') {
|
|
92
|
-
return new
|
|
92
|
+
return new SidekickReactRoom(this.core, type, id);
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
95
95
|
* Hooks for working with rooms
|
|
@@ -106,7 +106,7 @@ export default class InstantReactAbstractDatabase {
|
|
|
106
106
|
/**
|
|
107
107
|
* Use this to write data! You can create, update, delete, and link objects
|
|
108
108
|
*
|
|
109
|
-
* @see https://sidekickdb.co/docs/
|
|
109
|
+
* @see https://sidekickdb.co/docs/sideml
|
|
110
110
|
*
|
|
111
111
|
* @example
|
|
112
112
|
* // Create a new object in the `goals` namespace
|
|
@@ -132,7 +132,7 @@ export default class InstantReactAbstractDatabase {
|
|
|
132
132
|
/**
|
|
133
133
|
* Use this to query your data!
|
|
134
134
|
*
|
|
135
|
-
* @see https://sidekickdb.co/docs/
|
|
135
|
+
* @see https://sidekickdb.co/docs/sideql
|
|
136
136
|
*
|
|
137
137
|
* @example
|
|
138
138
|
* // listen to all goals
|
|
@@ -223,7 +223,7 @@ export default class InstantReactAbstractDatabase {
|
|
|
223
223
|
useUser = () => {
|
|
224
224
|
const { user } = this.useAuth();
|
|
225
225
|
if (!user) {
|
|
226
|
-
throw new
|
|
226
|
+
throw new SidekickError('useUser must be used within an auth-protected route');
|
|
227
227
|
}
|
|
228
228
|
return user;
|
|
229
229
|
};
|
|
@@ -241,10 +241,10 @@ export default class InstantReactAbstractDatabase {
|
|
|
241
241
|
return this.core.getAuth();
|
|
242
242
|
}
|
|
243
243
|
/**
|
|
244
|
-
* Listen for connection status changes to
|
|
244
|
+
* Listen for connection status changes to SidekickDB. Use this for things like
|
|
245
245
|
* showing connection state to users
|
|
246
246
|
*
|
|
247
|
-
* @see https://sidekickdb.co/docs/patterns#connection-status
|
|
247
|
+
* @see https://sidekickdb.co/docs/patterns#listen-to-sidekick-db-connection-status
|
|
248
248
|
* @example
|
|
249
249
|
* function App() {
|
|
250
250
|
* const status = db.useConnectionStatus()
|
|
@@ -310,7 +310,7 @@ export default class InstantReactAbstractDatabase {
|
|
|
310
310
|
* Because we want to avoid stale data, this method will throw an error
|
|
311
311
|
* if the user is offline or there is no active connection to the server.
|
|
312
312
|
*
|
|
313
|
-
* @see https://sidekickdb.co/docs/
|
|
313
|
+
* @see https://sidekickdb.co/docs/sideql
|
|
314
314
|
*
|
|
315
315
|
* @example
|
|
316
316
|
*
|
|
@@ -384,4 +384,4 @@ export default class InstantReactAbstractDatabase {
|
|
|
384
384
|
return _jsx(_Fragment, { children: children });
|
|
385
385
|
};
|
|
386
386
|
}
|
|
387
|
-
//# sourceMappingURL=
|
|
387
|
+
//# sourceMappingURL=SidekickReactAbstractDatabase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SidekickReactAbstractDatabase.js","sourceRoot":"","sources":["../../src/SidekickReactAbstractDatabase.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AACb,OAAO,EAIL,MAAM,EAWN,IAAI,IAAI,SAAS,EAMjB,aAAa,GAGd,MAAM,kBAAkB,CAAC;AAC1B,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,iBAAiB,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAElE,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,SAAS;CACjB,CAAC;AAEF,MAAM,qBAAqB,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AAE;QACH,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC,CAAC;IAEF;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,gBAAgB,GAAG,CACjB,KAAQ,EACR,IAAoB,EACsB,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 AppStatusState,\n type TransactionChunk,\n type RoomSchemaShape,\n type SideQLOptions,\n type SidekickConfig,\n type PageInfoResponse,\n SidekickCoreDatabase,\n init as core_init,\n SideQLLifecycleState,\n SideQLResponse,\n RoomsOf,\n SidekickSchemaDef,\n ISidekickDatabase,\n SidekickError,\n ValidQuery,\n Streams,\n} from '@sidekickdb/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 { SidekickReactRoom, rooms } from './SidekickReactRoom.ts';\n\nconst defaultAuthState = {\n isLoading: true,\n user: undefined,\n error: undefined,\n};\n\nconst defaultAppStatusState: AppStatusState = {\n isLoading: true,\n isReadOnly: undefined,\n};\n\nexport default abstract class SidekickReactAbstractDatabase<\n // need to pull this schema out to another generic for query params, not sure why\n Schema extends SidekickSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n Config extends SidekickConfig<Schema, boolean> = SidekickConfig<\n Schema,\n UseDates\n >,\n Rooms extends RoomSchemaShape = RoomsOf<Schema>,\n> implements ISidekickDatabase<Schema>\n{\n public tx = txInit<Schema>();\n\n public auth: Auth;\n public storage: Storage;\n public streams: Streams;\n public core: SidekickCoreDatabase<Schema, UseDates>;\n\n /** @deprecated use `core` instead */\n public _core: SidekickCoreDatabase<Schema, UseDates>;\n\n static Store?: any;\n static NetworkListener?: any;\n static EventSourceImpl?: any;\n\n constructor(\n config: Omit<SidekickConfig<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`, SidekickDB will default to `_defaultRoomType` and `_defaultRoomId`\n * as the room type and id, respectively.\n *\n * @see https://sidekickdb.co/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 SidekickReactRoom<Schema, Rooms, RoomType>(this.core, type, id);\n }\n\n /**\n * Hooks for working with rooms\n *\n * @see https://sidekickdb.co/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://sidekickdb.co/docs/sideml\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://sidekickdb.co/docs/sideql\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?: SideQLOptions,\n ): SideQLLifecycleState<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://sidekickdb.co/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://sidekickdb.co/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 SidekickError(\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://sidekickdb.co/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 SidekickDB. Use this for things like\n * showing connection state to users\n *\n * @see https://sidekickdb.co/docs/patterns#listen-to-sidekick-db-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 * Observe the app's maintenance-mode state. While read-only, reads and\n * live queries keep working but writes are rejected.\n *\n * @example\n * function App() {\n * const { isLoading, isReadOnly } = db.useAppStatus()\n * if (isLoading) return null\n * if (isReadOnly) return <MaintenanceBanner />\n * // ...\n * }\n */\n useAppStatus = (): AppStatusState => {\n const stateRef = useRef<AppStatusState>(this.core._reactor._appStatusState);\n\n const subscribe = useCallback((cb: () => void) => {\n return this.core.subscribeAppStatus((state) => {\n if (\n state.isLoading !== stateRef.current.isLoading ||\n state.isReadOnly !== stateRef.current.isReadOnly\n ) {\n stateRef.current = state;\n cb();\n }\n });\n }, []);\n\n const state = useSyncExternalStore<AppStatusState>(\n subscribe,\n () => stateRef.current,\n () => defaultAppStatusState,\n );\n\n return state;\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://sidekickdb.co/docs/sideql\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?: SideQLOptions,\n ): Promise<{\n data: SideQLResponse<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?: SideQLOptions,\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://sidekickdb.co/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://sidekickdb.co/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,4 +1,4 @@
|
|
|
1
|
-
import { type PresenceOpts, type PresenceResponse, type RoomSchemaShape,
|
|
1
|
+
import { type PresenceOpts, type PresenceResponse, type RoomSchemaShape, SidekickCoreDatabase, SidekickSchemaDef } from '@sidekickdb/core';
|
|
2
2
|
import { KeyboardEvent } from 'react';
|
|
3
3
|
export type PresenceHandle<PresenceShape, Keys extends keyof PresenceShape> = PresenceResponse<PresenceShape, Keys> & {
|
|
4
4
|
publishPresence: (data: Partial<PresenceShape>) => void;
|
|
@@ -30,7 +30,7 @@ export declare const defaultActivityStopTimeout = 1000;
|
|
|
30
30
|
* // ...
|
|
31
31
|
* }
|
|
32
32
|
*/
|
|
33
|
-
export declare function useTopicEffect<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema, TopicType extends keyof RoomSchema[RoomType]['topics']>(room:
|
|
33
|
+
export declare function useTopicEffect<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema, TopicType extends keyof RoomSchema[RoomType]['topics']>(room: SidekickReactRoom<any, RoomSchema, RoomType>, topic: TopicType, onEvent: (event: RoomSchema[RoomType]['topics'][TopicType], peer: RoomSchema[RoomType]['presence']) => any): void;
|
|
34
34
|
/**
|
|
35
35
|
* Broadcast an event to a room.
|
|
36
36
|
*
|
|
@@ -46,7 +46,7 @@ export declare function useTopicEffect<RoomSchema extends RoomSchemaShape, RoomT
|
|
|
46
46
|
* }
|
|
47
47
|
*
|
|
48
48
|
*/
|
|
49
|
-
export declare function usePublishTopic<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema, TopicType extends keyof RoomSchema[RoomType]['topics']>(room:
|
|
49
|
+
export declare function usePublishTopic<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema, TopicType extends keyof RoomSchema[RoomType]['topics']>(room: SidekickReactRoom<any, RoomSchema, RoomType>, topic: TopicType): (data: RoomSchema[RoomType]['topics'][TopicType]) => void;
|
|
50
50
|
/**
|
|
51
51
|
* Listen for peer's presence data in a room, and publish the current user's presence.
|
|
52
52
|
*
|
|
@@ -61,7 +61,7 @@ export declare function usePublishTopic<RoomSchema extends RoomSchemaShape, Room
|
|
|
61
61
|
* // ...
|
|
62
62
|
* }
|
|
63
63
|
*/
|
|
64
|
-
export declare function usePresence<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema, Keys extends keyof RoomSchema[RoomType]['presence']>(room:
|
|
64
|
+
export declare function usePresence<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema, Keys extends keyof RoomSchema[RoomType]['presence']>(room: SidekickReactRoom<any, RoomSchema, RoomType>, opts?: PresenceOpts<RoomSchema[RoomType]['presence'], Keys>): PresenceHandle<RoomSchema[RoomType]['presence'], Keys>;
|
|
65
65
|
/**
|
|
66
66
|
* Publishes presence data to a room
|
|
67
67
|
*
|
|
@@ -72,7 +72,7 @@ export declare function usePresence<RoomSchema extends RoomSchemaShape, RoomType
|
|
|
72
72
|
* db.rooms.useSyncPresence(room, { nickname });
|
|
73
73
|
* }
|
|
74
74
|
*/
|
|
75
|
-
export declare function useSyncPresence<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema>(room:
|
|
75
|
+
export declare function useSyncPresence<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema>(room: SidekickReactRoom<any, RoomSchema, RoomType>, data: Partial<RoomSchema[RoomType]['presence']>, deps?: any[]): void;
|
|
76
76
|
/**
|
|
77
77
|
* Manage typing indicator state
|
|
78
78
|
*
|
|
@@ -89,7 +89,7 @@ export declare function useSyncPresence<RoomSchema extends RoomSchemaShape, Room
|
|
|
89
89
|
* return <input {...inputProps} />;
|
|
90
90
|
* }
|
|
91
91
|
*/
|
|
92
|
-
export declare function useTypingIndicator<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema>(room:
|
|
92
|
+
export declare function useTypingIndicator<RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema>(room: SidekickReactRoom<any, RoomSchema, RoomType>, inputName: string, opts?: TypingIndicatorOpts): TypingIndicatorHandle<RoomSchema[RoomType]['presence']>;
|
|
93
93
|
export declare const rooms: {
|
|
94
94
|
useTopicEffect: typeof useTopicEffect;
|
|
95
95
|
usePublishTopic: typeof usePublishTopic;
|
|
@@ -97,13 +97,13 @@ export declare const rooms: {
|
|
|
97
97
|
useSyncPresence: typeof useSyncPresence;
|
|
98
98
|
useTypingIndicator: typeof useTypingIndicator;
|
|
99
99
|
};
|
|
100
|
-
export declare class
|
|
101
|
-
core:
|
|
100
|
+
export declare class SidekickReactRoom<Schema extends SidekickSchemaDef<any, any, any>, RoomSchema extends RoomSchemaShape, RoomType extends string & keyof RoomSchema> {
|
|
101
|
+
core: SidekickCoreDatabase<Schema, boolean>;
|
|
102
102
|
/** @deprecated use `core` instead */
|
|
103
|
-
_core:
|
|
103
|
+
_core: SidekickCoreDatabase<Schema, boolean>;
|
|
104
104
|
type: RoomType;
|
|
105
105
|
id: string;
|
|
106
|
-
constructor(core:
|
|
106
|
+
constructor(core: SidekickCoreDatabase<Schema, boolean>, type: RoomType, id: string);
|
|
107
107
|
/**
|
|
108
108
|
* @deprecated
|
|
109
109
|
* `db.room(...).useTopicEffect` is deprecated. You can replace it with `db.rooms.useTopicEffect`.
|
|
@@ -180,4 +180,4 @@ export declare class InstantReactRoom<Schema extends InstantSchemaDef<any, any,
|
|
|
180
180
|
*/
|
|
181
181
|
useTypingIndicator: (inputName: string, opts?: TypingIndicatorOpts) => TypingIndicatorHandle<RoomSchema[RoomType]["presence"]>;
|
|
182
182
|
}
|
|
183
|
-
//# sourceMappingURL=
|
|
183
|
+
//# sourceMappingURL=SidekickReactRoom.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SidekickReactRoom.d.ts","sourceRoot":"","sources":["../../src/SidekickReactRoom.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,oBAAoB,EACpB,iBAAiB,EAClB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACL,aAAa,EAMd,MAAM,OAAO,CAAC;AAIf,MAAM,MAAM,cAAc,CACxB,aAAa,EACb,IAAI,SAAS,MAAM,aAAa,IAC9B,gBAAgB,CAAC,aAAa,EAAE,IAAI,CAAC,GAAG;IAC1C,eAAe,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,KAAK,IAAI,CAAC;CACzD,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,qBAAqB,CAAC,aAAa,IAAI;IACjD,MAAM,EAAE,aAAa,EAAE,CAAC;IACxB,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;IACjC,UAAU,EAAE;QACV,SAAS,EAAE,CAAC,CAAC,EAAE,aAAa,KAAK,IAAI,CAAC;QACt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
|
|
|
180
180
|
};
|
|
181
181
|
// ------------
|
|
182
182
|
// Class
|
|
183
|
-
export class
|
|
183
|
+
export class SidekickReactRoom {
|
|
184
184
|
core;
|
|
185
185
|
/** @deprecated use `core` instead */
|
|
186
186
|
_core;
|
|
@@ -278,4 +278,4 @@ export class InstantReactRoom {
|
|
|
278
278
|
return rooms.useTypingIndicator(this, inputName, opts);
|
|
279
279
|
};
|
|
280
280
|
}
|
|
281
|
-
//# sourceMappingURL=
|
|
281
|
+
//# sourceMappingURL=SidekickReactRoom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SidekickReactRoom.js","sourceRoot":"","sources":["../../src/SidekickReactRoom.ts"],"names":[],"mappings":"AASA,OAAO,EAEL,WAAW,EACX,SAAS,EACT,OAAO,EACP,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAC;AAEf,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAyB7C,MAAM,CAAC,MAAM,0BAA0B,GAAG,KAAK,CAAC;AAEhD,SAAS;AACT,SAAS;AAET;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,cAAc,CAK5B,IAAkD,EAClD,KAAgB,EAChB,OAGQ;IAER,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IACnC,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAC7C,IAAI,CAAC,IAAI,EACT,IAAI,CAAC,EAAE,EACP,KAAK,EACL,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;YACd,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC,CACF,CAAC;QAEF,OAAO,KAAK,CAAC;IACf,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC;AACvB,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EAAE,CAAS;IAEX,YACE,IAA2C,EAC3C,IAAc,EACd,EAAU;QAEV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACf,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,cAAc,GAAG,CACf,KAAgB,EAChB,OAGQ,EACF,EAAE;QACR,KAAK,CAAC,cAAc,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC,CAAC;IAEF;;;;;;;;;;;;;OAaG;IACH,eAAe,GAAG,CAChB,KAAY,EAC6C,EAAE;QAC3D,OAAO,KAAK,CAAC,eAAe,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC,CAAC;IAEF;;;;;;;;;;;;;OAaG;IACH,WAAW,GAAG,CACZ,OAA6D,EAAE,EACP,EAAE;QAC1D,OAAO,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACvC,CAAC,CAAC;IAEF;;;;;;;;;;;;;OAaG;IACH,eAAe,GAAG,CAChB,IAA+C,EAC/C,IAAY,EACN,EAAE;QACR,OAAO,KAAK,CAAC,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACjD,CAAC,CAAC;IAEF;;;;;;;;;;;;;OAaG;IACH,kBAAkB,GAAG,CACnB,SAAiB,EACjB,OAA4B,EAAE,EAC2B,EAAE;QAC3D,OAAO,KAAK,CAAC,kBAAkB,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IACzD,CAAC,CAAC;CACH","sourcesContent":["import {\n // types\n type PresenceOpts,\n type PresenceResponse,\n type RoomSchemaShape,\n SidekickCoreDatabase,\n SidekickSchemaDef,\n} from '@sidekickdb/core';\n\nimport {\n KeyboardEvent,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\n\nimport { useTimeout } from './useTimeout.ts';\n\nexport type PresenceHandle<\n PresenceShape,\n Keys extends keyof PresenceShape,\n> = PresenceResponse<PresenceShape, Keys> & {\n publishPresence: (data: Partial<PresenceShape>) => void;\n};\n\nexport type TypingIndicatorOpts = {\n timeout?: number | null;\n stopOnEnter?: boolean;\n // Perf opt - `active` will always be an empty array\n writeOnly?: boolean;\n};\n\nexport type TypingIndicatorHandle<PresenceShape> = {\n active: PresenceShape[];\n setActive(active: boolean): void;\n inputProps: {\n onKeyDown: (e: KeyboardEvent) => void;\n onBlur: () => void;\n };\n};\n\nexport const defaultActivityStopTimeout = 1_000;\n\n// ------\n// Topics\n\n/**\n * Listen for broadcasted events given a room and topic.\n *\n * @see https://sidekickdb.co/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const room = db.room('chats', roomId);\n * db.rooms.useTopicEffect(room, 'emoji', (message, peer) => {\n * console.log(peer.name, 'sent', message);\n * });\n * // ...\n * }\n */\nexport function useTopicEffect<\n RoomSchema extends RoomSchemaShape,\n RoomType extends string & keyof RoomSchema,\n TopicType extends keyof RoomSchema[RoomType]['topics'],\n>(\n room: SidekickReactRoom<any, RoomSchema, RoomType>,\n topic: TopicType,\n onEvent: (\n event: RoomSchema[RoomType]['topics'][TopicType],\n peer: RoomSchema[RoomType]['presence'],\n ) => any,\n): void {\n const onEventRef = useRef(onEvent);\n onEventRef.current = onEvent;\n\n useEffect(() => {\n const unsub = room.core._reactor.subscribeTopic(\n room.type,\n room.id,\n topic,\n (event, peer) => {\n onEventRef.current(event, peer);\n },\n );\n\n return unsub;\n }, [room.id, topic]);\n}\n\n/**\n * Broadcast an event to a room.\n *\n * @see https://sidekickdb.co/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const room = db.room('chat', roomId);\n * const publishTopic = db.rooms.usePublishTopic(room, \"emoji\");\n *\n * return (\n * <button onClick={() => publishTopic({ emoji: \"🔥\" })}>Send emoji</button>\n * );\n * }\n *\n */\nexport function usePublishTopic<\n RoomSchema extends RoomSchemaShape,\n RoomType extends string & keyof RoomSchema,\n TopicType extends keyof RoomSchema[RoomType]['topics'],\n>(\n room: SidekickReactRoom<any, RoomSchema, RoomType>,\n topic: TopicType,\n): (data: RoomSchema[RoomType]['topics'][TopicType]) => void {\n useEffect(() => room.core._reactor.joinRoom(room.type, room.id), [room.id]);\n\n const publishTopic = useCallback(\n (data) => {\n room.core._reactor.publishTopic({\n roomType: room.type,\n roomId: room.id,\n topic,\n data,\n });\n },\n [room.id, topic],\n );\n\n return publishTopic;\n}\n\n// ---------\n// Presence\n\n/**\n * Listen for peer's presence data in a room, and publish the current user's presence.\n *\n * @see https://sidekickdb.co/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const {\n * peers,\n * publishPresence\n * } = db.room(roomType, roomId).usePresence({ keys: [\"name\", \"avatar\"] });\n *\n * // ...\n * }\n */\nexport function usePresence<\n RoomSchema extends RoomSchemaShape,\n RoomType extends string & keyof RoomSchema,\n Keys extends keyof RoomSchema[RoomType]['presence'],\n>(\n room: SidekickReactRoom<any, RoomSchema, RoomType>,\n opts: PresenceOpts<RoomSchema[RoomType]['presence'], Keys> = {},\n): PresenceHandle<RoomSchema[RoomType]['presence'], Keys> {\n const [state, setState] = useState<\n PresenceResponse<RoomSchema[RoomType]['presence'], Keys>\n >(() => {\n return (\n room.core._reactor.getPresence(room.type, room.id, opts) ?? {\n peers: {},\n isLoading: true,\n }\n );\n });\n\n useEffect(() => {\n const unsub = room.core._reactor.subscribePresence(\n room.type,\n room.id,\n opts,\n (data) => {\n setState(data);\n },\n );\n\n return unsub;\n }, [room.id, opts.user, opts.peers?.join(), opts.keys?.join()]);\n\n const publishPresence = useCallback(\n (data) => {\n room.core._reactor.publishPresence(room.type, room.id, data);\n },\n [room.type, room.id],\n );\n const ret = useMemo(() => {\n return {\n ...state,\n publishPresence,\n };\n }, [state, publishPresence]);\n return ret;\n}\n\n/**\n * Publishes presence data to a room\n *\n * @see https://sidekickdb.co/docs/presence-and-topics\n * @example\n * function App({ roomId, nickname }) {\n * const room = db.room('chat', roomId);\n * db.rooms.useSyncPresence(room, { nickname });\n * }\n */\nexport function useSyncPresence<\n RoomSchema extends RoomSchemaShape,\n RoomType extends string & keyof RoomSchema,\n>(\n room: SidekickReactRoom<any, RoomSchema, RoomType>,\n data: Partial<RoomSchema[RoomType]['presence']>,\n deps?: any[],\n): void {\n useEffect(\n () => room.core._reactor.joinRoom(room.type, room.id, data),\n [room.id],\n );\n useEffect(() => {\n return room.core._reactor.publishPresence(room.type, room.id, data);\n }, [room.type, room.id, deps ?? JSON.stringify(data)]);\n}\n\n// -----------------\n// Typing Indicator\n\n/**\n * Manage typing indicator state\n *\n * @see https://sidekickdb.co/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const room = db.room('chat', roomId);\n * const {\n * active,\n * setActive,\n * inputProps,\n * } = db.rooms.useTypingIndicator(room, \"chat-input\");\n *\n * return <input {...inputProps} />;\n * }\n */\nexport function useTypingIndicator<\n RoomSchema extends RoomSchemaShape,\n RoomType extends string & keyof RoomSchema,\n>(\n room: SidekickReactRoom<any, RoomSchema, RoomType>,\n inputName: string,\n opts: TypingIndicatorOpts = {},\n): TypingIndicatorHandle<RoomSchema[RoomType]['presence']> {\n const timeout = useTimeout();\n\n const observedPresence = rooms.usePresence(room, {\n keys: [inputName] as (keyof RoomSchema[RoomType]['presence'])[],\n });\n\n const active = useMemo(() => {\n const presenceSnapshot = room._core._reactor.getPresence(\n room.type,\n room.id,\n );\n\n return opts?.writeOnly\n ? []\n : Object.values(presenceSnapshot?.peers ?? {}).filter(\n (p) => p[inputName] === true,\n );\n }, [opts?.writeOnly, observedPresence]);\n\n const setActive = useCallback(\n (isActive: boolean) => {\n room.core._reactor.publishPresence(room.type, room.id, {\n [inputName]: isActive,\n } as unknown as Partial<RoomSchema[RoomType]>);\n\n if (!isActive) return;\n\n if (opts?.timeout === null || opts?.timeout === 0) return;\n\n timeout.set(opts?.timeout ?? defaultActivityStopTimeout, () => {\n room.core._reactor.publishPresence(room.type, room.id, {\n [inputName]: null,\n } as Partial<RoomSchema[RoomType]>);\n });\n },\n [room.type, room.id, inputName, opts?.timeout, timeout],\n );\n const onKeyDown = useCallback(\n (e: KeyboardEvent) => {\n const isEnter = opts?.stopOnEnter && e.key === 'Enter';\n const isActive = !isEnter;\n\n setActive(isActive);\n },\n [opts.stopOnEnter, setActive],\n );\n const onBlur = useCallback(() => {\n setActive(false);\n }, [setActive]);\n\n const inputProps = useMemo(() => {\n return { onKeyDown, onBlur };\n }, [onKeyDown, onBlur]);\n\n return {\n active,\n setActive,\n inputProps,\n };\n}\n\n// --------------\n// Hooks\n\nexport const rooms = {\n useTopicEffect,\n usePublishTopic,\n usePresence,\n useSyncPresence,\n useTypingIndicator,\n};\n\n// ------------\n// Class\n\nexport class SidekickReactRoom<\n Schema extends SidekickSchemaDef<any, any, any>,\n RoomSchema extends RoomSchemaShape,\n RoomType extends string & keyof RoomSchema,\n> {\n core: SidekickCoreDatabase<Schema, boolean>;\n /** @deprecated use `core` instead */\n _core: SidekickCoreDatabase<Schema, boolean>;\n type: RoomType;\n id: string;\n\n constructor(\n core: SidekickCoreDatabase<Schema, boolean>,\n type: RoomType,\n id: string,\n ) {\n this.core = core;\n this._core = this.core;\n this.type = type;\n this.id = id;\n }\n\n /**\n * @deprecated\n * `db.room(...).useTopicEffect` is deprecated. You can replace it with `db.rooms.useTopicEffect`.\n *\n * @example\n *\n * // Before\n * const room = db.room('chat', 'room-id');\n * room.useTopicEffect('emoji', (message, peer) => { });\n *\n * // After\n * const room = db.room('chat', 'room-id');\n * db.rooms.useTopicEffect(room, 'emoji', (message, peer) => { });\n */\n useTopicEffect = <TopicType extends keyof RoomSchema[RoomType]['topics']>(\n topic: TopicType,\n onEvent: (\n event: RoomSchema[RoomType]['topics'][TopicType],\n peer: RoomSchema[RoomType]['presence'],\n ) => any,\n ): void => {\n rooms.useTopicEffect(this, topic, onEvent);\n };\n\n /**\n * @deprecated\n * `db.room(...).usePublishTopic` is deprecated. You can replace it with `db.rooms.usePublishTopic`.\n *\n * @example\n *\n * // Before\n * const room = db.room('chat', 'room-id');\n * const publish = room.usePublishTopic('emoji');\n *\n * // After\n * const room = db.room('chat', 'room-id');\n * const publish = db.rooms.usePublishTopic(room, 'emoji');\n */\n usePublishTopic = <Topic extends keyof RoomSchema[RoomType]['topics']>(\n topic: Topic,\n ): ((data: RoomSchema[RoomType]['topics'][Topic]) => void) => {\n return rooms.usePublishTopic(this, topic);\n };\n\n /**\n * @deprecated\n * `db.room(...).usePresence` is deprecated. You can replace it with `db.rooms.usePresence`.\n *\n * @example\n *\n * // Before\n * const room = db.room('chat', 'room-id');\n * const { peers } = room.usePresence({ keys: [\"name\", \"avatar\"] });\n *\n * // After\n * const room = db.room('chat', 'room-id');\n * const { peers } = db.rooms.usePresence(room, { keys: [\"name\", \"avatar\"] });\n */\n usePresence = <Keys extends keyof RoomSchema[RoomType]['presence']>(\n opts: PresenceOpts<RoomSchema[RoomType]['presence'], Keys> = {},\n ): PresenceHandle<RoomSchema[RoomType]['presence'], Keys> => {\n return rooms.usePresence(this, opts);\n };\n\n /**\n * @deprecated\n * `db.room(...).useSyncPresence` is deprecated. You can replace it with `db.rooms.useSyncPresence`.\n *\n * @example\n *\n * // Before\n * const room = db.room('chat', 'room-id');\n * room.useSyncPresence(room, { nickname });\n *\n * // After\n * const room = db.room('chat', 'room-id');\n * db.rooms.useSyncPresence(room, { nickname });\n */\n useSyncPresence = (\n data: Partial<RoomSchema[RoomType]['presence']>,\n deps?: any[],\n ): void => {\n return rooms.useSyncPresence(this, data, deps);\n };\n\n /**\n * @deprecated\n * `db.room(...).useTypingIndicator` is deprecated. You can replace it with `db.rooms.useTypingIndicator`.\n *\n * @example\n *\n * // Before\n * const room = db.room('chat', 'room-id');\n * const typing = room.useTypingIndiactor(room, 'chat-input');\n *\n * // After\n * const room = db.room('chat', 'room-id');\n * const typing = db.rooms.useTypingIndiactor(room, 'chat-input');\n */\n useTypingIndicator = (\n inputName: string,\n opts: TypingIndicatorOpts = {},\n ): TypingIndicatorHandle<RoomSchema[RoomType]['presence']> => {\n return rooms.useTypingIndicator(this, inputName, opts);\n };\n}\n"]}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
1
|
+
import SidekickReactAbstractDatabase from './SidekickReactAbstractDatabase.tsx';
|
|
2
|
+
import type { SidekickReactRoom } from './SidekickReactRoom.ts';
|
|
3
3
|
import { useQueryInternal } from './useQuery.ts';
|
|
4
|
-
export {
|
|
4
|
+
export { SidekickReactAbstractDatabase, type SidekickReactRoom, useQueryInternal, };
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,6BAA6B,MAAM,qCAAqC,CAAC;AAChF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EACL,6BAA6B,EAC7B,KAAK,iBAAiB,EACtB,gBAAgB,GACjB,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import SidekickReactAbstractDatabase from "./SidekickReactAbstractDatabase.js";
|
|
2
2
|
import { useQueryInternal } from "./useQuery.js";
|
|
3
|
-
export {
|
|
3
|
+
export { SidekickReactAbstractDatabase, useQueryInternal, };
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,6BAA6B,MAAM,oCAAqC,CAAC;AAEhF,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EACL,6BAA6B,EAE7B,gBAAgB,GACjB,CAAC","sourcesContent":["import SidekickReactAbstractDatabase from './SidekickReactAbstractDatabase.tsx';\nimport type { SidekickReactRoom } from './SidekickReactRoom.ts';\nimport { useQueryInternal } from './useQuery.ts';\n\nexport {\n SidekickReactAbstractDatabase,\n type SidekickReactRoom,\n useQueryInternal,\n};\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
export type InfiniteQueryResult<Schema extends
|
|
1
|
+
import { type SidekickCoreDatabase, type SidekickSchemaDef, type SideQLOptions, SideQLResponse, ValidQuery } from '@sidekickdb/core';
|
|
2
|
+
export type InfiniteQueryResult<Schema extends SidekickSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = {
|
|
3
3
|
error: {
|
|
4
4
|
message: string;
|
|
5
5
|
};
|
|
@@ -15,14 +15,14 @@ export type InfiniteQueryResult<Schema extends InstantSchemaDef<any, any, any>,
|
|
|
15
15
|
loadNextPage: () => void;
|
|
16
16
|
} | {
|
|
17
17
|
error: undefined;
|
|
18
|
-
data:
|
|
18
|
+
data: SideQLResponse<Schema, Q, UseDates>;
|
|
19
19
|
isLoading: false;
|
|
20
20
|
canLoadNextPage: boolean;
|
|
21
21
|
loadNextPage: () => void;
|
|
22
22
|
};
|
|
23
|
-
export declare function useInfiniteQuerySubscription<Schema extends
|
|
24
|
-
core:
|
|
23
|
+
export declare function useInfiniteQuerySubscription<Schema extends SidekickSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean>({ core, query, opts, }: {
|
|
24
|
+
core: SidekickCoreDatabase<Schema, UseDates>;
|
|
25
25
|
query: Q | null;
|
|
26
|
-
opts?:
|
|
26
|
+
opts?: SideQLOptions;
|
|
27
27
|
}): InfiniteQueryResult<Schema, Q, UseDates>;
|
|
28
28
|
//# sourceMappingURL=useInfiniteQuerySubscription.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useInfiniteQuerySubscription.d.ts","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,
|
|
1
|
+
{"version":3,"file":"useInfiniteQuerySubscription.d.ts","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAClB,cAAc,EACd,UAAU,EAGX,MAAM,kBAAkB,CAAC;AAG1B,MAAM,MAAM,mBAAmB,CAC7B,MAAM,SAAS,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC/C,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,cAAc,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ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,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
|
|
1
|
+
{"version":3,"file":"useInfiniteQuerySubscription.js","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,QAAQ,EACR,+BAA+B,GAChC,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AA6BlE,MAAM,YAAY,GAAG;IACnB,KAAK,EAAE,SAAS;IAChB,IAAI,EAAE,SAAS;IACf,SAAS,EAAE,IAAI;IACf,eAAe,EAAE,KAAK;CACvB,CAAC;AAEF,MAAM,UAAU,4BAA4B,CAI1C,EACA,IAAI,EACJ,KAAK,EACL,IAAI,GAKL;IACC,MAAM,MAAM,GAAG,MAAM,CAAmC,IAAI,CAAC,CAAC;IAE9D,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,+BAA+B,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,MAAM,CAE1B;QACA,GAAG,QAAQ;QACX,SAAS,EAAE,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK;KAC7C,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,EAAc,EAAE,EAAE;QACjB,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;QAEtB,MAAM,YAAY,GAAG,+BAA+B,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QACxE,aAAa,CAAC,OAAO,GAAG;YACtB,GAAG,YAAY;YACf,SAAS,EAAE,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK;SACrD,CAAC;QAEF,EAAE,EAAE,CAAC;QAEL,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;QAClB,CAAC;QAED,MAAM,GAAG,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 SidekickCoreDatabase,\n type SidekickSchemaDef,\n type SideQLOptions,\n SideQLResponse,\n ValidQuery,\n weakHash,\n getInfiniteQueryInitialSnapshot,\n} from '@sidekickdb/core';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\n\nexport type InfiniteQueryResult<\n Schema extends SidekickSchemaDef<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: SideQLResponse<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 SidekickSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n>({\n core,\n query,\n opts,\n}: {\n core: SidekickCoreDatabase<Schema, UseDates>;\n query: Q | null;\n opts?: SideQLOptions;\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\n const nextSnapshot = getInfiniteQueryInitialSnapshot(core, query, opts);\n stateCacheRef.current = {\n ...nextSnapshot,\n isLoading: !nextSnapshot.data && !nextSnapshot.error,\n };\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"]}
|
package/dist/esm/useQuery.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
export declare function useQueryInternal<Q extends ValidQuery<Q, Schema>, Schema extends
|
|
3
|
-
state:
|
|
1
|
+
import { type SideQLOptions, SidekickCoreDatabase, SideQLLifecycleState, SidekickSchemaDef, ValidQuery } from '@sidekickdb/core';
|
|
2
|
+
export declare function useQueryInternal<Q extends ValidQuery<Q, Schema>, Schema extends SidekickSchemaDef<any, any, any>, UseDates extends boolean>(_core: SidekickCoreDatabase<Schema, UseDates>, _query: null | Q, _opts?: SideQLOptions, serverSnapshot?: () => SideQLLifecycleState<Schema, Q, UseDates> | undefined): {
|
|
3
|
+
state: SideQLLifecycleState<Schema, Q, UseDates>;
|
|
4
4
|
query: any;
|
|
5
5
|
queryHash: string;
|
|
6
6
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,aAAa,EAClB,oBAAoB,EACpB,oBAAoB,EACpB,iBAAiB,EACjB,UAAU,EACX,MAAM,kBAAkB,CAAC;AAoB1B,wBAAgB,gBAAgB,CAC9B,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,MAAM,SAAS,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC/C,QAAQ,SAAS,OAAO,EAExB,KAAK,EAAE,oBAAoB,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC7C,MAAM,EAAE,IAAI,GAAG,CAAC,EAChB,KAAK,CAAC,EAAE,aAAa,EACrB,cAAc,CAAC,EAAE,MAAM,oBAAoB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,SAAS,GAC3E;IACD,KAAK,EAAE,oBAAoB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IACjD,KAAK,EAAE,GAAG,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;CACnB,CAsFA"}
|