@roko-app/instant-react-common 0.0.0-stage → 1.0.67-roko.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/LICENSE +202 -0
- package/README.md +59 -2
- package/dist/commonjs/InstantReactAbstractDatabase.d.ts +283 -0
- package/dist/commonjs/InstantReactAbstractDatabase.d.ts.map +1 -0
- package/dist/commonjs/InstantReactAbstractDatabase.js +390 -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 +290 -0
- package/dist/commonjs/InstantReactRoom.js.map +1 -0
- package/dist/commonjs/index.d.ts +5 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +11 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/useInfiniteQuerySubscription.d.ts +28 -0
- package/dist/commonjs/useInfiniteQuerySubscription.d.ts.map +1 -0
- package/dist/commonjs/useInfiniteQuerySubscription.js +57 -0
- package/dist/commonjs/useInfiniteQuerySubscription.js.map +1 -0
- package/dist/commonjs/useQuery.d.ts +7 -0
- package/dist/commonjs/useQuery.d.ts.map +1 -0
- package/dist/commonjs/useQuery.js +79 -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 +21 -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 +283 -0
- package/dist/esm/InstantReactAbstractDatabase.d.ts.map +1 -0
- package/dist/esm/InstantReactAbstractDatabase.js +387 -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 +281 -0
- package/dist/esm/InstantReactRoom.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/useInfiniteQuerySubscription.d.ts +28 -0
- package/dist/esm/useInfiniteQuerySubscription.d.ts.map +1 -0
- package/dist/esm/useInfiniteQuerySubscription.js +54 -0
- package/dist/esm/useInfiniteQuerySubscription.js.map +1 -0
- package/dist/esm/useQuery.d.ts +7 -0
- package/dist/esm/useQuery.d.ts.map +1 -0
- package/dist/esm/useQuery.js +76 -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 +18 -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 +8718 -0
- package/dist/standalone/index.umd.cjs +36 -0
- package/package.json +43 -3
- package/src/InstantReactAbstractDatabase.tsx +510 -0
- package/src/InstantReactRoom.ts +451 -0
- package/src/index.ts +9 -0
- package/src/useInfiniteQuerySubscription.ts +128 -0
- package/src/useQuery.ts +124 -0
- package/src/useTimeout.ts +22 -0
- package/src/version.ts +3 -0
- package/tsconfig.cjs.dev.json +15 -0
- package/tsconfig.dev.json +14 -0
- package/tsconfig.json +12 -0
- package/vite.config.ts +35 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InstantReactRoom = exports.rooms = exports.defaultActivityStopTimeout = void 0;
|
|
4
|
+
exports.useTopicEffect = useTopicEffect;
|
|
5
|
+
exports.usePublishTopic = usePublishTopic;
|
|
6
|
+
exports.usePresence = usePresence;
|
|
7
|
+
exports.useSyncPresence = useSyncPresence;
|
|
8
|
+
exports.useTypingIndicator = useTypingIndicator;
|
|
9
|
+
const react_1 = require("react");
|
|
10
|
+
const useTimeout_ts_1 = require("./useTimeout.js");
|
|
11
|
+
exports.defaultActivityStopTimeout = 1_000;
|
|
12
|
+
// ------
|
|
13
|
+
// Topics
|
|
14
|
+
/**
|
|
15
|
+
* Listen for broadcasted events given a room and topic.
|
|
16
|
+
*
|
|
17
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
18
|
+
* @example
|
|
19
|
+
* function App({ roomId }) {
|
|
20
|
+
* const room = db.room('chats', roomId);
|
|
21
|
+
* db.rooms.useTopicEffect(room, 'emoji', (message, peer) => {
|
|
22
|
+
* console.log(peer.name, 'sent', message);
|
|
23
|
+
* });
|
|
24
|
+
* // ...
|
|
25
|
+
* }
|
|
26
|
+
*/
|
|
27
|
+
function useTopicEffect(room, topic, onEvent) {
|
|
28
|
+
const onEventRef = (0, react_1.useRef)(onEvent);
|
|
29
|
+
onEventRef.current = onEvent;
|
|
30
|
+
(0, react_1.useEffect)(() => {
|
|
31
|
+
const unsub = room.core._reactor.subscribeTopic(room.type, room.id, topic, (event, peer) => {
|
|
32
|
+
onEventRef.current(event, peer);
|
|
33
|
+
});
|
|
34
|
+
return unsub;
|
|
35
|
+
}, [room.id, topic]);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Broadcast an event to a room.
|
|
39
|
+
*
|
|
40
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
41
|
+
* @example
|
|
42
|
+
* function App({ roomId }) {
|
|
43
|
+
* const room = db.room('chat', roomId);
|
|
44
|
+
* const publishTopic = db.rooms.usePublishTopic(room, "emoji");
|
|
45
|
+
*
|
|
46
|
+
* return (
|
|
47
|
+
* <button onClick={() => publishTopic({ emoji: "🔥" })}>Send emoji</button>
|
|
48
|
+
* );
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
function usePublishTopic(room, topic) {
|
|
53
|
+
(0, react_1.useEffect)(() => room.core._reactor.joinRoom(room.type, room.id), [room.id]);
|
|
54
|
+
const publishTopic = (0, react_1.useCallback)((data) => {
|
|
55
|
+
room.core._reactor.publishTopic({
|
|
56
|
+
roomType: room.type,
|
|
57
|
+
roomId: room.id,
|
|
58
|
+
topic,
|
|
59
|
+
data,
|
|
60
|
+
});
|
|
61
|
+
}, [room.id, topic]);
|
|
62
|
+
return publishTopic;
|
|
63
|
+
}
|
|
64
|
+
// ---------
|
|
65
|
+
// Presence
|
|
66
|
+
/**
|
|
67
|
+
* Listen for peer's presence data in a room, and publish the current user's presence.
|
|
68
|
+
*
|
|
69
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
70
|
+
* @example
|
|
71
|
+
* function App({ roomId }) {
|
|
72
|
+
* const {
|
|
73
|
+
* peers,
|
|
74
|
+
* publishPresence
|
|
75
|
+
* } = db.room(roomType, roomId).usePresence({ keys: ["name", "avatar"] });
|
|
76
|
+
*
|
|
77
|
+
* // ...
|
|
78
|
+
* }
|
|
79
|
+
*/
|
|
80
|
+
function usePresence(room, opts = {}) {
|
|
81
|
+
const [state, setState] = (0, react_1.useState)(() => {
|
|
82
|
+
return (room.core._reactor.getPresence(room.type, room.id, opts) ?? {
|
|
83
|
+
peers: {},
|
|
84
|
+
isLoading: true,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
(0, react_1.useEffect)(() => {
|
|
88
|
+
const unsub = room.core._reactor.subscribePresence(room.type, room.id, opts, (data) => {
|
|
89
|
+
setState(data);
|
|
90
|
+
});
|
|
91
|
+
return unsub;
|
|
92
|
+
}, [room.id, opts.user, opts.peers?.join(), opts.keys?.join()]);
|
|
93
|
+
const publishPresence = (0, react_1.useCallback)((data) => {
|
|
94
|
+
room.core._reactor.publishPresence(room.type, room.id, data);
|
|
95
|
+
}, [room.type, room.id]);
|
|
96
|
+
const ret = (0, react_1.useMemo)(() => {
|
|
97
|
+
return {
|
|
98
|
+
...state,
|
|
99
|
+
publishPresence,
|
|
100
|
+
};
|
|
101
|
+
}, [state, publishPresence]);
|
|
102
|
+
return ret;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Publishes presence data to a room
|
|
106
|
+
*
|
|
107
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
108
|
+
* @example
|
|
109
|
+
* function App({ roomId, nickname }) {
|
|
110
|
+
* const room = db.room('chat', roomId);
|
|
111
|
+
* db.rooms.useSyncPresence(room, { nickname });
|
|
112
|
+
* }
|
|
113
|
+
*/
|
|
114
|
+
function useSyncPresence(room, data, deps) {
|
|
115
|
+
(0, react_1.useEffect)(() => room.core._reactor.joinRoom(room.type, room.id, data), [room.id]);
|
|
116
|
+
(0, react_1.useEffect)(() => {
|
|
117
|
+
return room.core._reactor.publishPresence(room.type, room.id, data);
|
|
118
|
+
}, [room.type, room.id, deps ?? JSON.stringify(data)]);
|
|
119
|
+
}
|
|
120
|
+
// -----------------
|
|
121
|
+
// Typing Indicator
|
|
122
|
+
/**
|
|
123
|
+
* Manage typing indicator state
|
|
124
|
+
*
|
|
125
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
126
|
+
* @example
|
|
127
|
+
* function App({ roomId }) {
|
|
128
|
+
* const room = db.room('chat', roomId);
|
|
129
|
+
* const {
|
|
130
|
+
* active,
|
|
131
|
+
* setActive,
|
|
132
|
+
* inputProps,
|
|
133
|
+
* } = db.rooms.useTypingIndicator(room, "chat-input");
|
|
134
|
+
*
|
|
135
|
+
* return <input {...inputProps} />;
|
|
136
|
+
* }
|
|
137
|
+
*/
|
|
138
|
+
function useTypingIndicator(room, inputName, opts = {}) {
|
|
139
|
+
const timeout = (0, useTimeout_ts_1.useTimeout)();
|
|
140
|
+
const observedPresence = exports.rooms.usePresence(room, {
|
|
141
|
+
keys: [inputName],
|
|
142
|
+
});
|
|
143
|
+
const active = (0, react_1.useMemo)(() => {
|
|
144
|
+
const presenceSnapshot = room._core._reactor.getPresence(room.type, room.id);
|
|
145
|
+
return opts?.writeOnly
|
|
146
|
+
? []
|
|
147
|
+
: Object.values(presenceSnapshot?.peers ?? {}).filter((p) => p[inputName] === true);
|
|
148
|
+
}, [opts?.writeOnly, observedPresence]);
|
|
149
|
+
const setActive = (0, react_1.useCallback)((isActive) => {
|
|
150
|
+
room.core._reactor.publishPresence(room.type, room.id, {
|
|
151
|
+
[inputName]: isActive,
|
|
152
|
+
});
|
|
153
|
+
if (!isActive)
|
|
154
|
+
return;
|
|
155
|
+
if (opts?.timeout === null || opts?.timeout === 0)
|
|
156
|
+
return;
|
|
157
|
+
timeout.set(opts?.timeout ?? exports.defaultActivityStopTimeout, () => {
|
|
158
|
+
room.core._reactor.publishPresence(room.type, room.id, {
|
|
159
|
+
[inputName]: null,
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
}, [room.type, room.id, inputName, opts?.timeout, timeout]);
|
|
163
|
+
const onKeyDown = (0, react_1.useCallback)((e) => {
|
|
164
|
+
const isEnter = opts?.stopOnEnter && e.key === 'Enter';
|
|
165
|
+
const isActive = !isEnter;
|
|
166
|
+
setActive(isActive);
|
|
167
|
+
}, [opts.stopOnEnter, setActive]);
|
|
168
|
+
const onBlur = (0, react_1.useCallback)(() => {
|
|
169
|
+
setActive(false);
|
|
170
|
+
}, [setActive]);
|
|
171
|
+
const inputProps = (0, react_1.useMemo)(() => {
|
|
172
|
+
return { onKeyDown, onBlur };
|
|
173
|
+
}, [onKeyDown, onBlur]);
|
|
174
|
+
return {
|
|
175
|
+
active,
|
|
176
|
+
setActive,
|
|
177
|
+
inputProps,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
// --------------
|
|
181
|
+
// Hooks
|
|
182
|
+
exports.rooms = {
|
|
183
|
+
useTopicEffect,
|
|
184
|
+
usePublishTopic,
|
|
185
|
+
usePresence,
|
|
186
|
+
useSyncPresence,
|
|
187
|
+
useTypingIndicator,
|
|
188
|
+
};
|
|
189
|
+
// ------------
|
|
190
|
+
// Class
|
|
191
|
+
class InstantReactRoom {
|
|
192
|
+
core;
|
|
193
|
+
/** @deprecated use `core` instead */
|
|
194
|
+
_core;
|
|
195
|
+
type;
|
|
196
|
+
id;
|
|
197
|
+
constructor(core, type, id) {
|
|
198
|
+
this.core = core;
|
|
199
|
+
this._core = this.core;
|
|
200
|
+
this.type = type;
|
|
201
|
+
this.id = id;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* @deprecated
|
|
205
|
+
* `db.room(...).useTopicEffect` is deprecated. You can replace it with `db.rooms.useTopicEffect`.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
*
|
|
209
|
+
* // Before
|
|
210
|
+
* const room = db.room('chat', 'room-id');
|
|
211
|
+
* room.useTopicEffect('emoji', (message, peer) => { });
|
|
212
|
+
*
|
|
213
|
+
* // After
|
|
214
|
+
* const room = db.room('chat', 'room-id');
|
|
215
|
+
* db.rooms.useTopicEffect(room, 'emoji', (message, peer) => { });
|
|
216
|
+
*/
|
|
217
|
+
useTopicEffect = (topic, onEvent) => {
|
|
218
|
+
exports.rooms.useTopicEffect(this, topic, onEvent);
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* @deprecated
|
|
222
|
+
* `db.room(...).usePublishTopic` is deprecated. You can replace it with `db.rooms.usePublishTopic`.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
*
|
|
226
|
+
* // Before
|
|
227
|
+
* const room = db.room('chat', 'room-id');
|
|
228
|
+
* const publish = room.usePublishTopic('emoji');
|
|
229
|
+
*
|
|
230
|
+
* // After
|
|
231
|
+
* const room = db.room('chat', 'room-id');
|
|
232
|
+
* const publish = db.rooms.usePublishTopic(room, 'emoji');
|
|
233
|
+
*/
|
|
234
|
+
usePublishTopic = (topic) => {
|
|
235
|
+
return exports.rooms.usePublishTopic(this, topic);
|
|
236
|
+
};
|
|
237
|
+
/**
|
|
238
|
+
* @deprecated
|
|
239
|
+
* `db.room(...).usePresence` is deprecated. You can replace it with `db.rooms.usePresence`.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
*
|
|
243
|
+
* // Before
|
|
244
|
+
* const room = db.room('chat', 'room-id');
|
|
245
|
+
* const { peers } = room.usePresence({ keys: ["name", "avatar"] });
|
|
246
|
+
*
|
|
247
|
+
* // After
|
|
248
|
+
* const room = db.room('chat', 'room-id');
|
|
249
|
+
* const { peers } = db.rooms.usePresence(room, { keys: ["name", "avatar"] });
|
|
250
|
+
*/
|
|
251
|
+
usePresence = (opts = {}) => {
|
|
252
|
+
return exports.rooms.usePresence(this, opts);
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* @deprecated
|
|
256
|
+
* `db.room(...).useSyncPresence` is deprecated. You can replace it with `db.rooms.useSyncPresence`.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
*
|
|
260
|
+
* // Before
|
|
261
|
+
* const room = db.room('chat', 'room-id');
|
|
262
|
+
* room.useSyncPresence(room, { nickname });
|
|
263
|
+
*
|
|
264
|
+
* // After
|
|
265
|
+
* const room = db.room('chat', 'room-id');
|
|
266
|
+
* db.rooms.useSyncPresence(room, { nickname });
|
|
267
|
+
*/
|
|
268
|
+
useSyncPresence = (data, deps) => {
|
|
269
|
+
return exports.rooms.useSyncPresence(this, data, deps);
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* @deprecated
|
|
273
|
+
* `db.room(...).useTypingIndicator` is deprecated. You can replace it with `db.rooms.useTypingIndicator`.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
*
|
|
277
|
+
* // Before
|
|
278
|
+
* const room = db.room('chat', 'room-id');
|
|
279
|
+
* const typing = room.useTypingIndiactor(room, 'chat-input');
|
|
280
|
+
*
|
|
281
|
+
* // After
|
|
282
|
+
* const room = db.room('chat', 'room-id');
|
|
283
|
+
* const typing = db.rooms.useTypingIndiactor(room, 'chat-input');
|
|
284
|
+
*/
|
|
285
|
+
useTypingIndicator = (inputName, opts = {}) => {
|
|
286
|
+
return exports.rooms.useTypingIndicator(this, inputName, opts);
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
exports.InstantReactRoom = InstantReactRoom;
|
|
290
|
+
//# sourceMappingURL=InstantReactRoom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InstantReactRoom.js","sourceRoot":"","sources":["../../src/InstantReactRoom.ts"],"names":[],"mappings":";;;AA6DA,wCA2BC;AAiBD,0CAuBC;AAmBD,kCA6CC;AAYD,0CAeC;AAqBD,gDAmEC;AA1SD,iCAOe;AAEf,mDAA6C;AAyBhC,QAAA,0BAA0B,GAAG,KAAK,CAAC;AAEhD,SAAS;AACT,SAAS;AAET;;;;;;;;;;;;GAYG;AACH,SAAgB,cAAc,CAK5B,IAAiD,EACjD,KAAgB,EAChB,OAGQ;IAER,MAAM,UAAU,GAAG,IAAA,cAAM,EAAC,OAAO,CAAC,CAAC;IACnC,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,IAAA,iBAAS,EAAC,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,CAAC;AAED;;;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Database,\n InstantSchemaDef,\n} from '@instantdb/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://instantdb.com/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: InstantReactRoom<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://instantdb.com/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: InstantReactRoom<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://instantdb.com/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: InstantReactRoom<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://instantdb.com/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: InstantReactRoom<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://instantdb.com/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: InstantReactRoom<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 InstantReactRoom<\n Schema extends InstantSchemaDef<any, any, any>,\n RoomSchema extends RoomSchemaShape,\n RoomType extends string & keyof RoomSchema,\n> {\n core: InstantCoreDatabase<Schema, boolean>;\n /** @deprecated use `core` instead */\n _core: InstantCoreDatabase<Schema, boolean>;\n type: RoomType;\n id: string;\n\n constructor(\n core: InstantCoreDatabase<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"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import InstantReactAbstractDatabase from './InstantReactAbstractDatabase.tsx';
|
|
2
|
+
import type { InstantReactRoom } from './InstantReactRoom.ts';
|
|
3
|
+
import { useQueryInternal } from './useQuery.ts';
|
|
4
|
+
export { InstantReactAbstractDatabase, type InstantReactRoom, useQueryInternal, };
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,oCAAoC,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EACL,4BAA4B,EAC5B,KAAK,gBAAgB,EACrB,gBAAgB,GACjB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useQueryInternal = exports.InstantReactAbstractDatabase = void 0;
|
|
7
|
+
const InstantReactAbstractDatabase_tsx_1 = __importDefault(require("./InstantReactAbstractDatabase.js"));
|
|
8
|
+
exports.InstantReactAbstractDatabase = InstantReactAbstractDatabase_tsx_1.default;
|
|
9
|
+
const useQuery_ts_1 = require("./useQuery.js");
|
|
10
|
+
Object.defineProperty(exports, "useQueryInternal", { enumerable: true, get: function () { return useQuery_ts_1.useQueryInternal; } });
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,yGAA8E;AAK5E,uCALK,0CAA4B,CAKL;AAH9B,+CAAiD;AAK/C,iGALO,8BAAgB,OAKP","sourcesContent":["import InstantReactAbstractDatabase from './InstantReactAbstractDatabase.tsx';\nimport type { InstantReactRoom } from './InstantReactRoom.ts';\nimport { useQueryInternal } from './useQuery.ts';\n\nexport {\n InstantReactAbstractDatabase,\n type InstantReactRoom,\n useQueryInternal,\n};\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type InstantCoreDatabase, type InstantSchemaDef, type InstaQLOptions, InstaQLResponse, ValidQuery } from '@instantdb/core';
|
|
2
|
+
export type InfiniteQueryResult<Schema extends InstantSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = {
|
|
3
|
+
error: {
|
|
4
|
+
message: string;
|
|
5
|
+
};
|
|
6
|
+
data: undefined;
|
|
7
|
+
isLoading: false;
|
|
8
|
+
canLoadNextPage: boolean;
|
|
9
|
+
loadNextPage: () => void;
|
|
10
|
+
} | {
|
|
11
|
+
error: undefined;
|
|
12
|
+
data: undefined;
|
|
13
|
+
isLoading: true;
|
|
14
|
+
canLoadNextPage: boolean;
|
|
15
|
+
loadNextPage: () => void;
|
|
16
|
+
} | {
|
|
17
|
+
error: undefined;
|
|
18
|
+
data: InstaQLResponse<Schema, Q, UseDates>;
|
|
19
|
+
isLoading: false;
|
|
20
|
+
canLoadNextPage: boolean;
|
|
21
|
+
loadNextPage: () => void;
|
|
22
|
+
};
|
|
23
|
+
export declare function useInfiniteQuerySubscription<Schema extends InstantSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean>({ core, query, opts, }: {
|
|
24
|
+
core: InstantCoreDatabase<Schema, UseDates>;
|
|
25
|
+
query: Q | null;
|
|
26
|
+
opts?: InstaQLOptions;
|
|
27
|
+
}): InfiniteQueryResult<Schema, Q, UseDates>;
|
|
28
|
+
//# sourceMappingURL=useInfiniteQuerySubscription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInfiniteQuerySubscription.d.ts","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,eAAe,EACf,UAAU,EAGX,MAAM,iBAAiB,CAAC;AAGzB,MAAM,MAAM,mBAAmB,CAC7B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,QAAQ,SAAS,OAAO,IAEtB;IACE,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3B,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,KAAK,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,GACD;IACE,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,GACD;IACE,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC3C,SAAS,EAAE,KAAK,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B,CAAC;AASN,wBAAgB,4BAA4B,CAC1C,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,QAAQ,SAAS,OAAO,EACxB,EACA,IAAI,EACJ,KAAK,EACL,IAAI,GACL,EAAE;IACD,IAAI,EAAE,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC5C,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAChB,IAAI,CAAC,EAAE,cAAc,CAAC;CACvB,GAAG,mBAAmB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAqE3C"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useInfiniteQuerySubscription = useInfiniteQuerySubscription;
|
|
4
|
+
const core_1 = require("@instantdb/core");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const defaultState = {
|
|
7
|
+
error: undefined,
|
|
8
|
+
data: undefined,
|
|
9
|
+
isLoading: true,
|
|
10
|
+
canLoadNextPage: false,
|
|
11
|
+
};
|
|
12
|
+
function useInfiniteQuerySubscription({ core, query, opts, }) {
|
|
13
|
+
const subRef = (0, react_1.useRef)(null);
|
|
14
|
+
const queryHash = (0, core_1.weakHash)(query);
|
|
15
|
+
const optsHash = (0, core_1.weakHash)(opts);
|
|
16
|
+
const snapshot = (0, core_1.getInfiniteQueryInitialSnapshot)(core, query, opts);
|
|
17
|
+
const stateCacheRef = (0, react_1.useRef)({
|
|
18
|
+
...snapshot,
|
|
19
|
+
isLoading: !snapshot.data && !snapshot.error,
|
|
20
|
+
});
|
|
21
|
+
const subscribe = (0, react_1.useCallback)((cb) => {
|
|
22
|
+
subRef.current = null;
|
|
23
|
+
const nextSnapshot = (0, core_1.getInfiniteQueryInitialSnapshot)(core, query, opts);
|
|
24
|
+
stateCacheRef.current = {
|
|
25
|
+
...nextSnapshot,
|
|
26
|
+
isLoading: !nextSnapshot.data && !nextSnapshot.error,
|
|
27
|
+
};
|
|
28
|
+
cb();
|
|
29
|
+
if (!query) {
|
|
30
|
+
return () => { };
|
|
31
|
+
}
|
|
32
|
+
const sub = core.subscribeInfiniteQuery(query, (resp) => {
|
|
33
|
+
stateCacheRef.current = {
|
|
34
|
+
...resp,
|
|
35
|
+
isLoading: false,
|
|
36
|
+
};
|
|
37
|
+
cb();
|
|
38
|
+
}, opts);
|
|
39
|
+
subRef.current = sub;
|
|
40
|
+
return () => {
|
|
41
|
+
sub.unsubscribe();
|
|
42
|
+
if (subRef.current === sub) {
|
|
43
|
+
subRef.current = null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}, [queryHash, optsHash]);
|
|
47
|
+
const state = (0, react_1.useSyncExternalStore)(subscribe, () => stateCacheRef.current, () => defaultState);
|
|
48
|
+
const loadNextPage = () => {
|
|
49
|
+
subRef.current?.loadNextPage();
|
|
50
|
+
};
|
|
51
|
+
// @ts-expect-error union type
|
|
52
|
+
return {
|
|
53
|
+
...state,
|
|
54
|
+
loadNextPage,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=useInfiniteQuerySubscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInfiniteQuerySubscription.js","sourceRoot":"","sources":["../../src/useInfiniteQuerySubscription.ts"],"names":[],"mappings":";;AA8CA,oEAiFC;AA/HD,0CASyB;AACzB,iCAAkE;AA6BlE,MAAM,YAAY,GAAG;IACnB,KAAK,EAAE,SAAS;IAChB,IAAI,EAAE,SAAS;IACf,SAAS,EAAE,IAAI;IACf,eAAe,EAAE,KAAK;CACvB,CAAC;AAEF,SAAgB,4BAA4B,CAI1C,EACA,IAAI,EACJ,KAAK,EACL,IAAI,GAKL;IACC,MAAM,MAAM,GAAG,IAAA,cAAM,EAAmC,IAAI,CAAC,CAAC;IAE9D,MAAM,SAAS,GAAG,IAAA,eAAQ,EAAC,KAAK,CAAC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AAS,EAAE,QAAQ,CAAC,CACtB,CAAC;IAEF,MAAM,KAAK,GAAG,IAAA,4BAAoB,EAChC,SAAS,EACT,GAAG,EAAE,CAAC,aAAa,CAAC,OAAO,EAC3B,GAAG,EAAE,CAAC,YAAY,CACnB,CAAC;IAEF,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC;IACjC,CAAC,CAAC;IAEF,8BAA8B;IAC9B,OAAO;QACL,GAAG,KAAK;QACR,YAAY;KACb,CAAC;AACJ,CAAC","sourcesContent":["import {\n type InfiniteQuerySubscription,\n type InstantCoreDatabase,\n type InstantSchemaDef,\n type InstaQLOptions,\n InstaQLResponse,\n ValidQuery,\n weakHash,\n getInfiniteQueryInitialSnapshot,\n} from '@instantdb/core';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\n\nexport type InfiniteQueryResult<\n Schema extends InstantSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n> =\n | {\n error: { message: string };\n data: undefined;\n isLoading: false;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n }\n | {\n error: undefined;\n data: undefined;\n isLoading: true;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n }\n | {\n error: undefined;\n data: InstaQLResponse<Schema, Q, UseDates>;\n isLoading: false;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n };\n\nconst defaultState = {\n error: undefined,\n data: undefined,\n isLoading: true,\n canLoadNextPage: false,\n};\n\nexport function useInfiniteQuerySubscription<\n Schema extends InstantSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n>({\n core,\n query,\n opts,\n}: {\n core: InstantCoreDatabase<Schema, UseDates>;\n query: Q | null;\n opts?: InstaQLOptions;\n}): InfiniteQueryResult<Schema, Q, UseDates> {\n const subRef = useRef<InfiniteQuerySubscription | null>(null);\n\n const queryHash = weakHash(query);\n const optsHash = weakHash(opts);\n const snapshot = getInfiniteQueryInitialSnapshot(core, query, opts);\n\n const stateCacheRef = useRef<\n Omit<InfiniteQueryResult<Schema, Q, UseDates>, 'loadNextPage'>\n >({\n ...snapshot,\n isLoading: !snapshot.data && !snapshot.error,\n });\n\n const subscribe = useCallback(\n (cb: () => void) => {\n subRef.current = null;\n\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"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type InstaQLOptions, InstantCoreDatabase, InstaQLLifecycleState, InstantSchemaDef, ValidQuery } from '@instantdb/core';
|
|
2
|
+
export declare function useQueryInternal<Q extends ValidQuery<Q, Schema>, Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean>(_core: InstantCoreDatabase<Schema, UseDates>, _query: null | Q, _opts?: InstaQLOptions, serverSnapshot?: () => InstaQLLifecycleState<Schema, Q, UseDates> | undefined): {
|
|
3
|
+
state: InstaQLLifecycleState<Schema, Q, UseDates>;
|
|
4
|
+
query: any;
|
|
5
|
+
queryHash: string;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=useQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,cAAc,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,UAAU,EACX,MAAM,iBAAiB,CAAC;AAoBzB,wBAAgB,gBAAgB,CAC9B,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,QAAQ,SAAS,OAAO,EAExB,KAAK,EAAE,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC5C,MAAM,EAAE,IAAI,GAAG,CAAC,EAChB,KAAK,CAAC,EAAE,cAAc,EACtB,cAAc,CAAC,EAAE,MAAM,qBAAqB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,SAAS,GAC5E;IACD,KAAK,EAAE,qBAAqB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAClD,KAAK,EAAE,GAAG,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;CACnB,CAgFA"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useQueryInternal = useQueryInternal;
|
|
5
|
+
const core_1 = require("@instantdb/core");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const defaultState = {
|
|
8
|
+
isLoading: true,
|
|
9
|
+
data: undefined,
|
|
10
|
+
pageInfo: undefined,
|
|
11
|
+
error: undefined,
|
|
12
|
+
};
|
|
13
|
+
function stateForResult(result) {
|
|
14
|
+
return {
|
|
15
|
+
isLoading: !Boolean(result),
|
|
16
|
+
data: undefined,
|
|
17
|
+
pageInfo: undefined,
|
|
18
|
+
error: undefined,
|
|
19
|
+
...(result ? result : {}),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function useQueryInternal(_core, _query, _opts, serverSnapshot) {
|
|
23
|
+
if (_query && _opts && 'ruleParams' in _opts) {
|
|
24
|
+
_query = { $$ruleParams: _opts['ruleParams'], ..._query };
|
|
25
|
+
}
|
|
26
|
+
const query = _query ? (0, core_1.coerceQuery)(_query) : null;
|
|
27
|
+
const queryHash = (0, core_1.weakHash)(query);
|
|
28
|
+
// We use a ref to store the result of the query.
|
|
29
|
+
// This is becuase `useSyncExternalStore` uses `Object.is`
|
|
30
|
+
// to compare the previous and next state.
|
|
31
|
+
// If we don't use a ref, the state will always be considered different, so
|
|
32
|
+
// the component will always re-render.
|
|
33
|
+
const resultCacheRef = (0, react_1.useRef)(stateForResult(_core._reactor.getPreviousResult(query)));
|
|
34
|
+
// Cache the server snapshot to satisfy useSyncExternalStore's requirement
|
|
35
|
+
// that getServerSnapshot returns the same reference across calls.
|
|
36
|
+
const serverSnapshotCacheRef = (0, react_1.useRef)(defaultState);
|
|
37
|
+
// Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe
|
|
38
|
+
// if `subscribe` changes, so we use `useCallback` to memoize the function.
|
|
39
|
+
const subscribe = (0, react_1.useCallback)((cb) => {
|
|
40
|
+
// Update the ref when the query changes to avoid showing stale data
|
|
41
|
+
resultCacheRef.current = stateForResult(_core._reactor.getPreviousResult(query));
|
|
42
|
+
// before the subscribeQuery is connected and calls it's own callback,
|
|
43
|
+
// we might have data in the store via SSR, we need to notify useSyncExternalStore that
|
|
44
|
+
// the data has changed.
|
|
45
|
+
cb();
|
|
46
|
+
// Don't subscribe if query is null
|
|
47
|
+
if (!query) {
|
|
48
|
+
const unsubscribe = () => { };
|
|
49
|
+
return unsubscribe;
|
|
50
|
+
}
|
|
51
|
+
const unsubscribe = _core.subscribeQuery(query, (result) => {
|
|
52
|
+
resultCacheRef.current = {
|
|
53
|
+
isLoading: !Boolean(result),
|
|
54
|
+
// @ts-expect-error: ts thinks this will always be overwritten
|
|
55
|
+
data: undefined,
|
|
56
|
+
// @ts-expect-error: ts thinks this will always be overwritten
|
|
57
|
+
pageInfo: undefined,
|
|
58
|
+
// @ts-expect-error: ts thinks this will always be overwritten
|
|
59
|
+
error: undefined,
|
|
60
|
+
...result,
|
|
61
|
+
};
|
|
62
|
+
cb();
|
|
63
|
+
});
|
|
64
|
+
return unsubscribe;
|
|
65
|
+
},
|
|
66
|
+
// Build a new subscribe function if the query changes
|
|
67
|
+
[queryHash]);
|
|
68
|
+
const state = (0, react_1.useSyncExternalStore)(subscribe, () => resultCacheRef.current, () => {
|
|
69
|
+
if (serverSnapshot && serverSnapshotCacheRef.current === defaultState) {
|
|
70
|
+
const result = serverSnapshot();
|
|
71
|
+
if (result) {
|
|
72
|
+
serverSnapshotCacheRef.current = result;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return serverSnapshotCacheRef.current;
|
|
76
|
+
});
|
|
77
|
+
return { state, query, queryHash };
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=useQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AA8Bb,4CA6FC;AAzHD,0CAQyB;AACzB,iCAAkE;AAElE,MAAM,YAAY,GAAG;IACnB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,QAAQ,EAAE,SAAS;IACnB,KAAK,EAAE,SAAS;CACR,CAAC;AAEX,SAAS,cAAc,CAAC,MAAW;IACjC,OAAO;QACL,SAAS,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC;QAC3B,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,SAAS;QACnB,KAAK,EAAE,SAAS;QAChB,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;KAC1B,CAAC;AACJ,CAAC;AAED,SAAgB,gBAAgB,CAK9B,KAA4C,EAC5C,MAAgB,EAChB,KAAsB,EACtB,cAA6E;IAM7E,IAAI,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AC9D,IAAI,EAAE,SAAS;gBACf,8DAA8D;gBAC9D,QAAQ,EAAE,SAAS;gBACnB,8DAA8D;gBAC9D,KAAK,EAAE,SAAS;gBAChB,GAAG,MAAM;aACV,CAAC;YAEF,EAAE,EAAE,CAAC;QACP,CAAC,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACrB,CAAC;IACD,sDAAsD;IACtD,CAAC,SAAS,CAAC,CACZ,CAAC;IAEF,MAAM,KAAK,GAAG,IAAA,4BAAoB,EAGhC,SAAS,EACT,GAAG,EAAE,CAAC,cAAc,CAAC,OAAO,EAC5B,GAAG,EAAE;QACH,IAAI,cAAc,IAAI,sBAAsB,CAAC,OAAO,KAAK,YAAY,EAAE,CAAC;YACtE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;YAChC,IAAI,MAAM,EAAE,CAAC;gBACX,sBAAsB,CAAC,OAAO,GAAG,MAAM,CAAC;YAC1C,CAAC;QACH,CAAC;QAED,OAAO,sBAAsB,CAAC,OAAO,CAAC;IACxC,CAAC,CACF,CAAC;IAEF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AACrC,CAAC","sourcesContent":["'use client';\n\nimport {\n weakHash,\n coerceQuery,\n type InstaQLOptions,\n InstantCoreDatabase,\n InstaQLLifecycleState,\n InstantSchemaDef,\n ValidQuery,\n} from '@instantdb/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 InstantSchemaDef<any, any, any>,\n UseDates extends boolean,\n>(\n _core: InstantCoreDatabase<Schema, UseDates>,\n _query: null | Q,\n _opts?: InstaQLOptions,\n serverSnapshot?: () => InstaQLLifecycleState<Schema, Q, UseDates> | undefined,\n): {\n state: InstaQLLifecycleState<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 const resultCacheRef = useRef<InstaQLLifecycleState<Schema, Q, UseDates>>(\n stateForResult(_core._reactor.getPreviousResult(query)),\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 InstaQLLifecycleState<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<\n InstaQLLifecycleState<Schema, Q, UseDates>\n >(\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTimeout.d.ts","sourceRoot":"","sources":["../../src/useTimeout.ts"],"names":[],"mappings":"AAEA,wBAAgB,UAAU;iBAOJ,MAAM,MAAM,MAAM,IAAI;;EAY3C"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useTimeout = useTimeout;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useTimeout() {
|
|
6
|
+
const timeoutRef = (0, react_1.useRef)(null);
|
|
7
|
+
(0, react_1.useEffect)(() => {
|
|
8
|
+
clear();
|
|
9
|
+
}, []);
|
|
10
|
+
function set(delay, fn) {
|
|
11
|
+
clear();
|
|
12
|
+
timeoutRef.current = setTimeout(fn, delay);
|
|
13
|
+
}
|
|
14
|
+
function clear() {
|
|
15
|
+
if (timeoutRef.current) {
|
|
16
|
+
clearTimeout(timeoutRef.current);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return { set, clear };
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=useTimeout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTimeout.js","sourceRoot":"","sources":["../../src/useTimeout.ts"],"names":[],"mappings":";;AAEA,gCAmBC;AArBD,iCAA0C;AAE1C,SAAgB,UAAU;IACxB,MAAM,UAAU,GAAG,IAAA,cAAM,EAAuC,IAAI,CAAC,CAAC;IAEtE,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,KAAK,EAAE,CAAC;IACV,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,SAAS,GAAG,CAAC,KAAa,EAAE,EAAc;QACxC,KAAK,EAAE,CAAC;QACR,UAAU,CAAC,OAAO,GAAG,UAAU,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED,SAAS,KAAK;QACZ,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;YACvB,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;IAED,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AACxB,CAAC","sourcesContent":["import { useEffect, useRef } from 'react';\n\nexport function useTimeout() {\n const timeoutRef = useRef<null | ReturnType<typeof setTimeout>>(null);\n\n useEffect(() => {\n clear();\n }, []);\n\n function set(delay: number, fn: () => void) {\n clear();\n timeoutRef.current = setTimeout(fn, delay);\n }\n\n function clear() {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n }\n\n return { set, clear };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,eAAe,OAAO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";;AAAA,gDAA6C;AAE7C,kBAAe,iBAAO,CAAC","sourcesContent":["import { version } from '@instantdb/version';\n\nexport default version;\n"]}
|