@instantdb/react-common 0.22.15-experimental.react-common.18536829600.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/.tshy/build.json +8 -0
  2. package/.tshy/commonjs.json +16 -0
  3. package/.tshy/esm.json +15 -0
  4. package/README.md +53 -0
  5. package/dist/commonjs/InstantReactAbstractDatabase.d.ts +241 -0
  6. package/dist/commonjs/InstantReactAbstractDatabase.d.ts.map +1 -0
  7. package/dist/commonjs/InstantReactAbstractDatabase.js +323 -0
  8. package/dist/commonjs/InstantReactAbstractDatabase.js.map +1 -0
  9. package/dist/commonjs/InstantReactRoom.d.ts +183 -0
  10. package/dist/commonjs/InstantReactRoom.d.ts.map +1 -0
  11. package/dist/commonjs/InstantReactRoom.js +284 -0
  12. package/dist/commonjs/InstantReactRoom.js.map +1 -0
  13. package/dist/commonjs/index.d.ts +4 -0
  14. package/dist/commonjs/index.d.ts.map +1 -0
  15. package/dist/commonjs/index.js +9 -0
  16. package/dist/commonjs/index.js.map +1 -0
  17. package/dist/commonjs/package.json +3 -0
  18. package/dist/commonjs/useQuery.d.ts +6 -0
  19. package/dist/commonjs/useQuery.d.ts.map +1 -0
  20. package/dist/commonjs/useQuery.js +48 -0
  21. package/dist/commonjs/useQuery.js.map +1 -0
  22. package/dist/commonjs/useTimeout.d.ts +5 -0
  23. package/dist/commonjs/useTimeout.d.ts.map +1 -0
  24. package/dist/commonjs/useTimeout.js +19 -0
  25. package/dist/commonjs/useTimeout.js.map +1 -0
  26. package/dist/commonjs/version.d.ts +3 -0
  27. package/dist/commonjs/version.d.ts.map +1 -0
  28. package/dist/commonjs/version.js +5 -0
  29. package/dist/commonjs/version.js.map +1 -0
  30. package/dist/esm/InstantReactAbstractDatabase.d.ts +241 -0
  31. package/dist/esm/InstantReactAbstractDatabase.d.ts.map +1 -0
  32. package/dist/esm/InstantReactAbstractDatabase.js +320 -0
  33. package/dist/esm/InstantReactAbstractDatabase.js.map +1 -0
  34. package/dist/esm/InstantReactRoom.d.ts +183 -0
  35. package/dist/esm/InstantReactRoom.d.ts.map +1 -0
  36. package/dist/esm/InstantReactRoom.js +275 -0
  37. package/dist/esm/InstantReactRoom.js.map +1 -0
  38. package/dist/esm/index.d.ts +4 -0
  39. package/dist/esm/index.d.ts.map +1 -0
  40. package/dist/esm/index.js +3 -0
  41. package/dist/esm/index.js.map +1 -0
  42. package/dist/esm/package.json +3 -0
  43. package/dist/esm/useQuery.d.ts +6 -0
  44. package/dist/esm/useQuery.d.ts.map +1 -0
  45. package/dist/esm/useQuery.js +45 -0
  46. package/dist/esm/useQuery.js.map +1 -0
  47. package/dist/esm/useTimeout.d.ts +5 -0
  48. package/dist/esm/useTimeout.d.ts.map +1 -0
  49. package/dist/esm/useTimeout.js +16 -0
  50. package/dist/esm/useTimeout.js.map +1 -0
  51. package/dist/esm/version.d.ts +3 -0
  52. package/dist/esm/version.d.ts.map +1 -0
  53. package/dist/esm/version.js +3 -0
  54. package/dist/esm/version.js.map +1 -0
  55. package/dist/standalone/index.js +5993 -0
  56. package/dist/standalone/index.umd.cjs +36 -0
  57. package/package.json +68 -0
  58. package/src/InstantReactAbstractDatabase.tsx +421 -0
  59. package/src/InstantReactRoom.ts +443 -0
  60. package/src/index.ts +4 -0
  61. package/src/useQuery.ts +99 -0
  62. package/src/useTimeout.ts +20 -0
  63. package/src/version.ts +3 -0
  64. package/tsconfig.json +11 -0
  65. package/vite.config.ts +35 -0
