@proveanything/smartlinks 2.2.0 → 2.2.3
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/dataChanged.d.ts +74 -0
- package/dist/dataChanged.js +162 -0
- package/dist/docs/API_SUMMARY.md +3 -3
- package/dist/docs/caching.md +32 -3
- package/dist/docs/data-changed.md +91 -0
- package/dist/docs/i18n.md +41 -287
- package/dist/docs/iframe-responder.md +11 -0
- package/dist/docs/overview.md +1 -0
- package/dist/docs/realtime.md +6 -0
- package/dist/docs/standard-query.md +12 -3
- package/dist/http.d.ts +6 -3
- package/dist/http.js +48 -19
- package/dist/i18n.d.ts +14 -2
- package/dist/i18n.js +23 -6
- package/dist/iframeResponder.d.ts +7 -0
- package/dist/iframeResponder.js +10 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/API_SUMMARY.md +3 -3
- package/docs/caching.md +32 -3
- package/docs/data-changed.md +91 -0
- package/docs/i18n.md +41 -287
- package/docs/iframe-responder.md +11 -0
- package/docs/overview.md +1 -0
- package/docs/realtime.md +6 -0
- package/docs/standard-query.md +12 -3
- package/package.json +1 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** One thing that changed. The same words as a realtime change (`RealtimeChangeEvent`). */
|
|
2
|
+
export interface DataChange {
|
|
3
|
+
/** 'product' | 'proof' | 'asset' | 'record' | 'contact' | 'collection' | 'settings' | 'appConfig' | … */
|
|
4
|
+
type: string;
|
|
5
|
+
id?: string;
|
|
6
|
+
productId?: string;
|
|
7
|
+
proofId?: string;
|
|
8
|
+
appId?: string;
|
|
9
|
+
recordType?: string;
|
|
10
|
+
/** 'created' | 'updated' | 'deleted', when the host knows. */
|
|
11
|
+
status?: string;
|
|
12
|
+
}
|
|
13
|
+
/** What the host sends (and what `postDataChanged` takes). */
|
|
14
|
+
export interface DataChangedMessage {
|
|
15
|
+
collectionId?: string;
|
|
16
|
+
/** What changed. Leave out (or empty) when the host can't say: "something in this collection changed". */
|
|
17
|
+
changes?: DataChange[];
|
|
18
|
+
/** Who made the change, when the host knows: 'assistant' | 'app' | 'person' | … */
|
|
19
|
+
source?: string;
|
|
20
|
+
}
|
|
21
|
+
/** What an app's listener is given. */
|
|
22
|
+
export interface DataChangedEvent {
|
|
23
|
+
collectionId?: string;
|
|
24
|
+
/** What changed. Empty means the host couldn't say: treat everything you show as possibly changed. */
|
|
25
|
+
changes: DataChange[];
|
|
26
|
+
source?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Does this event say (or leave open) that this thing changed? True when a change matches every field
|
|
29
|
+
* given, and always true when the host couldn't say what changed.
|
|
30
|
+
*
|
|
31
|
+
* @example if (e.touches({ type: 'product', id: productId })) refetch()
|
|
32
|
+
*/
|
|
33
|
+
touches(what: Partial<DataChange>): boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare const DATA_CHANGED = "smartlinks:data-changed";
|
|
36
|
+
type Forget = (pathPart?: string) => void;
|
|
37
|
+
/** The event an app's listener gets, from a raw message. Null when the message isn't one of ours. */
|
|
38
|
+
export declare function toDataChangedEvent(data: unknown): DataChangedEvent | null;
|
|
39
|
+
/**
|
|
40
|
+
* Start listening for the host's message (once, and only inside a frame). `initializeApi` calls this
|
|
41
|
+
* with the cache's own forget, so kept reads are dropped even in an app that never calls
|
|
42
|
+
* `onDataChanged`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function watchDataChanged(forgetFn?: Forget): void;
|
|
45
|
+
/**
|
|
46
|
+
* Hear when the host says records changed behind this page's back (the assistant applied a change,
|
|
47
|
+
* another app saved). By the time the listener runs, the SDK has already forgotten the reads it kept
|
|
48
|
+
* for them: read again and show what comes back. Only messages from the parent window are taken.
|
|
49
|
+
* Returns a function that stops listening. Does nothing outside a frame.
|
|
50
|
+
*
|
|
51
|
+
* A form the person has started typing in should not be overwritten: re-fill it only when it is
|
|
52
|
+
* untouched, otherwise say the record was changed elsewhere and offer the new version.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* // React Query: re-read the open product and the list
|
|
56
|
+
* useEffect(() => SL.onDataChanged((e) => {
|
|
57
|
+
* if (e.collectionId && e.collectionId !== collectionId) return
|
|
58
|
+
* if (e.touches({ type: 'product' })) queryClient.invalidateQueries({ queryKey: ['products'] })
|
|
59
|
+
* if (e.touches({ type: 'product', id: productId })) queryClient.invalidateQueries({ queryKey: ['product', collectionId, productId] })
|
|
60
|
+
* }), [collectionId, productId])
|
|
61
|
+
*/
|
|
62
|
+
export declare function onDataChanged(handler: (e: DataChangedEvent) => void): () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Host side: tell an embedded app that records changed. Send it after a change the app did not make
|
|
65
|
+
* itself: an assistant's change set applied or undone, a save in another app, a change heard on the
|
|
66
|
+
* realtime feed. Leave `changes` out when you can't say what changed.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* SL.postDataChanged(iframe.contentWindow, { collectionId, changes: [{ type: 'product', id }], source: 'assistant' })
|
|
70
|
+
*/
|
|
71
|
+
export declare function postDataChanged(target: Window | null | undefined, message?: DataChangedMessage, targetOrigin?: string): void;
|
|
72
|
+
/** Tests only: forget every listener and the cache hand-over, and stop listening. */
|
|
73
|
+
export declare function _resetDataChanged(): void;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// src/dataChanged.ts
|
|
2
|
+
// DATA CHANGED — the host tells an embedded app that records changed behind its back: the assistant
|
|
3
|
+
// applied a change, another app saved, another person edited. One message:
|
|
4
|
+
// host → app { type: 'smartlinks:data-changed', collectionId?, changes?: DataChange[], source? }
|
|
5
|
+
// The SDK in the app's frame forgets the reads it kept for those records, then calls the app's
|
|
6
|
+
// listeners so the open screen can read again. Ids only, never data: the app re-reads through the
|
|
7
|
+
// normal API. See docs/data-changed.md.
|
|
8
|
+
//
|
|
9
|
+
// This file must not import ./http (http starts the listener): the cache is handed in.
|
|
10
|
+
export const DATA_CHANGED = 'smartlinks:data-changed';
|
|
11
|
+
const FIELDS = ['id', 'productId', 'proofId', 'appId', 'recordType', 'status'];
|
|
12
|
+
let forget = null;
|
|
13
|
+
let listening = false;
|
|
14
|
+
const handlers = new Set();
|
|
15
|
+
function inIframe() {
|
|
16
|
+
return typeof window !== 'undefined' && !!window.parent && window.parent !== window;
|
|
17
|
+
}
|
|
18
|
+
function clean(list) {
|
|
19
|
+
if (!Array.isArray(list))
|
|
20
|
+
return [];
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const raw of list) {
|
|
23
|
+
if (!raw || typeof raw !== 'object')
|
|
24
|
+
continue;
|
|
25
|
+
const o = raw;
|
|
26
|
+
if (typeof o.type !== 'string' || !o.type)
|
|
27
|
+
continue;
|
|
28
|
+
const c = { type: o.type };
|
|
29
|
+
for (const k of FIELDS)
|
|
30
|
+
if (typeof o[k] === 'string' && o[k])
|
|
31
|
+
c[k] = o[k];
|
|
32
|
+
out.push(c);
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
/** Does `change` match every field given in `what`? */
|
|
37
|
+
function matches(change, what) {
|
|
38
|
+
if (what.type && change.type !== what.type)
|
|
39
|
+
return false;
|
|
40
|
+
for (const k of FIELDS) {
|
|
41
|
+
if (k === 'status')
|
|
42
|
+
continue;
|
|
43
|
+
// A change that doesn't name the field leaves it open: "a product changed" touches every product.
|
|
44
|
+
if (what[k] != null && change[k] != null && change[k] !== what[k])
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
/** The event an app's listener gets, from a raw message. Null when the message isn't one of ours. */
|
|
50
|
+
export function toDataChangedEvent(data) {
|
|
51
|
+
if (!data || typeof data !== 'object')
|
|
52
|
+
return null;
|
|
53
|
+
const d = data;
|
|
54
|
+
if (d.type !== DATA_CHANGED)
|
|
55
|
+
return null;
|
|
56
|
+
const changes = clean(d.changes);
|
|
57
|
+
return Object.assign(Object.assign(Object.assign(Object.assign({}, (typeof d.collectionId === 'string' && d.collectionId ? { collectionId: d.collectionId } : {})), { changes }), (typeof d.source === 'string' && d.source ? { source: d.source } : {})), { touches: (what) => changes.length === 0 || changes.some((c) => matches(c, what || {})) });
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Forget the reads the SDK kept for what changed. Products and proofs have known addresses; anything
|
|
61
|
+
* else, or "something changed", clears everything kept for the collection (everything, with no collection).
|
|
62
|
+
*/
|
|
63
|
+
function forgetKept(e) {
|
|
64
|
+
if (!forget)
|
|
65
|
+
return;
|
|
66
|
+
const cid = e.collectionId ? `/collection/${encodeURIComponent(e.collectionId)}` : '';
|
|
67
|
+
const known = e.changes.length > 0 && e.changes.every((c) => c.type === 'product' || c.type === 'proof');
|
|
68
|
+
try {
|
|
69
|
+
if (!known) {
|
|
70
|
+
forget(cid || undefined);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
// One product, its lists, and the proofs under it all live below these.
|
|
74
|
+
forget(`${cid}/product`);
|
|
75
|
+
if (e.changes.some((c) => c.type === 'proof'))
|
|
76
|
+
forget(`${cid}/proof`);
|
|
77
|
+
}
|
|
78
|
+
catch (_a) {
|
|
79
|
+
/* a cache that can't be cleared must not stop the app hearing about the change */
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function onMessage(ev) {
|
|
83
|
+
if (ev.source !== window.parent)
|
|
84
|
+
return;
|
|
85
|
+
const e = toDataChangedEvent(ev.data);
|
|
86
|
+
if (!e)
|
|
87
|
+
return;
|
|
88
|
+
forgetKept(e);
|
|
89
|
+
for (const h of [...handlers]) {
|
|
90
|
+
try {
|
|
91
|
+
h(e);
|
|
92
|
+
}
|
|
93
|
+
catch ( /* one listener failing must not stop the others */_a) { /* one listener failing must not stop the others */ }
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Start listening for the host's message (once, and only inside a frame). `initializeApi` calls this
|
|
98
|
+
* with the cache's own forget, so kept reads are dropped even in an app that never calls
|
|
99
|
+
* `onDataChanged`.
|
|
100
|
+
*/
|
|
101
|
+
export function watchDataChanged(forgetFn) {
|
|
102
|
+
if (forgetFn)
|
|
103
|
+
forget = forgetFn;
|
|
104
|
+
if (listening || !inIframe())
|
|
105
|
+
return;
|
|
106
|
+
listening = true;
|
|
107
|
+
window.addEventListener('message', onMessage);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Hear when the host says records changed behind this page's back (the assistant applied a change,
|
|
111
|
+
* another app saved). By the time the listener runs, the SDK has already forgotten the reads it kept
|
|
112
|
+
* for them: read again and show what comes back. Only messages from the parent window are taken.
|
|
113
|
+
* Returns a function that stops listening. Does nothing outside a frame.
|
|
114
|
+
*
|
|
115
|
+
* A form the person has started typing in should not be overwritten: re-fill it only when it is
|
|
116
|
+
* untouched, otherwise say the record was changed elsewhere and offer the new version.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* // React Query: re-read the open product and the list
|
|
120
|
+
* useEffect(() => SL.onDataChanged((e) => {
|
|
121
|
+
* if (e.collectionId && e.collectionId !== collectionId) return
|
|
122
|
+
* if (e.touches({ type: 'product' })) queryClient.invalidateQueries({ queryKey: ['products'] })
|
|
123
|
+
* if (e.touches({ type: 'product', id: productId })) queryClient.invalidateQueries({ queryKey: ['product', collectionId, productId] })
|
|
124
|
+
* }), [collectionId, productId])
|
|
125
|
+
*/
|
|
126
|
+
export function onDataChanged(handler) {
|
|
127
|
+
if (typeof handler !== 'function' || !inIframe())
|
|
128
|
+
return () => { };
|
|
129
|
+
watchDataChanged();
|
|
130
|
+
handlers.add(handler);
|
|
131
|
+
return () => { handlers.delete(handler); };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Host side: tell an embedded app that records changed. Send it after a change the app did not make
|
|
135
|
+
* itself: an assistant's change set applied or undone, a save in another app, a change heard on the
|
|
136
|
+
* realtime feed. Leave `changes` out when you can't say what changed.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* SL.postDataChanged(iframe.contentWindow, { collectionId, changes: [{ type: 'product', id }], source: 'assistant' })
|
|
140
|
+
*/
|
|
141
|
+
export function postDataChanged(target, message = {}, targetOrigin = '*') {
|
|
142
|
+
if (!target)
|
|
143
|
+
return;
|
|
144
|
+
try {
|
|
145
|
+
target.postMessage(Object.assign(Object.assign(Object.assign({ type: DATA_CHANGED }, (message.collectionId ? { collectionId: message.collectionId } : {})), { changes: clean(message.changes) }), (message.source ? { source: message.source } : {})), targetOrigin);
|
|
146
|
+
}
|
|
147
|
+
catch (_a) {
|
|
148
|
+
/* ignore */
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Tests only: forget every listener and the cache hand-over, and stop listening. */
|
|
152
|
+
export function _resetDataChanged() {
|
|
153
|
+
handlers.clear();
|
|
154
|
+
forget = null;
|
|
155
|
+
if (listening && typeof window !== 'undefined') {
|
|
156
|
+
try {
|
|
157
|
+
window.removeEventListener('message', onMessage);
|
|
158
|
+
}
|
|
159
|
+
catch ( /* ignore */_a) { /* ignore */ }
|
|
160
|
+
}
|
|
161
|
+
listening = false;
|
|
162
|
+
}
|
package/dist/docs/API_SUMMARY.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Smartlinks API Summary
|
|
2
2
|
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.3 | Generated: 2026-12-02T13:03:18.253Z
|
|
4
4
|
|
|
5
5
|
This is a concise summary of all available API functions and types.
|
|
6
6
|
|
|
@@ -260,7 +260,7 @@ Returns true if the SDK currently has any auth credential set (bearer token or A
|
|
|
260
260
|
serveStaleOnOffline?: boolean
|
|
261
261
|
clearOnPageLoad?: boolean
|
|
262
262
|
}) → `void`
|
|
263
|
-
Configure the SDK's built-in in-memory GET cache. The cache is transparent — it sits inside the HTTP layer and requires no changes to your existing API calls.
|
|
263
|
+
Configure the SDK's built-in in-memory GET cache. The cache is transparent — it sits inside the HTTP layer and requires no changes to your existing API calls. Public GET requests benefit automatically. Admin GETs (`/admin/...`) are never kept, whatever is set here: see docs/caching.md. Per-resource rules (collections/products → 1 h, proofs → 30 s, etc.) override this value. in-memory only (`'none'`, default). Ignored in Node.js. fallback, from the original fetch time (default: 7 days). `SmartlinksOfflineError` with stale data instead of propagating the network error. caches on page load/refresh. IndexedDB persists for offline. ```ts // Enable IndexedDB persistence for offline support configureSdkCache({ persistence: 'indexeddb' }) // Disable cache entirely in test environments configureSdkCache({ enabled: false }) // Keep caches across page refreshes (not recommended for production) configureSdkCache({ clearOnPageLoad: false }) ```
|
|
264
264
|
|
|
265
265
|
**invalidateCache**(urlPattern?: string, options?: InvalidateCacheOptions) → `void`
|
|
266
266
|
Manually invalidate entries in the SDK's GET cache. Note: the GET cache is **in-memory, per page load** (with an optional L2 IndexedDB layer when persistence is enabled) — it does not persist across reloads unless you opt into persistence, so it rarely needs disabling "for correctness". *contains* this string is removed). With `{ exact: true }`, matches the path precisely. Omit to wipe the entire cache. ```ts invalidateCache() // clear everything invalidateCache('/collection/abc123') // that collection AND everything under it invalidateCache('/collection/abc123', { exact: true }) // ONLY that collection entry invalidateCache('/products/') // all canonical plural product responses ```
|
|
@@ -271,7 +271,7 @@ Manually invalidate entries in the SDK's GET cache. Note: the GET cache is **in-
|
|
|
271
271
|
Upload a FormData payload via proxy with progress events using chunked postMessage. Parent is expected to implement the counterpart protocol.
|
|
272
272
|
|
|
273
273
|
**request**(path: string) → `Promise<T>`
|
|
274
|
-
Internal helper that performs a GET request to `${baseURL}${path}`, injecting headers for apiKey or bearerToken if present. Cache pipeline (when caching is not skipped): L1 hit → return from memory (no I/O) L2 hit → return from IndexedDB, promote to L1 (no network) Miss → fetch from network, store in L1 + L2 Offline → serve stale L2 entry via SmartlinksOfflineError (if persistence enabled) Concurrent identical GETs share one in-flight promise (deduplication). Node-safe: IndexedDB calls are no-ops when IDB is unavailable.
|
|
274
|
+
Internal helper that performs a GET request to `${baseURL}${path}`, injecting headers for apiKey or bearerToken if present. Cache pipeline (when caching is not skipped): L1 hit → return from memory (no I/O) L2 hit → return from IndexedDB, promote to L1 (no network) Miss → fetch from network, store in L1 + L2 Offline → serve stale L2 entry via SmartlinksOfflineError (if persistence enabled) Admin paths (`/admin/...`) are never kept (see isAdminPath): every call goes to the network. Concurrent identical GETs share one in-flight promise (deduplication), admin ones included. Node-safe: IndexedDB calls are no-ops when IDB is unavailable.
|
|
275
275
|
|
|
276
276
|
**post**(path: string,
|
|
277
277
|
body: any,
|
package/dist/docs/caching.md
CHANGED
|
@@ -23,7 +23,7 @@ This ensures that **page refreshes always fetch fresh data from the server**, wh
|
|
|
23
23
|
### How It Works
|
|
24
24
|
|
|
25
25
|
1. **During browsing (same page load)**:
|
|
26
|
-
- API requests are cached in memory and sessionStorage
|
|
26
|
+
- Public API requests are cached in memory and sessionStorage (admin requests never are, see below)
|
|
27
27
|
- Subsequent identical requests serve from cache (fast!)
|
|
28
28
|
- TTL rules determine cache freshness (30s - 1h depending on resource)
|
|
29
29
|
|
|
@@ -99,9 +99,35 @@ configureSdkCache({
|
|
|
99
99
|
- **Only used as offline fallback**, not for normal requests
|
|
100
100
|
- ~50MB+ quota
|
|
101
101
|
|
|
102
|
+
## Admin reads are never kept
|
|
103
|
+
|
|
104
|
+
Since 2.2.2, a GET on an admin path (`/admin/...`) always goes to the network. Its answer is not kept in
|
|
105
|
+
memory and not written to IndexedDB, whatever `configureSdkCache` says.
|
|
106
|
+
|
|
107
|
+
Why: an admin screen is where records change, and not only through your page. Another app in another
|
|
108
|
+
frame, the assistant, or another person may have saved since you last read. Each frame has its own cache
|
|
109
|
+
and a write only forgets reads of its own address, so a kept admin answer showed old data. Worse, code
|
|
110
|
+
that reads a record, changes part of it and saves the whole wrote the old data back over the newer.
|
|
111
|
+
|
|
112
|
+
What this means for an admin app:
|
|
113
|
+
|
|
114
|
+
- `products.get(collectionId, productId, true)`, `proof.get(..., true)`, `appConfiguration.getConfig({ admin: true })`
|
|
115
|
+
and every other admin read return what the server has now.
|
|
116
|
+
- You no longer need `invalidateCache(...)` before a read that feeds a save or fills an edit form. Calls
|
|
117
|
+
already in your code are harmless.
|
|
118
|
+
- Identical admin GETs asked at the same moment still share one network call, so several components
|
|
119
|
+
mounting together do not multiply requests.
|
|
120
|
+
- Your own page state is still yours: a query library's `staleTime`, or a form filled once on load, will
|
|
121
|
+
show what it loaded until you re-read. Re-read before a save that replaces a whole record.
|
|
122
|
+
- To hear that a record changed behind your page's back (the assistant applied a change, another app
|
|
123
|
+
saved), use `onDataChanged`: see `docs/data-changed.md`. The SDK forgets the reads it kept for what
|
|
124
|
+
changed before it tells you.
|
|
125
|
+
|
|
126
|
+
Public reads (`/public/...`) keep the rules below.
|
|
127
|
+
|
|
102
128
|
## TTL Rules
|
|
103
129
|
|
|
104
|
-
Different API resources have different cache lifetimes:
|
|
130
|
+
Different public API resources have different cache lifetimes:
|
|
105
131
|
|
|
106
132
|
| Resource | TTL | Reason |
|
|
107
133
|
|----------|-----|--------|
|
|
@@ -110,7 +136,10 @@ Different API resources have different cache lifetimes:
|
|
|
110
136
|
| `/product/*` | 1 hour | Products are relatively stable |
|
|
111
137
|
| `/variant/*` | 1 hour | Variants rarely change |
|
|
112
138
|
| `/collection/*` | 1 hour | Collections are stable |
|
|
113
|
-
| Everything else |
|
|
139
|
+
| Everything else | 15 seconds | Default safety |
|
|
140
|
+
|
|
141
|
+
Never kept, on any path: every `/admin/...` read (above), sign-in and account calls, `/me` calls, bookings,
|
|
142
|
+
user groups, option pickers, tag ranges, realtime tokens and a collection's translation revision.
|
|
114
143
|
|
|
115
144
|
## Manual Cache Control
|
|
116
145
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Data changed: an open screen reads again
|
|
2
|
+
|
|
3
|
+
Since 2.2.3.
|
|
4
|
+
|
|
5
|
+
An admin screen is not the only thing that changes records. The assistant applies a change, another app
|
|
6
|
+
saves, another person edits. The screen that is open keeps showing what it loaded, and the person has to
|
|
7
|
+
reload to see the change. **Data changed** is the host telling the app, so it can read again.
|
|
8
|
+
|
|
9
|
+
It is one message, host to app:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
{ type: 'smartlinks:data-changed', collectionId: 'acme', changes: [{ type: 'product', id: 'p_1', status: 'updated' }], source: 'assistant' }
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Ids only, never data. The app reads again through the normal API, with the normal permissions.
|
|
16
|
+
|
|
17
|
+
## In an app
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import * as SL from '@proveanything/smartlinks'
|
|
21
|
+
|
|
22
|
+
useEffect(() => SL.onDataChanged((e) => {
|
|
23
|
+
if (e.collectionId && e.collectionId !== collectionId) return
|
|
24
|
+
if (e.touches({ type: 'product' })) queryClient.invalidateQueries({ queryKey: ['products'] })
|
|
25
|
+
if (e.touches({ type: 'product', id: productId })) queryClient.invalidateQueries({ queryKey: ['product', collectionId, productId] })
|
|
26
|
+
}), [collectionId, productId])
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- `SL.onDataChanged(listener)` returns a function that stops listening. It does nothing outside a frame.
|
|
30
|
+
- Before your listener runs, the SDK has already forgotten the reads it kept for what changed, so the
|
|
31
|
+
next read returns what the server has now. This happens even in an app that never calls `onDataChanged`.
|
|
32
|
+
- `e.changes` is what changed. It is **empty when the host can't say**: treat everything you show from
|
|
33
|
+
the collection as possibly changed.
|
|
34
|
+
- `e.touches({ type, id })` answers "might this be about my record?". It is true when a change matches
|
|
35
|
+
every field you give, and always true when `e.changes` is empty.
|
|
36
|
+
- `e.source` is who made the change when the host knows (`'assistant'`, `'app'`, `'person'`).
|
|
37
|
+
|
|
38
|
+
### A form the person is filling in
|
|
39
|
+
|
|
40
|
+
Reading again must not throw away typing.
|
|
41
|
+
|
|
42
|
+
- **Untouched form:** fill it again from the new record.
|
|
43
|
+
- **Form with changes in it:** keep what the person typed. Say plainly that the record was changed
|
|
44
|
+
elsewhere, and offer to load the new version. Do not save the old copy over the new one without asking.
|
|
45
|
+
|
|
46
|
+
Compare the record you loaded with the one you just read (its `updatedAt`, or the fields the form shows)
|
|
47
|
+
before saying anything: "something changed" is not always about the record on screen.
|
|
48
|
+
|
|
49
|
+
### One change word per kind of record
|
|
50
|
+
|
|
51
|
+
`type` uses the same words as realtime changes (`docs/realtime.md`): `product`, `proof`, `asset`, `record`,
|
|
52
|
+
`collection`, `collection.apps`, `settings`. A host may also send its own words for other records
|
|
53
|
+
(`contact`, `appConfig`). `id` is the record's id; `productId`, `proofId`, `appId` and `recordType` say
|
|
54
|
+
where it lives when that helps.
|
|
55
|
+
|
|
56
|
+
## In a host
|
|
57
|
+
|
|
58
|
+
Send it after a change the app did not make itself.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
// Any host
|
|
62
|
+
SL.postDataChanged(iframe.contentWindow, { collectionId, changes: [{ type: 'product', id }], source: 'assistant' })
|
|
63
|
+
|
|
64
|
+
// With IframeResponder
|
|
65
|
+
responder.dataChanged({ collectionId, changes: [{ type: 'product', id }] })
|
|
66
|
+
|
|
67
|
+
// When you can't say what changed
|
|
68
|
+
SL.postDataChanged(iframe.contentWindow, { collectionId })
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Studio sends it to every open app frame when an assistant change set is applied or undone, when the
|
|
72
|
+
assistant finishes a step that writes, and when its turn ends.
|
|
73
|
+
|
|
74
|
+
## What it is not
|
|
75
|
+
|
|
76
|
+
- It is not the realtime feed. `SL.realtime.onChange` (`docs/realtime.md`) is the server telling a page
|
|
77
|
+
that something changed, over a live connection. Data changed is the host passing a change it already
|
|
78
|
+
knows about into a frame, with no connection of the app's own. A host that listens to the realtime
|
|
79
|
+
feed can pass those changes on with the same message, so an app needs only `onDataChanged`.
|
|
80
|
+
- It does not carry data and it is not a save. Nothing is written by sending or hearing it.
|
|
81
|
+
|
|
82
|
+
## What the SDK forgets
|
|
83
|
+
|
|
84
|
+
| The host says | The SDK forgets |
|
|
85
|
+
|---|---|
|
|
86
|
+
| Only products and proofs changed | Everything kept under that collection's products (the product, its lists, its proofs) |
|
|
87
|
+
| Anything else, or nothing specific | Everything kept for that collection |
|
|
88
|
+
| No collection given | Everything kept |
|
|
89
|
+
|
|
90
|
+
Admin reads are never kept in the first place (`docs/caching.md`), so this matters for public reads an
|
|
91
|
+
admin screen makes, and for apps on an SDK before 2.2.2.
|