@oxy.so/services 14.19.2 → 14.20.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.
Files changed (68) hide show
  1. package/lib/commonjs/index.js.map +1 -1
  2. package/lib/commonjs/ui/client.js.map +1 -1
  3. package/lib/commonjs/ui/hooks/accountQueryPersistence.js +13 -1
  4. package/lib/commonjs/ui/hooks/accountQueryPersistence.js.map +1 -1
  5. package/lib/commonjs/ui/hooks/persistedQueryCache.js +120 -8
  6. package/lib/commonjs/ui/hooks/persistedQueryCache.js.map +1 -1
  7. package/lib/commonjs/ui/hooks/queryClient.js +6 -1
  8. package/lib/commonjs/ui/hooks/queryClient.js.map +1 -1
  9. package/lib/commonjs/ui/index.js.map +1 -1
  10. package/lib/commonjs/ui/utils/persistedDataVersion.js +16 -0
  11. package/lib/commonjs/ui/utils/persistedDataVersion.js.map +1 -0
  12. package/lib/module/index.js.map +1 -1
  13. package/lib/module/ui/client.js +3 -0
  14. package/lib/module/ui/client.js.map +1 -1
  15. package/lib/module/ui/hooks/accountQueryPersistence.js +14 -2
  16. package/lib/module/ui/hooks/accountQueryPersistence.js.map +1 -1
  17. package/lib/module/ui/hooks/persistedQueryCache.js +117 -8
  18. package/lib/module/ui/hooks/persistedQueryCache.js.map +1 -1
  19. package/lib/module/ui/hooks/queryClient.js +7 -2
  20. package/lib/module/ui/hooks/queryClient.js.map +1 -1
  21. package/lib/module/ui/index.js.map +1 -1
  22. package/lib/module/ui/utils/persistedDataVersion.js +12 -0
  23. package/lib/module/ui/utils/persistedDataVersion.js.map +1 -0
  24. package/lib/typescript/commonjs/index.d.ts +1 -0
  25. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  26. package/lib/typescript/commonjs/ui/client.d.ts +2 -0
  27. package/lib/typescript/commonjs/ui/client.d.ts.map +1 -1
  28. package/lib/typescript/commonjs/ui/hooks/accountQueryPersistence.d.ts +17 -0
  29. package/lib/typescript/commonjs/ui/hooks/accountQueryPersistence.d.ts.map +1 -1
  30. package/lib/typescript/commonjs/ui/hooks/persistedQueryCache.d.ts +51 -3
  31. package/lib/typescript/commonjs/ui/hooks/persistedQueryCache.d.ts.map +1 -1
  32. package/lib/typescript/commonjs/ui/hooks/queryClient.d.ts +2 -1
  33. package/lib/typescript/commonjs/ui/hooks/queryClient.d.ts.map +1 -1
  34. package/lib/typescript/commonjs/ui/icons/Ionicons.d.ts +1 -1
  35. package/lib/typescript/commonjs/ui/icons/MaterialCommunityIcons.d.ts +1 -1
  36. package/lib/typescript/commonjs/ui/index.d.ts +1 -0
  37. package/lib/typescript/commonjs/ui/index.d.ts.map +1 -1
  38. package/lib/typescript/commonjs/ui/types/navigation.d.ts +1 -1
  39. package/lib/typescript/commonjs/ui/types/navigation.d.ts.map +1 -1
  40. package/lib/typescript/commonjs/ui/utils/persistedDataVersion.d.ts +10 -0
  41. package/lib/typescript/commonjs/ui/utils/persistedDataVersion.d.ts.map +1 -0
  42. package/lib/typescript/module/index.d.ts +1 -0
  43. package/lib/typescript/module/index.d.ts.map +1 -1
  44. package/lib/typescript/module/ui/client.d.ts +2 -0
  45. package/lib/typescript/module/ui/client.d.ts.map +1 -1
  46. package/lib/typescript/module/ui/hooks/accountQueryPersistence.d.ts +17 -0
  47. package/lib/typescript/module/ui/hooks/accountQueryPersistence.d.ts.map +1 -1
  48. package/lib/typescript/module/ui/hooks/persistedQueryCache.d.ts +51 -3
  49. package/lib/typescript/module/ui/hooks/persistedQueryCache.d.ts.map +1 -1
  50. package/lib/typescript/module/ui/hooks/queryClient.d.ts +2 -1
  51. package/lib/typescript/module/ui/hooks/queryClient.d.ts.map +1 -1
  52. package/lib/typescript/module/ui/icons/Ionicons.d.ts +1 -1
  53. package/lib/typescript/module/ui/icons/MaterialCommunityIcons.d.ts +1 -1
  54. package/lib/typescript/module/ui/index.d.ts +1 -0
  55. package/lib/typescript/module/ui/index.d.ts.map +1 -1
  56. package/lib/typescript/module/ui/types/navigation.d.ts +1 -1
  57. package/lib/typescript/module/ui/types/navigation.d.ts.map +1 -1
  58. package/lib/typescript/module/ui/utils/persistedDataVersion.d.ts +10 -0
  59. package/lib/typescript/module/ui/utils/persistedDataVersion.d.ts.map +1 -0
  60. package/package.json +3 -3
  61. package/src/index.ts +1 -0
  62. package/src/ui/client.ts +3 -0
  63. package/src/ui/hooks/accountQueryPersistence.ts +37 -0
  64. package/src/ui/hooks/persistedQueryCache.ts +150 -13
  65. package/src/ui/hooks/queryClient.ts +8 -1
  66. package/src/ui/index.ts +1 -0
  67. package/src/ui/types/navigation.ts +1 -1
  68. package/src/ui/utils/persistedDataVersion.ts +9 -0
