@tapedb/tapedb 0.0.0-stage → 0.1.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.
@@ -0,0 +1,41 @@
1
+ import { LiveState, Page, BroadcastTopic, AcrossQuery, AcrossLiveState, Query, PageQuery, PresenceRoom } from './index.js';
2
+ import { TopicSender, PresencePeer } from './index.js';
3
+ import './schema-entry-CpO_IGFv.js';
4
+
5
+ /**
6
+ * Live results for a query: `useLive(db.task.findMany({ where }))`.
7
+ *
8
+ * A new query object every render is fine: the subscription follows the
9
+ * query's key, not its identity, so equal arguments keep one subscription.
10
+ */
11
+ declare function useLive<T>(query: AcrossQuery<T>): AcrossLiveState<T>;
12
+ declare function useLive<T>(query: Query<T>): LiveState<T>;
13
+ interface LivePage<R> {
14
+ rows: R[];
15
+ hasMore: boolean;
16
+ isLoading: boolean;
17
+ error: LiveState<Page<R>>["error"];
18
+ /** Appends the next page; every loaded page stays live. */
19
+ loadMore(): Promise<boolean>;
20
+ }
21
+ /** Live pages: `useLivePage(db.task.findPage({ orderBy, take: 50 }))`. */
22
+ declare function useLivePage<R>(query: PageQuery<R>): LivePage<R>;
23
+ /**
24
+ * Joins a presence room with `presence` and returns the other peers. Leaves
25
+ * the room on unmount; publishes again whenever `presence` changes.
26
+ */
27
+ declare function usePresence(db: {
28
+ $presence(room: string): PresenceRoom;
29
+ }, room: string, presence?: unknown): PresencePeer[];
30
+ /**
31
+ * Listens on a broadcast topic while mounted and returns its `send`. The
32
+ * latest `onMessage` is always the one called, so it need not be memoised.
33
+ *
34
+ * @experimental May change in a minor release.
35
+ */
36
+ declare function useBroadcast<T = unknown>(db: {
37
+ $broadcast<M>(topic: string): BroadcastTopic<M>;
38
+ }, topic: string, onMessage?: (message: T, from: TopicSender) => void): (message: T) => boolean | "queued";
39
+
40
+ export { useBroadcast, useLive, useLivePage, usePresence };
41
+ export type { LivePage };
package/dist/react.js ADDED
@@ -0,0 +1,59 @@
1
+ "use client";
2
+ import {
3
+ stableStringify
4
+ } from "./chunk-UYZJAMBR.js";
5
+
6
+ // src/react/index.ts
7
+ import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
8
+ function useLive(query) {
9
+ const queryRef = useRef(query);
10
+ queryRef.current = query;
11
+ const subscribe = useMemo(
12
+ () => (onChange) => queryRef.current.subscribe(onChange),
13
+ // eslint-disable-next-line react-hooks/exhaustive-deps
14
+ [query.key]
15
+ );
16
+ const snapshot = () => queryRef.current.snapshot();
17
+ return useSyncExternalStore(subscribe, snapshot, snapshot);
18
+ }
19
+ function useLivePage(query) {
20
+ const state = useLive(query);
21
+ const queryRef = useRef(query);
22
+ queryRef.current = query;
23
+ const loadMore = useCallback(() => queryRef.current.loadMore(), []);
24
+ return {
25
+ rows: state.data?.rows ?? [],
26
+ hasMore: state.data?.hasMore ?? false,
27
+ isLoading: state.isLoading,
28
+ error: state.error,
29
+ loadMore
30
+ };
31
+ }
32
+ function usePresence(db, room, presence) {
33
+ const [peers, setPeers] = useState([]);
34
+ const handle = useMemo(() => db.$presence(room), [db, room]);
35
+ const serialised = presence === void 0 ? void 0 : stableStringify(presence);
36
+ useEffect(() => handle.subscribe(setPeers), [handle]);
37
+ useEffect(() => {
38
+ if (presence !== void 0) handle.set(presence);
39
+ }, [handle, serialised]);
40
+ useEffect(() => () => handle.leave(), [handle]);
41
+ return peers;
42
+ }
43
+ function useBroadcast(db, topic, onMessage) {
44
+ const handle = useMemo(() => db.$broadcast(topic), [db, topic]);
45
+ const listener = useRef(onMessage);
46
+ listener.current = onMessage;
47
+ useEffect(
48
+ () => handle.subscribe((message, from) => listener.current?.(message, from)),
49
+ [handle]
50
+ );
51
+ return useCallback((message) => handle.send(message), [handle]);
52
+ }
53
+ export {
54
+ useBroadcast,
55
+ useLive,
56
+ useLivePage,
57
+ usePresence
58
+ };
59
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/react/index.ts"],"sourcesContent":["\"use client\";\n\n/**\n * React bindings: `useLive`, `useLivePage`, `usePresence`.\n *\n * The only entry in the package that imports React. Hooks take the queries\n * and rooms a `createClient()` client returns, so hooks and plain calls share\n * one socket, one cache and one outbox.\n */\n\nimport { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from \"react\";\n\nimport { stableStringify } from \"../client/stable\";\nimport type { AcrossLiveState, AcrossQuery } from \"../model/across\";\nimport type { PresencePeer } from \"../client/types\";\nimport type { BroadcastTopic, LiveState, Page, PageQuery, PresenceRoom, Query } from \"../model/client\";\nimport type { TopicSender } from \"../client/types\";\n\n/**\n * Live results for a query: `useLive(db.task.findMany({ where }))`.\n *\n * A new query object every render is fine: the subscription follows the\n * query's key, not its identity, so equal arguments keep one subscription.\n */\nexport function useLive<T>(query: AcrossQuery<T>): AcrossLiveState<T>;\nexport function useLive<T>(query: Query<T>): LiveState<T>;\nexport function useLive<T>(query: Query<T> | AcrossQuery<T>): LiveState<T> {\n const queryRef = useRef(query);\n queryRef.current = query;\n\n const subscribe = useMemo(\n () => (onChange: () => void) => (queryRef.current as Query<T>).subscribe(onChange),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [query.key]\n );\n const snapshot = () => (queryRef.current as Query<T>).snapshot();\n return useSyncExternalStore(subscribe, snapshot, snapshot);\n}\n\nexport interface LivePage<R> {\n rows: R[];\n hasMore: boolean;\n isLoading: boolean;\n error: LiveState<Page<R>>[\"error\"];\n /** Appends the next page; every loaded page stays live. */\n loadMore(): Promise<boolean>;\n}\n\n/** Live pages: `useLivePage(db.task.findPage({ orderBy, take: 50 }))`. */\nexport function useLivePage<R>(query: PageQuery<R>): LivePage<R> {\n const state = useLive(query);\n const queryRef = useRef(query);\n queryRef.current = query;\n const loadMore = useCallback(() => queryRef.current.loadMore(), []);\n return {\n rows: state.data?.rows ?? [],\n hasMore: state.data?.hasMore ?? false,\n isLoading: state.isLoading,\n error: state.error,\n loadMore,\n };\n}\n\n/**\n * Joins a presence room with `presence` and returns the other peers. Leaves\n * the room on unmount; publishes again whenever `presence` changes.\n */\nexport function usePresence(\n db: { $presence(room: string): PresenceRoom },\n room: string,\n presence?: unknown\n): PresencePeer[] {\n const [peers, setPeers] = useState<PresencePeer[]>([]);\n const handle = useMemo(() => db.$presence(room), [db, room]);\n const serialised = presence === undefined ? undefined : stableStringify(presence);\n\n useEffect(() => handle.subscribe(setPeers), [handle]);\n useEffect(() => {\n if (presence !== undefined) handle.set(presence);\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [handle, serialised]);\n useEffect(() => () => handle.leave(), [handle]);\n\n return peers;\n}\n\n\n/**\n * Listens on a broadcast topic while mounted and returns its `send`. The\n * latest `onMessage` is always the one called, so it need not be memoised.\n *\n * @experimental May change in a minor release.\n */\nexport function useBroadcast<T = unknown>(\n db: { $broadcast<M>(topic: string): BroadcastTopic<M> },\n topic: string,\n onMessage?: (message: T, from: TopicSender) => void\n): (message: T) => boolean | \"queued\" {\n const handle = useMemo(() => db.$broadcast<T>(topic), [db, topic]);\n const listener = useRef(onMessage);\n listener.current = onMessage;\n\n useEffect(\n () => handle.subscribe((message, from) => listener.current?.(message, from)),\n [handle]\n );\n return useCallback((message: T) => handle.send(message), [handle]);\n}\n"],"mappings":";;;;;;AAUA,SAAS,aAAa,WAAW,SAAS,QAAQ,UAAU,4BAA4B;AAgBjF,SAAS,QAAW,OAAgD;AACzE,QAAM,WAAW,OAAO,KAAK;AAC7B,WAAS,UAAU;AAEnB,QAAM,YAAY;AAAA,IAChB,MAAM,CAAC,aAA0B,SAAS,QAAqB,UAAU,QAAQ;AAAA;AAAA,IAEjF,CAAC,MAAM,GAAG;AAAA,EACZ;AACA,QAAM,WAAW,MAAO,SAAS,QAAqB,SAAS;AAC/D,SAAO,qBAAqB,WAAW,UAAU,QAAQ;AAC3D;AAYO,SAAS,YAAe,OAAkC;AAC/D,QAAM,QAAQ,QAAQ,KAAK;AAC3B,QAAM,WAAW,OAAO,KAAK;AAC7B,WAAS,UAAU;AACnB,Q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