@pylonsync/react 0.3.303 → 0.3.304

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/dist/db.d.ts CHANGED
@@ -19,6 +19,25 @@ import { type UploadedFile } from "./index";
19
19
  export declare function init(config?: Partial<SyncEngineConfig> & {
20
20
  baseUrl?: string;
21
21
  }): void;
22
+ /**
23
+ * Switch the active session token and re-sync the local replica to the new
24
+ * identity. Call this on EVERY auth transition — login, signup, AND logout
25
+ * (pass `null`).
26
+ *
27
+ * It writes the token where both the request helpers and the sync engine read
28
+ * it, then wipes the local replica — dropping the previous identity's pending
29
+ * offline writes so they can't replay as the new user — and re-snapshots under
30
+ * the new identity.
31
+ *
32
+ * Why this exists: changing the stored token alone is NOT enough. The engine is
33
+ * already running with the previous identity's replica + cursor; without an
34
+ * explicit reset it keeps serving that (or, after a re-init, an empty replica on
35
+ * a stale cursor) — so the rows the new user should see never load and freshly
36
+ * written rows appear to "vanish" on the next render/refresh. This is the
37
+ * supported, can't-get-it-wrong way to flip identity; hand-rolling token writes
38
+ * without it is the #1 cause of "my data disappears after login" in Pylon apps.
39
+ */
40
+ export declare function setSessionToken(token: string | null): Promise<void>;
22
41
  /** Module-internal accessor for the global sync engine. Exported so
23
42
  * hooks living outside this file (e.g. `useRoom`) can share the same
24
43
  * engine instance and benefit from the same lazy-start / lazy-init
package/dist/index.d.ts CHANGED
@@ -20,7 +20,7 @@ export { useSession } from "./useSession";
20
20
  export type { UseSessionReturn, ResolvedSession } from "./useSession";
21
21
  export { useSyncStatus } from "./useSyncStatus";
22
22
  export type { SyncConnectionStatus } from "./useSyncStatus";
23
- export { db, init, getSync } from "./db";
23
+ export { db, init, getSync, setSessionToken } from "./db";
24
24
  export { createTypedDb } from "./typed";
25
25
  export type { TypedDb, AgentDBSchema } from "./typed";
26
26
  export { SyncEngine, createSyncEngine, getServerData, LocalStore, MutationQueue, } from "@pylonsync/sync";
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.3.303",
6
+ "version": "0.3.304",
7
7
  "type": "module",
8
8
  "main": "./src/index.ts",
9
9
  "types": "./dist/index.d.ts",
@@ -13,8 +13,8 @@
13
13
  "prepack": "bun run build"
14
14
  },
15
15
  "dependencies": {
16
- "@pylonsync/sdk": "0.3.303",
17
- "@pylonsync/sync": "0.3.303"
16
+ "@pylonsync/sdk": "0.3.304",
17
+ "@pylonsync/sync": "0.3.304"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "react": ">=19.0.0"
package/src/db.ts CHANGED
@@ -24,6 +24,8 @@ import {
24
24
  import {
25
25
  callFn,
26
26
  configureClient,
27
+ getReactStorage,
28
+ storageKey,
27
29
  streamFn,
28
30
  uploadFile,
29
31
  uploadFileMultipart,
@@ -75,6 +77,40 @@ export function init(config?: Partial<SyncEngineConfig> & { baseUrl?: string })
75
77
  });
76
78
  }
77
79
 
80
+ /**
81
+ * Switch the active session token and re-sync the local replica to the new
82
+ * identity. Call this on EVERY auth transition — login, signup, AND logout
83
+ * (pass `null`).
84
+ *
85
+ * It writes the token where both the request helpers and the sync engine read
86
+ * it, then wipes the local replica — dropping the previous identity's pending
87
+ * offline writes so they can't replay as the new user — and re-snapshots under
88
+ * the new identity.
89
+ *
90
+ * Why this exists: changing the stored token alone is NOT enough. The engine is
91
+ * already running with the previous identity's replica + cursor; without an
92
+ * explicit reset it keeps serving that (or, after a re-init, an empty replica on
93
+ * a stale cursor) — so the rows the new user should see never load and freshly
94
+ * written rows appear to "vanish" on the next render/refresh. This is the
95
+ * supported, can't-get-it-wrong way to flip identity; hand-rolling token writes
96
+ * without it is the #1 cause of "my data disappears after login" in Pylon apps.
97
+ */
98
+ export async function setSessionToken(token: string | null): Promise<void> {
99
+ const key = storageKey("token");
100
+ const store = getReactStorage();
101
+ if (token) store.set(key, token);
102
+ else store.remove(key);
103
+ // resetReplica wipes rows + cursor (wipeMutations drops the outgoing
104
+ // identity's queued writes) so the previous identity's data is gone
105
+ // immediately. The re-snapshot pull runs in the BACKGROUND (not awaited) — for
106
+ // a large replica it can take a moment, and blocking login on a full snapshot
107
+ // is bad UX; live `useQuery`/`useSearch` hooks show loading and populate as the
108
+ // snapshot lands. Awaiting resetReplica (fast) is enough for callers.
109
+ const engine = getSync();
110
+ await engine.resetReplica({ wipeMutations: true });
111
+ void engine.pull();
112
+ }
113
+
78
114
  /** Module-internal accessor for the global sync engine. Exported so
79
115
  * hooks living outside this file (e.g. `useRoom`) can share the same
80
116
  * engine instance and benefit from the same lazy-start / lazy-init
package/src/index.ts CHANGED
@@ -118,7 +118,7 @@ export { useSyncStatus } from "./useSyncStatus";
118
118
  export type { SyncConnectionStatus } from "./useSyncStatus";
119
119
 
120
120
  // One-liner API
121
- export { db, init, getSync } from "./db";
121
+ export { db, init, getSync, setSessionToken } from "./db";
122
122
 
123
123
  // Typed client (consumes generated AppSchema)
124
124
  export { createTypedDb } from "./typed";