@cf-sync/client 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,49 @@
1
+ import { o as SyncStatus, r as PresenceApi, t as SyncClient } from "./client-tvsT_ixa.js";
2
+ import { PresencePeer } from "@cf-sync/protocol";
3
+ //#region src/react.d.ts
4
+ /**
5
+ * The client's sync status as React state — re-renders on every transition:
6
+ *
7
+ * ```tsx
8
+ * const status = useSyncStatus(syncClient)
9
+ * return <span>{status === 'synced' ? 'saved' : 'syncing…'}</span>
10
+ * ```
11
+ *
12
+ * Server-rendered output reads the same snapshot, so SSR/hydration match
13
+ * (construct the client with `autoStart: false` on the server).
14
+ */
15
+ declare function useSyncStatus(client: SyncClient<any, any>): SyncStatus;
16
+ /**
17
+ * Whether locally cached data has been restored and collections are ready to
18
+ * render — the gate an offline-first UI opens its first paint on:
19
+ *
20
+ * ```tsx
21
+ * const status = useSyncStatus(syncClient)
22
+ * const hydrated = useHydrated(syncClient)
23
+ * if (status !== 'synced' && !hydrated) return <Spinner />
24
+ * return <Workspace />
25
+ * ```
26
+ *
27
+ * False until hydration settles, and false forever when there was nothing to
28
+ * restore (no store, an empty or discarded cache) — see `client.hydrated`.
29
+ * The server pre-render sees the same `false` a fresh client does, so
30
+ * SSR/hydration match.
31
+ */
32
+ declare function useHydrated(client: SyncClient<any, any>): boolean;
33
+ /**
34
+ * Peers' presence as React state, typed by the app's presence schema and
35
+ * excluding self — render your own state from `presence.self`, not here:
36
+ *
37
+ * ```tsx
38
+ * const peers = usePresence(syncClient)
39
+ * return <>{peers.map((p) => <Avatar key={p.clientId} name={p.state.name} />)}</>
40
+ * ```
41
+ *
42
+ * The snapshot is stable between changes, and the server pre-render sees the
43
+ * same empty peers a fresh client does, so SSR/hydration match.
44
+ */
45
+ declare function usePresence<TOut>(client: {
46
+ presence: PresenceApi<any, TOut>;
47
+ }): ReadonlyArray<PresencePeer<TOut>>;
48
+ //#endregion
49
+ export { useHydrated, usePresence, useSyncStatus };
package/dist/react.js ADDED
@@ -0,0 +1,53 @@
1
+ import { useSyncExternalStore } from "react";
2
+ //#region src/react.ts
3
+ /**
4
+ * The client's sync status as React state — re-renders on every transition:
5
+ *
6
+ * ```tsx
7
+ * const status = useSyncStatus(syncClient)
8
+ * return <span>{status === 'synced' ? 'saved' : 'syncing…'}</span>
9
+ * ```
10
+ *
11
+ * Server-rendered output reads the same snapshot, so SSR/hydration match
12
+ * (construct the client with `autoStart: false` on the server).
13
+ */
14
+ function useSyncStatus(client) {
15
+ return useSyncExternalStore(client.subscribeStatus, () => client.status, () => client.status);
16
+ }
17
+ /**
18
+ * Whether locally cached data has been restored and collections are ready to
19
+ * render — the gate an offline-first UI opens its first paint on:
20
+ *
21
+ * ```tsx
22
+ * const status = useSyncStatus(syncClient)
23
+ * const hydrated = useHydrated(syncClient)
24
+ * if (status !== 'synced' && !hydrated) return <Spinner />
25
+ * return <Workspace />
26
+ * ```
27
+ *
28
+ * False until hydration settles, and false forever when there was nothing to
29
+ * restore (no store, an empty or discarded cache) — see `client.hydrated`.
30
+ * The server pre-render sees the same `false` a fresh client does, so
31
+ * SSR/hydration match.
32
+ */
33
+ function useHydrated(client) {
34
+ return useSyncExternalStore(client.subscribeHydrated, () => client.hydrated, () => false);
35
+ }
36
+ /**
37
+ * Peers' presence as React state, typed by the app's presence schema and
38
+ * excluding self — render your own state from `presence.self`, not here:
39
+ *
40
+ * ```tsx
41
+ * const peers = usePresence(syncClient)
42
+ * return <>{peers.map((p) => <Avatar key={p.clientId} name={p.state.name} />)}</>
43
+ * ```
44
+ *
45
+ * The snapshot is stable between changes, and the server pre-render sees the
46
+ * same empty peers a fresh client does, so SSR/hydration match.
47
+ */
48
+ function usePresence(client) {
49
+ const presence = client.presence;
50
+ return useSyncExternalStore(presence.subscribe, () => presence.peers, () => presence.peers);
51
+ }
52
+ //#endregion
53
+ export { useHydrated, usePresence, useSyncStatus };
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@cf-sync/client",
3
+ "version": "0.1.0",
4
+ "description": "Browser client for cf-sync: SyncClient (optimistic mutations, offline outbox, IndexedDB mirror, presence) and a TanStack DB collection adapter, with React hooks",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/InfinityBowman/cf-sync.git",
9
+ "directory": "packages/client"
10
+ },
11
+ "homepage": "https://github.com/InfinityBowman/cf-sync#readme",
12
+ "keywords": [
13
+ "sync",
14
+ "sync-engine",
15
+ "local-first",
16
+ "offline-first",
17
+ "optimistic-updates",
18
+ "tanstack-db",
19
+ "react",
20
+ "indexeddb",
21
+ "presence"
22
+ ],
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./react": {
30
+ "types": "./dist/react.d.ts",
31
+ "default": "./dist/react.js"
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "dependencies": {
42
+ "@cf-sync/protocol": "^0.1.0"
43
+ },
44
+ "peerDependencies": {
45
+ "@tanstack/db": "^0.6.16",
46
+ "@tanstack/react-db": "^0.1.94",
47
+ "react": ">=18"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "@tanstack/react-db": {
51
+ "optional": true
52
+ },
53
+ "react": {
54
+ "optional": true
55
+ }
56
+ },
57
+ "devDependencies": {
58
+ "@tanstack/db": "^0.6.16",
59
+ "@types/react": "^19.0.0",
60
+ "@types/react-dom": "^19.2.3",
61
+ "fake-indexeddb": "^6.2.5",
62
+ "react": "^19.0.0",
63
+ "react-dom": "^19.0.0",
64
+ "typescript": "^5.6.0",
65
+ "vite": "^8.1.5",
66
+ "vitest": "^4.1.10",
67
+ "yjs": "^13.6.31",
68
+ "zod": "^4.0.0",
69
+ "@cf-sync/yjs": "0.1.0"
70
+ },
71
+ "scripts": {
72
+ "build": "tsdown",
73
+ "test": "vitest run",
74
+ "typecheck": "tsc --noEmit"
75
+ }
76
+ }