@@ -6,8 +6,10 @@
6
6
  * A blob is `{ buster, timestamp, clientState }`, TanStack's persisted-client
7
7
  * shape. Restoring it treats its two halves differently:
8
8
  *
9
- * - Queries are served before any refetch, so a query written by another
10
- * build, in whatever shape that build used, is dropped (`getOxyBuildId`).
9
+ * - Queries are served before any refetch, so a query written under another
10
+ * buster, in whatever shape it had then, is dropped. The buster is the build
11
+ * (`getOxyBuildId`) unless the app declares the version of the data it
12
+ * persists (`AccountQueriesConfig.dataVersion`, see `persistedBuster`).
11
13
  * - Paused mutations are the user's queued actions (a star, a send made
12
14
  * offline). A deploy must not lose them, so they are restored whatever build
13
15
  * wrote them; the current build's `setMutationDefaults` replays them.
@@ -22,6 +24,7 @@ import {
22
24
  type QueryClient,
23
25
  } from '@tanstack/react-query';
24
26
  import { getOxyBuildId } from '../utils/buildId';
27
+ import { SERVICES_PERSISTED_DATA_VERSION } from '../utils/persistedDataVersion';
25
28
 
26
29
  /** The SDK's own account-agnostic query roots, persisted by `attachQueryPersistence`. */