@@ -0,0 +1,421 @@
1
+ import {
2
+ // types
3
+ Auth,
4
+ Storage,
5
+ txInit,
6
+ type AuthState,
7
+ type User,
8
+ type ConnectionStatus,
9
+ type TransactionChunk,
10
+ type RoomSchemaShape,
11
+ type InstaQLParams,
12
+ type InstaQLOptions,
13
+ type InstantConfig,
14
+ type PageInfoResponse,
15
+ InstantCoreDatabase,
16
+ init as core_init,
17
+ InstaQLLifecycleState,
18
+ InstaQLResponse,
19
+ RoomsOf,
20
+ InstantSchemaDef,
21
+ IInstantDatabase,
22
+ InstantError,
23
+ ValidQuery,
24
+ } from '@instantdb/core';
25
+ import {
26
+ ReactNode,
27
+ useCallback,
28
+ useEffect,
29
+ useRef,
30
+ useState,
31
+ useSyncExternalStore,
32
+ } from 'react';
33
+ import { useQueryInternal } from './useQuery.ts';
34
+ import { InstantReactRoom, rooms } from './InstantReactRoom.ts';
35
+
36
+ const defaultAuthState = {
37
+ isLoading: true,
38
+ user: undefined,
39
+ error: undefined,
40
+ };
41
+
42
+ export default abstract class InstantReactAbstractDatabase<
43
+ // need to pull this schema out to another generic for query params, not sure why
44
+ Schema extends InstantSchemaDef<any, any, any>,
45
+ Config extends InstantConfig<Schema, boolean> = InstantConfig<Schema, false>,
46
+ Rooms extends RoomSchemaShape = RoomsOf<Schema>,
47
+ > implements IInstantDatabase<Schema>
48
+ {
49
+ public tx = txInit<Schema>();
50
+
51
+ public auth: Auth;
52
+ public storage: Storage;
53
+ public core: InstantCoreDatabase<Schema, Config['useDateObjects']>;
54
+
55
+ /** @deprecated use `core` instead */
56
+ public _core: InstantCoreDatabase<Schema, Config['useDateObjects']>;
57
+
58
+ static Storage?: any;
59
+ static NetworkListener?: any;
60
+ static EventSourceImpl?: any;
61
+
62
+ constructor(config: Config, versions?: { [key: string]: string }) {
63
+ this.core = core_init<Schema, Config['useDateObjects']>(
64
+ config,
65
+ // @ts-expect-error because TS can't resolve subclass statics
66
+ this.constructor.Storage,
67
+ // @ts-expect-error because TS can't resolve subclass statics
68
+ this.constructor.NetworkListener,
69
+ versions,
70
+ // @ts-expect-error because TS can't resolve subclass statics
71
+ this.constructor.EventSourceImpl,
72
+ );
73
+ this._core = this.core;
74
+ this.auth = this.core.auth;
75
+ this.storage = this.core.storage;
76
+ }
77
+
78
+ /**
79
+ * Returns a unique ID for a given `name`. It's stored in local storage,
80
+ * so you will get the same ID across sessions.
81
+ *
82
+ * This is useful for generating IDs that could identify a local device or user.
83
+ *
84
+ * @example
85
+ * const deviceId = await db.getLocalId('device');
86
+ */
87
+ getLocalId = (name: string): Promise<string> => {
88
+ return this.core.getLocalId(name);
89
+ };
90
+
91
+ /**
92
+ * A hook that returns a unique ID for a given `name`. localIds are
93
+ * stored in local storage, so you will get the same ID across sessions.
94
+ *
95
+ * Initially returns `null`, and then loads the localId.
96
+ *
97
+ * @example
98
+ * const deviceId = db.useLocalId('device');
99
+ * if (!deviceId) return null; // loading
100
+ * console.log('Device ID:', deviceId)
101
+ */
102
+ useLocalId = (name: string): string | null => {
103
+ const [localId, setLocalId] = useState<string | null>(null);
104
+
105
+ useEffect(() => {
106
+ let mounted = true;
107
+ const f = async () => {
108
+ const id = await this.getLocalId(name);
109
+ if (!mounted) return;
110
+ setLocalId(id);
111
+ };
112
+ f();
113
+ return;
114
+ }, [name]);
115
+
116
+ return localId;
117
+ };
118
+
119
+ /**
120
+ * Obtain a handle to a room, which allows you to listen to topics and presence data
121
+ *
122
+ * If you don't provide a `type` or `id`, Instant will default to `_defaultRoomType` and `_defaultRoomId`
123
+ * as the room type and id, respectively.
124
+ *
125
+ * @see https://instantdb.com/docs/presence-and-topics
126
+ *
127
+ * @example
128
+ * const room = db.room('chat', roomId);
129
+ * const { peers } = db.rooms.usePresence(room);
130
+ */
131
+ room<RoomType extends keyof Rooms>(
132
+ type: RoomType = '_defaultRoomType' as RoomType,
133
+ id: string = '_defaultRoomId',
134
+ ) {
135
+ return new InstantReactRoom<Schema, Rooms, RoomType>(this.core, type, id);
136
+ }
137
+
138
+ /**
139
+ * Hooks for working with rooms
140
+ *
141
+ * @see https://instantdb.com/docs/presence-and-topics
142
+ *
143
+ * @example
144
+ * const room = db.room('chat', roomId);
145
+ * const { peers } = db.rooms.usePresence(room);
146
+ * const publish = db.rooms.usePublishTopic(room, 'emoji');
147
+ * // ...
148
+ */
149
+ rooms = rooms;
150
+
151
+ /**
152
+ * Use this to write data! You can create, update, delete, and link objects
153
+ *
154
+ * @see https://instantdb.com/docs/instaml
155
+ *
156
+ * @example
157
+ * // Create a new object in the `goals` namespace
158
+ * const goalId = id();
159
+ * db.transact(db.tx.goals[goalId].update({title: "Get fit"}))
160
+ *
161
+ * // Update the title
162
+ * db.transact(db.tx.goals[goalId].update({title: "Get super fit"}))
163
+ *
164
+ * // Delete it
165
+ * db.transact(db.tx.goals[goalId].delete())
166
+ *
167
+ * // Or create an association:
168
+ * todoId = id();
169
+ * db.transact([
170
+ * db.tx.todos[todoId].update({ title: 'Go on a run' }),
171
+ * db.tx.goals[goalId].link({todos: todoId}),
172
+ * ])
173
+ */
174
+ transact = (
175
+ chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],
176
+ ) => {
177
+ return this.core.transact(chunks);
178
+ };
179
+
180
+ /**
181
+ * Use this to query your data!
182
+ *
183
+ * @see https://instantdb.com/docs/instaql
184
+ *
185
+ * @example
186
+ * // listen to all goals
187
+ * const { isLoading, error, data } = db.useQuery({ goals: {} });
188
+ *
189
+ * // goals where the title is "Get Fit"
190
+ * const { isLoading, error, data } = db.useQuery({
191
+ * goals: { $: { where: { title: 'Get Fit' } } },
192
+ * });
193
+ *
194
+ * // all goals, _alongside_ their todos
195
+ * const { isLoading, error, data } = db.useQuery({
196
+ * goals: { todos: {} },
197
+ * });
198
+ *
199
+ * // skip if `user` is not logged in
200
+ * const { isLoading, error, data } = db.useQuery(
201
+ * auth.user ? { goals: {} } : null,
202
+ * );
203
+ */
204
+ useQuery = <Q extends ValidQuery<Q, Schema>>(
205
+ query: null | Q,
206
+ opts?: InstaQLOptions,
207
+ ): InstaQLLifecycleState<
208
+ Schema,
209
+ Q,
210
+ NonNullable<Config['useDateObjects']>
211
+ > => {
212
+ return useQueryInternal<Q, Schema, Config['useDateObjects']>(
213
+ this.core,
214
+ query,
215
+ opts,
216
+ ).state;
217
+ };
218
+
219
+ /**
220
+ * Listen for the logged in state. This is useful
221
+ * for deciding when to show a login screen.
222
+ *
223
+ * Check out the docs for an example `Login` component too!
224
+ *
225
+ * @see https://instantdb.com/docs/auth
226
+ * @example
227
+ * function App() {
228
+ * const { isLoading, user, error } = db.useAuth()
229
+ * if (isLoading) {
230
+ * return <div>Loading...</div>
231
+ * }
232
+ * if (error) {
233
+ * return <div>Uh oh! {error.message}</div>
234
+ * }
235
+ * if (user) {
236
+ * return <Main user={user} />
237
+ * }
238
+ * return <Login />
239
+ * }
240
+ *
241
+ */
242
+ useAuth = (): AuthState => {
243
+ // We use a ref to store the result of the query.
244
+ // This is becuase `useSyncExternalStore` uses `Object.is`
245
+ // to compare the previous and next state.
246
+ // If we don't use a ref, the state will always be considered different, so
247
+ // the component will always re-render.
248
+ const resultCacheRef = useRef<AuthState>(
249
+ this.core._reactor._currentUserCached,
250
+ );
251
+
252
+ // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe
253
+ // if `subscribe` changes, so we use `useCallback` to memoize the function.
254
+ const subscribe = useCallback((cb: Function) => {
255
+ const unsubscribe = this.core.subscribeAuth((auth) => {
256
+ resultCacheRef.current = { isLoading: false, ...auth };
257
+ cb();
258
+ });
259
+
260
+ return unsubscribe;
261
+ }, []);
262
+
263
+ const state = useSyncExternalStore<AuthState>(
264
+ subscribe,
265
+ () => resultCacheRef.current,
266
+ () => defaultAuthState,
267
+ );
268
+ return state;
269
+ };
270
+
271
+ /**
272
+ * Subscribe to the currently logged in user.
273
+ * If the user is not logged in, this hook with throw an Error.
274
+ * You will want to protect any calls of this hook with a
275
+ * <db.SignedIn> component, or your own logic based on db.useAuth()
276
+ *
277
+ * @see https://instantdb.com/docs/auth
278
+ * @throws Error indicating user not signed in
279
+ * @example
280
+ * function UserDisplay() {
281
+ * const user = db.useUser()
282
+ * return <div>Logged in as: {user.email}</div>
283
+ * }
284
+ *
285
+ * <db.SignedIn>
286
+ * <UserDisplay />
287
+ * </db.SignedIn>
288
+ *
289
+ */
290
+ useUser = (): User => {
291
+ const { user } = this.useAuth();
292
+ if (!user) {
293
+ throw new InstantError(
294
+ 'useUser must be used within an auth-protected route',
295
+ );
296
+ }
297
+ return user;
298
+ };
299
+
300
+ /**
301
+ * One time query for the logged in state. This is useful
302
+ * for scenarios where you want to know the current auth
303
+ * state without subscribing to changes.
304
+ *
305
+ * @see https://instantdb.com/docs/auth
306
+ * @example
307
+ * const user = await db.getAuth();
308
+ * console.log('logged in as', user.email)
309
+ */
310
+ getAuth(): Promise<User | null> {
311
+ return this.core.getAuth();
312
+ }
313
+
314
+ /**
315
+ * Listen for connection status changes to Instant. Use this for things like
316
+ * showing connection state to users
317
+ *
318
+ * @see https://www.instantdb.com/docs/patterns#connection-status
319
+ * @example
320
+ * function App() {
321
+ * const status = db.useConnectionStatus()
322
+ * const connectionState =
323
+ * status === 'connecting' || status === 'opened'
324
+ * ? 'authenticating'
325
+ * : status === 'authenticated'
326
+ * ? 'connected'
327
+ * : status === 'closed'
328
+ * ? 'closed'
329
+ * : status === 'errored'
330
+ * ? 'errored'
331
+ * : 'unexpected state';
332
+ *
333
+ * return <div>Connection state: {connectionState}</div>
334
+ * }
335
+ */
336
+ useConnectionStatus = (): ConnectionStatus => {
337
+ const statusRef = useRef<ConnectionStatus>(
338
+ this.core._reactor.status as ConnectionStatus,
339
+ );
340
+
341
+ const subscribe = useCallback((cb: Function) => {
342
+ const unsubscribe = this.core.subscribeConnectionStatus((newStatus) => {
343
+ if (newStatus !== statusRef.current) {
344
+ statusRef.current = newStatus;
345
+ cb();
346
+ }
347
+ });
348
+
349
+ return unsubscribe;
350
+ }, []);
351
+
352
+ const status = useSyncExternalStore<ConnectionStatus>(
353
+ subscribe,
354
+ () => statusRef.current,
355
+ // For SSR, always return 'connecting' as the initial state
356
+ () => 'connecting',
357
+ );
358
+
359
+ return status;
360
+ };
361
+
362
+ /**
363
+ * Use this for one-off queries.
364
+ * Returns local data if available, otherwise fetches from the server.
365
+ * Because we want to avoid stale data, this method will throw an error
366
+ * if the user is offline or there is no active connection to the server.
367
+ *
368
+ * @see https://instantdb.com/docs/instaql
369
+ *
370
+ * @example
371
+ *
372
+ * const resp = await db.queryOnce({ goals: {} });
373
+ * console.log(resp.data.goals)
374
+ */
375
+ queryOnce = <Q extends ValidQuery<Q, Schema>>(
376
+ query: Q,
377
+ opts?: InstaQLOptions,
378
+ ): Promise<{
379
+ data: InstaQLResponse<Schema, Q, Config['useDateObjects']>;
380
+ pageInfo: PageInfoResponse<Q>;
381
+ }> => {
382
+ return this.core.queryOnce(query, opts);
383
+ };
384
+
385
+ /**
386
+ * Only render children if the user is signed in.
387
+ * @see https://instantdb.com/docs/auth
388
+ *
389
+ * @example
390
+ * <db.SignedIn>
391
+ * <MyComponent />
392
+ * </db.SignedIn>
393
+ *
394
+ */
395
+ SignedIn: React.FC<{
396
+ children: ReactNode;
397
+ }> = ({ children }) => {
398
+ const auth = this.useAuth();
399
+ if (auth.isLoading || auth.error || !auth.user) return null;
400
+
401
+ return <>{children}</>;
402
+ };
403
+
404
+ /**
405
+ * Only render children if the user is signed out.
406
+ * @see https://instantdb.com/docs/auth
407
+ *
408
+ * @example
409
+ * <db.SignedOut>
410
+ * <MyComponent />
411
+ * </db.SignedOut>
412
+ *
413
+ */
414
+ SignedOut: React.FC<{
415
+ children: ReactNode;
416
+ }> = ({ children }) => {
417
+ const auth = this.useAuth();
418
+ if (auth.isLoading || auth.error || auth.user) return null;
419
+ return <>{children}</>;
420
+ };
421
+ }