@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.
- package/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/README.md +53 -0
- package/dist/commonjs/InstantReactAbstractDatabase.d.ts +241 -0
- package/dist/commonjs/InstantReactAbstractDatabase.d.ts.map +1 -0
- package/dist/commonjs/InstantReactAbstractDatabase.js +323 -0
- package/dist/commonjs/InstantReactAbstractDatabase.js.map +1 -0
- package/dist/commonjs/InstantReactRoom.d.ts +183 -0
- package/dist/commonjs/InstantReactRoom.d.ts.map +1 -0
- package/dist/commonjs/InstantReactRoom.js +284 -0
- package/dist/commonjs/InstantReactRoom.js.map +1 -0
- package/dist/commonjs/index.d.ts +4 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +9 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/useQuery.d.ts +6 -0
- package/dist/commonjs/useQuery.d.ts.map +1 -0
- package/dist/commonjs/useQuery.js +48 -0
- package/dist/commonjs/useQuery.js.map +1 -0
- package/dist/commonjs/useTimeout.d.ts +5 -0
- package/dist/commonjs/useTimeout.d.ts.map +1 -0
- package/dist/commonjs/useTimeout.js +19 -0
- package/dist/commonjs/useTimeout.js.map +1 -0
- package/dist/commonjs/version.d.ts +3 -0
- package/dist/commonjs/version.d.ts.map +1 -0
- package/dist/commonjs/version.js +5 -0
- package/dist/commonjs/version.js.map +1 -0
- package/dist/esm/InstantReactAbstractDatabase.d.ts +241 -0
- package/dist/esm/InstantReactAbstractDatabase.d.ts.map +1 -0
- package/dist/esm/InstantReactAbstractDatabase.js +320 -0
- package/dist/esm/InstantReactAbstractDatabase.js.map +1 -0
- package/dist/esm/InstantReactRoom.d.ts +183 -0
- package/dist/esm/InstantReactRoom.d.ts.map +1 -0
- package/dist/esm/InstantReactRoom.js +275 -0
- package/dist/esm/InstantReactRoom.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/useQuery.d.ts +6 -0
- package/dist/esm/useQuery.d.ts.map +1 -0
- package/dist/esm/useQuery.js +45 -0
- package/dist/esm/useQuery.js.map +1 -0
- package/dist/esm/useTimeout.d.ts +5 -0
- package/dist/esm/useTimeout.d.ts.map +1 -0
- package/dist/esm/useTimeout.js +16 -0
- package/dist/esm/useTimeout.js.map +1 -0
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/standalone/index.js +5993 -0
- package/dist/standalone/index.umd.cjs +36 -0
- package/package.json +68 -0
- package/src/InstantReactAbstractDatabase.tsx +421 -0
- package/src/InstantReactRoom.ts +443 -0
- package/src/index.ts +4 -0
- package/src/useQuery.ts +99 -0
- package/src/useTimeout.ts +20 -0
- package/src/version.ts +3 -0
- package/tsconfig.json +11 -0
- package/vite.config.ts +35 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import {
|
|
2
|
+
// types
|
|
3
|
+
type PresenceOpts,
|
|
4
|
+
type PresenceResponse,
|
|
5
|
+
type RoomSchemaShape,
|
|
6
|
+
InstantCoreDatabase,
|
|
7
|
+
InstantSchemaDef,
|
|
8
|
+
} from '@instantdb/core';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
KeyboardEvent,
|
|
12
|
+
useCallback,
|
|
13
|
+
useEffect,
|
|
14
|
+
useMemo,
|
|
15
|
+
useState,
|
|
16
|
+
} from 'react';
|
|
17
|
+
|
|
18
|
+
import { useTimeout } from './useTimeout.ts';
|
|
19
|
+
|
|
20
|
+
export type PresenceHandle<
|
|
21
|
+
PresenceShape,
|
|
22
|
+
Keys extends keyof PresenceShape,
|
|
23
|
+
> = PresenceResponse<PresenceShape, Keys> & {
|
|
24
|
+
publishPresence: (data: Partial<PresenceShape>) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type TypingIndicatorOpts = {
|
|
28
|
+
timeout?: number | null;
|
|
29
|
+
stopOnEnter?: boolean;
|
|
30
|
+
// Perf opt - `active` will always be an empty array
|
|
31
|
+
writeOnly?: boolean;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type TypingIndicatorHandle<PresenceShape> = {
|
|
35
|
+
active: PresenceShape[];
|
|
36
|
+
setActive(active: boolean): void;
|
|
37
|
+
inputProps: {
|
|
38
|
+
onKeyDown: (e: KeyboardEvent) => void;
|
|
39
|
+
onBlur: () => void;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const defaultActivityStopTimeout = 1_000;
|
|
44
|
+
|
|
45
|
+
// ------
|
|
46
|
+
// Topics
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Listen for broadcasted events given a room and topic.
|
|
50
|
+
*
|
|
51
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
52
|
+
* @example
|
|
53
|
+
* function App({ roomId }) {
|
|
54
|
+
* const room = db.room('chats', roomId);
|
|
55
|
+
* db.rooms.useTopicEffect(room, 'emoji', (message, peer) => {
|
|
56
|
+
* console.log(peer.name, 'sent', message);
|
|
57
|
+
* });
|
|
58
|
+
* // ...
|
|
59
|
+
* }
|
|
60
|
+
*/
|
|
61
|
+
export function useTopicEffect<
|
|
62
|
+
RoomSchema extends RoomSchemaShape,
|
|
63
|
+
RoomType extends keyof RoomSchema,
|
|
64
|
+
TopicType extends keyof RoomSchema[RoomType]['topics'],
|
|
65
|
+
>(
|
|
66
|
+
room: InstantReactRoom<any, RoomSchema, RoomType>,
|
|
67
|
+
topic: TopicType,
|
|
68
|
+
onEvent: (
|
|
69
|
+
event: RoomSchema[RoomType]['topics'][TopicType],
|
|
70
|
+
peer: RoomSchema[RoomType]['presence'],
|
|
71
|
+
) => any,
|
|
72
|
+
): void {
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
const unsub = room.core._reactor.subscribeTopic(
|
|
75
|
+
room.id,
|
|
76
|
+
topic,
|
|
77
|
+
(event, peer) => {
|
|
78
|
+
onEvent(event, peer);
|
|
79
|
+
},
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
return unsub;
|
|
83
|
+
}, [room.id, topic]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Broadcast an event to a room.
|
|
88
|
+
*
|
|
89
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
90
|
+
* @example
|
|
91
|
+
* function App({ roomId }) {
|
|
92
|
+
* const room = db.room('chat', roomId);
|
|
93
|
+
* const publishTopic = db.rooms.usePublishTopic(room, "emoji");
|
|
94
|
+
*
|
|
95
|
+
* return (
|
|
96
|
+
* <button onClick={() => publishTopic({ emoji: "🔥" })}>Send emoji</button>
|
|
97
|
+
* );
|
|
98
|
+
* }
|
|
99
|
+
*
|
|
100
|
+
*/
|
|
101
|
+
export function usePublishTopic<
|
|
102
|
+
RoomSchema extends RoomSchemaShape,
|
|
103
|
+
RoomType extends keyof RoomSchema,
|
|
104
|
+
TopicType extends keyof RoomSchema[RoomType]['topics'],
|
|
105
|
+
>(
|
|
106
|
+
room: InstantReactRoom<any, RoomSchema, RoomType>,
|
|
107
|
+
topic: TopicType,
|
|
108
|
+
): (data: RoomSchema[RoomType]['topics'][TopicType]) => void {
|
|
109
|
+
useEffect(() => room.core._reactor.joinRoom(room.id), [room.id]);
|
|
110
|
+
|
|
111
|
+
const publishTopic = useCallback(
|
|
112
|
+
(data) => {
|
|
113
|
+
room.core._reactor.publishTopic({
|
|
114
|
+
roomType: room.type,
|
|
115
|
+
roomId: room.id,
|
|
116
|
+
topic,
|
|
117
|
+
data,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
[room.id, topic],
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
return publishTopic;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ---------
|
|
127
|
+
// Presence
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Listen for peer's presence data in a room, and publish the current user's presence.
|
|
131
|
+
*
|
|
132
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
133
|
+
* @example
|
|
134
|
+
* function App({ roomId }) {
|
|
135
|
+
* const {
|
|
136
|
+
* peers,
|
|
137
|
+
* publishPresence
|
|
138
|
+
* } = db.room(roomType, roomId).usePresence({ keys: ["name", "avatar"] });
|
|
139
|
+
*
|
|
140
|
+
* // ...
|
|
141
|
+
* }
|
|
142
|
+
*/
|
|
143
|
+
export function usePresence<
|
|
144
|
+
RoomSchema extends RoomSchemaShape,
|
|
145
|
+
RoomType extends keyof RoomSchema,
|
|
146
|
+
Keys extends keyof RoomSchema[RoomType]['presence'],
|
|
147
|
+
>(
|
|
148
|
+
room: InstantReactRoom<any, RoomSchema, RoomType>,
|
|
149
|
+
opts: PresenceOpts<RoomSchema[RoomType]['presence'], Keys> = {},
|
|
150
|
+
): PresenceHandle<RoomSchema[RoomType]['presence'], Keys> {
|
|
151
|
+
const [state, setState] = useState<
|
|
152
|
+
PresenceResponse<RoomSchema[RoomType]['presence'], Keys>
|
|
153
|
+
>(() => {
|
|
154
|
+
return (
|
|
155
|
+
room.core._reactor.getPresence(room.type, room.id, opts) ?? {
|
|
156
|
+
peers: {},
|
|
157
|
+
isLoading: true,
|
|
158
|
+
}
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
const unsub = room.core._reactor.subscribePresence(
|
|
164
|
+
room.type,
|
|
165
|
+
room.id,
|
|
166
|
+
opts,
|
|
167
|
+
(data) => {
|
|
168
|
+
setState(data);
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return unsub;
|
|
173
|
+
}, [room.id, opts.user, opts.peers?.join(), opts.keys?.join()]);
|
|
174
|
+
|
|
175
|
+
const publishPresence = useCallback(
|
|
176
|
+
(data) => {
|
|
177
|
+
room.core._reactor.publishPresence(room.type, room.id, data);
|
|
178
|
+
},
|
|
179
|
+
[room.type, room.id],
|
|
180
|
+
);
|
|
181
|
+
const ret = useMemo(() => {
|
|
182
|
+
return {
|
|
183
|
+
...state,
|
|
184
|
+
publishPresence,
|
|
185
|
+
};
|
|
186
|
+
}, [state, publishPresence]);
|
|
187
|
+
return ret;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Publishes presence data to a room
|
|
192
|
+
*
|
|
193
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
194
|
+
* @example
|
|
195
|
+
* function App({ roomId, nickname }) {
|
|
196
|
+
* const room = db.room('chat', roomId);
|
|
197
|
+
* db.rooms.useSyncPresence(room, { nickname });
|
|
198
|
+
* }
|
|
199
|
+
*/
|
|
200
|
+
export function useSyncPresence<
|
|
201
|
+
RoomSchema extends RoomSchemaShape,
|
|
202
|
+
RoomType extends keyof RoomSchema,
|
|
203
|
+
>(
|
|
204
|
+
room: InstantReactRoom<any, RoomSchema, RoomType>,
|
|
205
|
+
data: Partial<RoomSchema[RoomType]['presence']>,
|
|
206
|
+
deps?: any[],
|
|
207
|
+
): void {
|
|
208
|
+
useEffect(() => room.core._reactor.joinRoom(room.id, data), [room.id]);
|
|
209
|
+
useEffect(() => {
|
|
210
|
+
return room.core._reactor.publishPresence(room.type, room.id, data);
|
|
211
|
+
}, [room.type, room.id, deps ?? JSON.stringify(data)]);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// -----------------
|
|
215
|
+
// Typing Indicator
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Manage typing indicator state
|
|
219
|
+
*
|
|
220
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
221
|
+
* @example
|
|
222
|
+
* function App({ roomId }) {
|
|
223
|
+
* const room = db.room('chat', roomId);
|
|
224
|
+
* const {
|
|
225
|
+
* active,
|
|
226
|
+
* setActive,
|
|
227
|
+
* inputProps,
|
|
228
|
+
* } = db.rooms.useTypingIndicator(room, "chat-input");
|
|
229
|
+
*
|
|
230
|
+
* return <input {...inputProps} />;
|
|
231
|
+
* }
|
|
232
|
+
*/
|
|
233
|
+
export function useTypingIndicator<
|
|
234
|
+
RoomSchema extends RoomSchemaShape,
|
|
235
|
+
RoomType extends keyof RoomSchema,
|
|
236
|
+
>(
|
|
237
|
+
room: InstantReactRoom<any, RoomSchema, RoomType>,
|
|
238
|
+
inputName: string,
|
|
239
|
+
opts: TypingIndicatorOpts = {},
|
|
240
|
+
): TypingIndicatorHandle<RoomSchema[RoomType]['presence']> {
|
|
241
|
+
const timeout = useTimeout();
|
|
242
|
+
|
|
243
|
+
const observedPresence = rooms.usePresence(room, {
|
|
244
|
+
keys: [inputName],
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const active = useMemo(() => {
|
|
248
|
+
const presenceSnapshot = room._core._reactor.getPresence(
|
|
249
|
+
room.type,
|
|
250
|
+
room.id,
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
return opts?.writeOnly
|
|
254
|
+
? []
|
|
255
|
+
: Object.values(presenceSnapshot?.peers ?? {}).filter(
|
|
256
|
+
(p) => p[inputName] === true,
|
|
257
|
+
);
|
|
258
|
+
}, [opts?.writeOnly, observedPresence]);
|
|
259
|
+
|
|
260
|
+
const setActive = useCallback(
|
|
261
|
+
(isActive: boolean) => {
|
|
262
|
+
room.core._reactor.publishPresence(room.type, room.id, {
|
|
263
|
+
[inputName]: isActive,
|
|
264
|
+
} as unknown as Partial<RoomSchema[RoomType]>);
|
|
265
|
+
|
|
266
|
+
if (!isActive) return;
|
|
267
|
+
|
|
268
|
+
if (opts?.timeout === null || opts?.timeout === 0) return;
|
|
269
|
+
|
|
270
|
+
timeout.set(opts?.timeout ?? defaultActivityStopTimeout, () => {
|
|
271
|
+
room.core._reactor.publishPresence(room.type, room.id, {
|
|
272
|
+
[inputName]: null,
|
|
273
|
+
} as Partial<RoomSchema[RoomType]>);
|
|
274
|
+
});
|
|
275
|
+
},
|
|
276
|
+
[room.type, room.id, inputName, opts?.timeout, timeout],
|
|
277
|
+
);
|
|
278
|
+
const onKeyDown = useCallback(
|
|
279
|
+
(e: KeyboardEvent) => {
|
|
280
|
+
const isEnter = opts?.stopOnEnter && e.key === 'Enter';
|
|
281
|
+
const isActive = !isEnter;
|
|
282
|
+
|
|
283
|
+
setActive(isActive);
|
|
284
|
+
},
|
|
285
|
+
[opts.stopOnEnter, setActive],
|
|
286
|
+
);
|
|
287
|
+
const onBlur = useCallback(() => {
|
|
288
|
+
setActive(false);
|
|
289
|
+
}, [setActive]);
|
|
290
|
+
|
|
291
|
+
const inputProps = useMemo(() => {
|
|
292
|
+
return { onKeyDown, onBlur };
|
|
293
|
+
}, [onKeyDown, onBlur]);
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
active,
|
|
297
|
+
setActive,
|
|
298
|
+
inputProps,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// --------------
|
|
303
|
+
// Hooks
|
|
304
|
+
|
|
305
|
+
export const rooms = {
|
|
306
|
+
useTopicEffect,
|
|
307
|
+
usePublishTopic,
|
|
308
|
+
usePresence,
|
|
309
|
+
useSyncPresence,
|
|
310
|
+
useTypingIndicator,
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// ------------
|
|
314
|
+
// Class
|
|
315
|
+
|
|
316
|
+
export class InstantReactRoom<
|
|
317
|
+
Schema extends InstantSchemaDef<any, any, any>,
|
|
318
|
+
RoomSchema extends RoomSchemaShape,
|
|
319
|
+
RoomType extends keyof RoomSchema,
|
|
320
|
+
> {
|
|
321
|
+
core: InstantCoreDatabase<Schema, boolean>;
|
|
322
|
+
/** @deprecated use `core` instead */
|
|
323
|
+
_core: InstantCoreDatabase<Schema, boolean>;
|
|
324
|
+
type: RoomType;
|
|
325
|
+
id: string;
|
|
326
|
+
|
|
327
|
+
constructor(
|
|
328
|
+
core: InstantCoreDatabase<Schema, boolean>,
|
|
329
|
+
type: RoomType,
|
|
330
|
+
id: string,
|
|
331
|
+
) {
|
|
332
|
+
this.core = core;
|
|
333
|
+
this._core = this.core;
|
|
334
|
+
this.type = type;
|
|
335
|
+
this.id = id;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* @deprecated
|
|
340
|
+
* `db.room(...).useTopicEffect` is deprecated. You can replace it with `db.rooms.useTopicEffect`.
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
*
|
|
344
|
+
* // Before
|
|
345
|
+
* const room = db.room('chat', 'room-id');
|
|
346
|
+
* room.useTopicEffect('emoji', (message, peer) => { });
|
|
347
|
+
*
|
|
348
|
+
* // After
|
|
349
|
+
* const room = db.room('chat', 'room-id');
|
|
350
|
+
* db.rooms.useTopicEffect(room, 'emoji', (message, peer) => { });
|
|
351
|
+
*/
|
|
352
|
+
useTopicEffect = <TopicType extends keyof RoomSchema[RoomType]['topics']>(
|
|
353
|
+
topic: TopicType,
|
|
354
|
+
onEvent: (
|
|
355
|
+
event: RoomSchema[RoomType]['topics'][TopicType],
|
|
356
|
+
peer: RoomSchema[RoomType]['presence'],
|
|
357
|
+
) => any,
|
|
358
|
+
): void => {
|
|
359
|
+
rooms.useTopicEffect(this, topic, onEvent);
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* @deprecated
|
|
364
|
+
* `db.room(...).usePublishTopic` is deprecated. You can replace it with `db.rooms.usePublishTopic`.
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
*
|
|
368
|
+
* // Before
|
|
369
|
+
* const room = db.room('chat', 'room-id');
|
|
370
|
+
* const publish = room.usePublishTopic('emoji');
|
|
371
|
+
*
|
|
372
|
+
* // After
|
|
373
|
+
* const room = db.room('chat', 'room-id');
|
|
374
|
+
* const publish = db.rooms.usePublishTopic(room, 'emoji');
|
|
375
|
+
*/
|
|
376
|
+
usePublishTopic = <Topic extends keyof RoomSchema[RoomType]['topics']>(
|
|
377
|
+
topic: Topic,
|
|
378
|
+
): ((data: RoomSchema[RoomType]['topics'][Topic]) => void) => {
|
|
379
|
+
return rooms.usePublishTopic(this, topic);
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* @deprecated
|
|
384
|
+
* `db.room(...).usePresence` is deprecated. You can replace it with `db.rooms.usePresence`.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
*
|
|
388
|
+
* // Before
|
|
389
|
+
* const room = db.room('chat', 'room-id');
|
|
390
|
+
* const { peers } = room.usePresence({ keys: ["name", "avatar"] });
|
|
391
|
+
*
|
|
392
|
+
* // After
|
|
393
|
+
* const room = db.room('chat', 'room-id');
|
|
394
|
+
* const { peers } = db.rooms.usePresence(room, { keys: ["name", "avatar"] });
|
|
395
|
+
*/
|
|
396
|
+
usePresence = <Keys extends keyof RoomSchema[RoomType]['presence']>(
|
|
397
|
+
opts: PresenceOpts<RoomSchema[RoomType]['presence'], Keys> = {},
|
|
398
|
+
): PresenceHandle<RoomSchema[RoomType]['presence'], Keys> => {
|
|
399
|
+
return rooms.usePresence(this, opts);
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* @deprecated
|
|
404
|
+
* `db.room(...).useSyncPresence` is deprecated. You can replace it with `db.rooms.useSyncPresence`.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
*
|
|
408
|
+
* // Before
|
|
409
|
+
* const room = db.room('chat', 'room-id');
|
|
410
|
+
* room.useSyncPresence(room, { nickname });
|
|
411
|
+
*
|
|
412
|
+
* // After
|
|
413
|
+
* const room = db.room('chat', 'room-id');
|
|
414
|
+
* db.rooms.useSyncPresence(room, { nickname });
|
|
415
|
+
*/
|
|
416
|
+
useSyncPresence = (
|
|
417
|
+
data: Partial<RoomSchema[RoomType]['presence']>,
|
|
418
|
+
deps?: any[],
|
|
419
|
+
): void => {
|
|
420
|
+
return rooms.useSyncPresence(this, data, deps);
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* @deprecated
|
|
425
|
+
* `db.room(...).useTypingIndicator` is deprecated. You can replace it with `db.rooms.useTypingIndicator`.
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
*
|
|
429
|
+
* // Before
|
|
430
|
+
* const room = db.room('chat', 'room-id');
|
|
431
|
+
* const typing = room.useTypingIndiactor(room, 'chat-input');
|
|
432
|
+
*
|
|
433
|
+
* // After
|
|
434
|
+
* const room = db.room('chat', 'room-id');
|
|
435
|
+
* const typing = db.rooms.useTypingIndiactor(room, 'chat-input');
|
|
436
|
+
*/
|
|
437
|
+
useTypingIndicator = (
|
|
438
|
+
inputName: string,
|
|
439
|
+
opts: TypingIndicatorOpts = {},
|
|
440
|
+
): TypingIndicatorHandle<RoomSchema[RoomType]['presence']> => {
|
|
441
|
+
return rooms.useTypingIndicator(this, inputName, opts);
|
|
442
|
+
};
|
|
443
|
+
}
|
package/src/index.ts
ADDED
package/src/useQuery.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
weakHash,
|
|
3
|
+
coerceQuery,
|
|
4
|
+
type InstaQLParams,
|
|
5
|
+
type InstaQLOptions,
|
|
6
|
+
type InstantGraph,
|
|
7
|
+
InstantCoreDatabase,
|
|
8
|
+
InstaQLLifecycleState,
|
|
9
|
+
InstantSchemaDef,
|
|
10
|
+
ValidQuery,
|
|
11
|
+
} from '@instantdb/core';
|
|
12
|
+
import { useCallback, useRef, useSyncExternalStore } from 'react';
|
|
13
|
+
|
|
14
|
+
const defaultState = {
|
|
15
|
+
isLoading: true,
|
|
16
|
+
data: undefined,
|
|
17
|
+
pageInfo: undefined,
|
|
18
|
+
error: undefined,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function stateForResult(result: any) {
|
|
22
|
+
return {
|
|
23
|
+
isLoading: !Boolean(result),
|
|
24
|
+
data: undefined,
|
|
25
|
+
pageInfo: undefined,
|
|
26
|
+
error: undefined,
|
|
27
|
+
...(result ? result : {}),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useQueryInternal<
|
|
32
|
+
Q extends ValidQuery<Q, Schema>,
|
|
33
|
+
Schema extends InstantSchemaDef<any, any, any>,
|
|
34
|
+
UseDates extends boolean,
|
|
35
|
+
>(
|
|
36
|
+
_core: InstantCoreDatabase<Schema, UseDates>,
|
|
37
|
+
_query: null | Q,
|
|
38
|
+
_opts?: InstaQLOptions,
|
|
39
|
+
): {
|
|
40
|
+
state: InstaQLLifecycleState<Schema, Q, UseDates>;
|
|
41
|
+
query: any;
|
|
42
|
+
} {
|
|
43
|
+
if (_query && _opts && 'ruleParams' in _opts) {
|
|
44
|
+
_query = { $$ruleParams: _opts['ruleParams'], ..._query };
|
|
45
|
+
}
|
|
46
|
+
const query = _query ? coerceQuery(_query) : null;
|
|
47
|
+
const queryHash = weakHash(query);
|
|
48
|
+
|
|
49
|
+
// We use a ref to store the result of the query.
|
|
50
|
+
// This is becuase `useSyncExternalStore` uses `Object.is`
|
|
51
|
+
// to compare the previous and next state.
|
|
52
|
+
// If we don't use a ref, the state will always be considered different, so
|
|
53
|
+
// the component will always re-render.
|
|
54
|
+
const resultCacheRef = useRef<InstaQLLifecycleState<Schema, Q, UseDates>>(
|
|
55
|
+
stateForResult(_core._reactor.getPreviousResult(query)),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
// Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe
|
|
59
|
+
// if `subscribe` changes, so we use `useCallback` to memoize the function.
|
|
60
|
+
const subscribe = useCallback(
|
|
61
|
+
(cb) => {
|
|
62
|
+
// Update the ref when the query changes to avoid showing stale data
|
|
63
|
+
resultCacheRef.current = stateForResult(
|
|
64
|
+
_core._reactor.getPreviousResult(query),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// Don't subscribe if query is null
|
|
68
|
+
if (!query) {
|
|
69
|
+
const unsubscribe = () => {};
|
|
70
|
+
return unsubscribe;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const unsubscribe = _core.subscribeQuery<Q, UseDates>(query, (result) => {
|
|
74
|
+
resultCacheRef.current = {
|
|
75
|
+
isLoading: !Boolean(result),
|
|
76
|
+
data: undefined,
|
|
77
|
+
pageInfo: undefined,
|
|
78
|
+
error: undefined,
|
|
79
|
+
...result,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
cb();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return unsubscribe;
|
|
86
|
+
},
|
|
87
|
+
// Build a new subscribe function if the query changes
|
|
88
|
+
[queryHash],
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const state = useSyncExternalStore<
|
|
92
|
+
InstaQLLifecycleState<Schema, Q, UseDates>
|
|
93
|
+
>(
|
|
94
|
+
subscribe,
|
|
95
|
+
() => resultCacheRef.current,
|
|
96
|
+
() => defaultState,
|
|
97
|
+
);
|
|
98
|
+
return { state, query };
|
|
99
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useTimeout() {
|
|
4
|
+
const timeoutRef = useRef(null);
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
clear();
|
|
8
|
+
}, []);
|
|
9
|
+
|
|
10
|
+
function set(delay: number, fn: () => void) {
|
|
11
|
+
clearTimeout(timeoutRef.current);
|
|
12
|
+
timeoutRef.current = setTimeout(fn, delay);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function clear() {
|
|
16
|
+
clearTimeout(timeoutRef.current);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return { set, clear };
|
|
20
|
+
}
|
package/src/version.ts
ADDED
package/tsconfig.json
ADDED
package/vite.config.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import react from '@vitejs/plugin-react';
|
|
3
|
+
import { resolve } from 'path';
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
clearScreen: false,
|
|
7
|
+
plugins: [react()],
|
|
8
|
+
build: {
|
|
9
|
+
outDir: 'dist/standalone',
|
|
10
|
+
lib: {
|
|
11
|
+
formats: ['umd', 'es'],
|
|
12
|
+
// this is the file that exports our components
|
|
13
|
+
entry: resolve(__dirname, 'src', 'index.ts'),
|
|
14
|
+
name: 'instantReact',
|
|
15
|
+
fileName: 'index',
|
|
16
|
+
},
|
|
17
|
+
rollupOptions: {
|
|
18
|
+
// don't bundle react libs
|
|
19
|
+
// the user will have provided them already
|
|
20
|
+
// via script tags or import maps
|
|
21
|
+
external: ['react', 'react-dom'],
|
|
22
|
+
output: {
|
|
23
|
+
// Provide global variables to use in the UMD build
|
|
24
|
+
// for externalized deps
|
|
25
|
+
globals: {
|
|
26
|
+
react: 'React',
|
|
27
|
+
'react-dom': 'ReactDOM',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
define: {
|
|
33
|
+
'process.env': {},
|
|
34
|
+
},
|
|
35
|
+
});
|