27
30
  export const PERSISTED_QUERY_PREFIXES: ReadonlyArray<string> = [
@@ -43,10 +46,119 @@ export const SDK_MEMORY_ONLY_ROOTS: ReadonlyArray<string> = [
43
46
  'justifiedPhotoDimensions',
44
47
  ];
45
48
 
49
+ /**
50
+ * A cap on what one root writes to disk: its `maxQueries` most recently updated queries, each
51
+ * passed through `trim` (for example, a conversation's last messages). Queries past the cap that
52
+ * no screen is watching leave memory too, so the root stays bounded in both.
53
+ */
54
+ export interface PersistedRootBound {
55
+ maxQueries: number;
56
+ /** Shrink one query's data before it is written. Must return data of the same shape. */
57
+ trim?: (data: unknown) => unknown;
58
+ /**
59
+ * Give a restored query's data its type back before anything reads it: the blob is JSON, so a
60
+ * `Date` comes back as a string. Runs once per query, on restore.
61
+ */
62
+ revive?: (data: unknown) => unknown;
63
+ }
64
+
46
65
  export interface PersistFilters {
47
66
  /** The queries this cache keeps, whatever their state; only successful ones are written. */
48
67
  persistsQuery: (query: Query) => boolean;
49
68
  shouldDehydrateMutation: (mutation: Mutation) => boolean;
69
+ /** What a blob is stamped with, and must match to be restored. Defaults to the build. */
70
+ buster?: string;
71
+ /** Per-root caps, by `queryKey[0]`. */
72
+ bounds?: ReadonlyMap<string, PersistedRootBound>;
73
+ }
74
+
75
+ /**
76
+ * The stamp a persisted blob must carry to be restored.
77
+ *
78
+ * Without a declared version it is the build: every deploy starts from the network, which is
79
+ * safe for an app that never says when its persisted data changes shape. With one, a blob
80
+ * survives deploys and is dropped only when that version, or this SDK's own (it owns the shape
81
+ * of the shared roots and of how blobs are written), changes.
82
+ */
83
+ export function persistedBuster(dataVersion?: string): string {
84
+ return dataVersion ? `data:${SERVICES_PERSISTED_DATA_VERSION}:${dataVersion}` : getOxyBuildId();
85
+ }
86
+
87
+ /** Within each bounded root, its queries ordered newest first by when their data arrived. */
88
+ function rankedByRoot<T extends { queryKey: readonly unknown[]; updatedAt: number }>(
89
+ items: readonly T[],
90
+ bounds: ReadonlyMap<string, PersistedRootBound>,
91
+ ): Map<string, T[]> {
92
+ const byRoot = new Map<string, T[]>();
93
+ for (const item of items) {
94
+ const root = typeof item.queryKey[0] === 'string' ? item.queryKey[0] : null;
95
+ if (root === null || !bounds.has(root)) continue;
96
+ const list = byRoot.get(root) ?? [];
97
+ list.push(item);
98
+ byRoot.set(root, list);
99
+ }
100
+ for (const list of byRoot.values()) list.sort((a, b) => b.updatedAt - a.updatedAt);
101
+ return byRoot;
102
+ }
103
+
104
+ /** Apply `bounds` to a dehydrated state: drop past the cap, trim what stays. */
105
+ function boundState(
106
+ state: DehydratedState,
107
+ bounds: ReadonlyMap<string, PersistedRootBound> | undefined,
108
+ ): DehydratedState {
109
+ if (!bounds || bounds.size === 0) return state;
110
+ const ranked = rankedByRoot(
111
+ state.queries.map((query) => ({ ...query, updatedAt: query.state.dataUpdatedAt })),
112
+ bounds,
113
+ );
114
+ const kept = new Set<string>();
115
+ for (const [root, list] of ranked) {
116
+ for (const query of list.slice(0, bounds.get(root)!.maxQueries)) kept.add(query.queryHash);
117
+ }
118
+ return {
119
+ ...state,
120
+ queries: state.queries.flatMap((query) => {
121
+ const root = typeof query.queryKey[0] === 'string' ? query.queryKey[0] : null;
122
+ const bound = root === null ? undefined : bounds.get(root);
123
+ if (!bound) return [query];
124
+ if (!kept.has(query.queryHash)) return [];
125
+ return [
126
+ bound.trim
127
+ ? { ...query, state: { ...query.state, data: bound.trim(query.state.data) } }
128
+ : query,
129
+ ];
130
+ }),
131
+ };
132
+ }
133
+
134
+ function reviveQuery(
135
+ query: DehydratedState['queries'][number],
136
+ bounds: ReadonlyMap<string, PersistedRootBound> | undefined,
137
+ ): DehydratedState['queries'][number] {
138
+ const root = typeof query.queryKey[0] === 'string' ? query.queryKey[0] : null;
139
+ const revive = root === null ? undefined : bounds?.get(root)?.revive;
140
+ if (!revive || query.state.data === undefined) return query;
141
+ return { ...query, state: { ...query.state, data: revive(query.state.data) } };
142
+ }
143
+
144
+ /** Remove, from memory, a bounded root's queries past its cap that nothing is watching. */
145
+ export function evictBeyondBounds(
146
+ queryClient: QueryClient,
147
+ bounds: ReadonlyMap<string, PersistedRootBound> | undefined,
148
+ ): void {
149
+ if (!bounds || bounds.size === 0) return;
150
+ const ranked = rankedByRoot(
151
+ queryClient
152
+ .getQueryCache()
153
+ .getAll()
154
+ .map((query) => ({ query, queryKey: query.queryKey, updatedAt: query.state.dataUpdatedAt })),
155
+ bounds,
156
+ );
157
+ for (const [root, list] of ranked) {
158
+ for (const { query } of list.slice(bounds.get(root)!.maxQueries)) {
159
+ if (query.getObserversCount() === 0) queryClient.getQueryCache().remove(query);
160
+ }
161
+ }
50
162
  }
51
163
 
52
164
  interface PersistedBlob {
@@ -55,21 +167,42 @@ interface PersistedBlob {
55
167
  clientState: DehydratedState;
56
168
  }
57
169
 
170
+ /**
171
+ * Keep a persisted root's queries in memory for as long as they live on disk.
172
+ *
173
+ * A snapshot is written from memory, so a query the client garbage-collects is gone from the next
174
+ * snapshot too. With the default `gcTime` (30 minutes) under a cache kept for days, "persisted"
175
+ * meant "seen in the last half hour": a skill, a thread or a list not on screen for that long
176
+ * dropped out of memory and of storage, and the next visit waited on the network behind a
177
+ * skeleton. `gcTime: Infinity` (TanStack's advice for a persisted cache, and no timer: a gcTime
178
+ * over 2^31 ms would overflow one) keeps them until the cache's own rules remove them: expiry on
179
+ * restore, an account switch, sign-out. Set before restoring, so hydrated queries get it too.
180
+ */
181
+ export function keepPersistedRootsInMemory(
182
+ queryClient: QueryClient,
183
+ roots: ReadonlyArray<string>,
184
+ ): void {
185
+ for (const root of roots) queryClient.setQueryDefaults([root], { gcTime: Infinity });
186
+ }
187
+
58
188
  export function rootOf(query: Pick<Query, 'queryKey'>): string | null {
59
189
  const head = query.queryKey[0];
60
190
  return typeof head === 'string' ? head : null;
61
191
  }
62
192
 
63
- /** Serialize what the filters select, stamped with this build. */
193
+ /** Serialize what the filters select, within their bounds, stamped with their buster. */
64
194
  export function snapshot(queryClient: QueryClient, filters: PersistFilters): string {
65
195
  const blob: PersistedBlob = {
66
- buster: getOxyBuildId(),
196
+ buster: filters.buster ?? getOxyBuildId(),
67
197
  timestamp: Date.now(),
68
- clientState: dehydrate(queryClient, {
69
- shouldDehydrateQuery: (query) =>
70
- query.state.status === 'success' && filters.persistsQuery(query),
71
- shouldDehydrateMutation: filters.shouldDehydrateMutation,
72
- }),
198
+ clientState: boundState(
199
+ dehydrate(queryClient, {
200
+ shouldDehydrateQuery: (query) =>
201
+ query.state.status === 'success' && filters.persistsQuery(query),
202
+ shouldDehydrateMutation: filters.shouldDehydrateMutation,
203
+ }),
204
+ filters.bounds,
205
+ ),
73
206
  };
74
207
  return JSON.stringify(blob);
75
208
  }
@@ -84,6 +217,10 @@ export function hydrateSnapshot(
84
217
  raw: string | null,
85
218
  maxAge: number,
86
219
  options?: {
220
+ /** The stamp the blob must carry for its queries to be restored. Defaults to the build. */
221
+ buster?: string;
222
+ /** Per-root `revive`, applied to each restored query of that root. */
223
+ bounds?: ReadonlyMap<string, PersistedRootBound>;
87
224
  shouldRestoreQuery?: (query: DehydratedState['queries'][number]) => boolean;
88
225
  shouldRestoreMutation?: (mutation: DehydratedState['mutations'][number]) => boolean;
89
226
  restoreMutations?: boolean;
@@ -103,12 +240,12 @@ export function hydrateSnapshot(
103
240
  ) {
104
241
  return false;
105
242
  }
106
- const sameBuild = blob.buster === getOxyBuildId();
243
+ const sameBuild = blob.buster === (options?.buster ?? getOxyBuildId());
107
244
  hydrate(queryClient, {
108
245
  queries: sameBuild
109
- ? (blob.clientState.queries ?? []).filter(
110
- (query) => options?.shouldRestoreQuery?.(query) ?? true,
111
- )
246
+ ? (blob.clientState.queries ?? [])
247
+ .filter((query) => options?.shouldRestoreQuery?.(query) ?? true)
248
+ .map((query) => reviveQuery(query, options?.bounds))
112
249
  : [],
113
250
  mutations:
114
251
  options?.restoreMutations === false
@@ -29,6 +29,8 @@ import { accountQueryOwnership, type AccountQueriesConfig } from './accountQuery
29
29
  import {
30
30
  PERSISTED_QUERY_PREFIXES,
31
31
  hydrateSnapshot,
32
+ keepPersistedRootsInMemory,
33
+ persistedBuster,
32
34
  subscribeSnapshots,
33
35
  type PersistFilters,
34
36
  } from './persistedQueryCache';
@@ -146,7 +148,8 @@ export interface AttachPersistenceResult {
146
148
  /**
147
149
  * Restore, then keep writing, the account-agnostic cache.
148
150
  *
149
- * Queries written by another build are dropped; queued mutations are kept
151
+ * Queries written under another buster are dropped (the build, or the app's declared
152
+ * `dataVersion`); queued mutations are kept
150
153
  * (`persistedQueryCache.ts`). Whatever belongs to the account (`accountQueries`,
151
154
  * or a query marked `meta: { persist: 'account' }`) is left to
152
155
  * `createAccountQueryPersistence`, even under one of the SDK's prefixes, so
@@ -169,7 +172,10 @@ export const attachQueryPersistence = (
169
172
  }
170
173
 
171
174
  const account = accountQueryOwnership(accountQueries);
175
+ keepPersistedRootsInMemory(queryClient, PERSISTED_QUERY_PREFIXES);
176
+ const buster = persistedBuster(accountQueries?.dataVersion);
172
177
  const filters: PersistFilters = {
178
+ buster,
173
179
  persistsQuery: (query) => isSharedQuery(query) && !account.ownsQuery(query),
174
180
  // Every other mutation, whatever its status: paused ones are exactly the
175
181
  // ones that must survive a restart to replay when online.
@@ -183,6 +189,7 @@ export const attachQueryPersistence = (
183
189
  if (stopped) return;
184
190
  if (
185
191
  !hydrateSnapshot(queryClient, raw, QUERY_CACHE_MAX_AGE, {
192
+ buster,
186
193
  shouldRestoreQuery: (query) => !account.ownsQuery(query),
187
194
  restoreMutations: accountQueries?.clearUnregisteredMutations !== true,
188
195
  shouldRestoreMutation: (mutation) => !account.ownsMutationKey(mutation.mutationKey),
package/src/ui/index.ts CHANGED
@@ -39,6 +39,7 @@ export { RequireOxyAuth } from './components/RequireOxyAuth';
39
39
  export type { RequireOxyAuthProps, RequireOxyAuthPrompt } from './components/RequireOxyAuth';
40
40
  // `OxyProvider`'s `accountQueries`: the app's per-account offline cache.
41
41
  export type { AccountQueriesConfig, AccountQueryMeta } from './hooks/accountQueryPersistence';
42
+ export type { PersistedRootBound } from './hooks/persistedQueryCache';
42
43
  export { default as FollowButton } from './components/FollowButton';
43
44
  export { default as PeableButton } from './components/PeableButton';
44
45
  export type { PeableButtonProps } from './components/PeableButton';
@@ -71,7 +71,7 @@ export interface OxyProviderProps {
71
71
  onAskAlia?: () => void | Promise<void>;
72
72
  /** App-specific documentation; defaults to https://oxy.so/developers/docs/. */
73
73
  documentationUrl?: string;
74
- /** Official AliaAvatar supplied by hosts that integrate Alia. */
74
+ /** Official `GemieAvatar` (Alia) supplied by hosts that integrate Alia. */
75
75
  aliaAvatar?: ReactNode;
76
76
  /** Optional animated Oxy One artwork, e.g. OxyOneVideo from /ui/one-video. */
77
77
  oxyOneArtwork?: ReactNode;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * This package's version, as part of every declared-version buster (`persistedBuster`).
3
+ *
4
+ * The SDK owns the shape of the shared roots (`accounts`, `users`, ...) and of the blob itself, so
5
+ * an app that declares its own data version still starts from the network after an SDK upgrade.
6
+ * `__tests__/hooks/persistedDataVersion.test.ts` keeps it equal to `package.json`'s `version`, so a
7
+ * release cannot forget it.
8
+ */
9
+ export const SERVICES_PERSISTED_DATA_VERSION = '14.20.0';