@snowcone-app/ui 0.9.7 → 0.9.8
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/CHANGELOG.md +13 -0
- package/dist/index.cjs +40 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +40 -12
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/composed/HeroProductImage.tsx +11 -1
- package/src/patterns/RealtimeProvider.tsx +52 -12
- package/src/patterns/__tests__/RealtimeProvider.blobLifecycle.test.tsx +57 -0
- package/src/patterns/__tests__/RealtimeProvider.cropDelivery.test.tsx +27 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/ui",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.8",
|
|
4
4
|
"description": "React components for merchandise visualization and customization",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
"react-instantsearch": "^7.15.5",
|
|
104
104
|
"react-zoom-pan-pinch": "^3.6.4",
|
|
105
105
|
"tailwind-merge": "^3.0.0",
|
|
106
|
-
"@snowcone-app/sdk": "0.
|
|
106
|
+
"@snowcone-app/sdk": "0.26.0"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
109
|
"@chromatic-com/storybook": "^4.1.2",
|
|
@@ -559,6 +559,16 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
559
559
|
const arrivedVersionRef = useRef(0);
|
|
560
560
|
const versionByUrlRef = useRef<Map<string, { v: number; versions: Record<string, number> | null }>>(new Map());
|
|
561
561
|
const loadedUrlRef = useRef<string | null>(null);
|
|
562
|
+
// This element's identity for frame release (a picture can have several elements showing different frames).
|
|
563
|
+
const blobConsumerRef = useRef(`el-${Math.random().toString(36).slice(2, 10)}`);
|
|
564
|
+
const releaseBlobConsumer = realtimeContext?.releaseBlobConsumer;
|
|
565
|
+
useEffect(() => {
|
|
566
|
+
const id = actualMockupId;
|
|
567
|
+
const consumer = blobConsumerRef.current;
|
|
568
|
+
return () => {
|
|
569
|
+
if (id) releaseBlobConsumer?.(id, consumer);
|
|
570
|
+
};
|
|
571
|
+
}, [actualMockupId, releaseBlobConsumer]);
|
|
562
572
|
const frameErrorRef = useRef<string | null>(null);
|
|
563
573
|
const showVersionOf = useCallback((url: string) => {
|
|
564
574
|
const e = versionByUrlRef.current.get(url);
|
|
@@ -1177,7 +1187,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
1177
1187
|
if (renderedUrl) {
|
|
1178
1188
|
loadedUrlRef.current = renderedUrl;
|
|
1179
1189
|
showVersionOf(renderedUrl);
|
|
1180
|
-
if (actualMockupId) realtimeContext?.markBlobShown?.(actualMockupId, renderedUrl);
|
|
1190
|
+
if (actualMockupId) realtimeContext?.markBlobShown?.(actualMockupId, renderedUrl, blobConsumerRef.current);
|
|
1181
1191
|
applyRenderStampRef.current?.();
|
|
1182
1192
|
}
|
|
1183
1193
|
if (!onLoadCalledRef.current && onLoad) {
|
|
@@ -309,7 +309,10 @@ export interface RealtimeContextValue {
|
|
|
309
309
|
registerBlobUrl: (blobOrUrl: Blob | string, placementName?: string) => string;
|
|
310
310
|
|
|
311
311
|
/** A picture's <img> loaded `url` (from getMockupUrl): its older composited frames are released. */
|
|
312
|
-
markBlobShown: (mockupId: string, url: string) => void;
|
|
312
|
+
markBlobShown: (mockupId: string, url: string, consumer?: string) => void;
|
|
313
|
+
|
|
314
|
+
/** An element that called markBlobShown unmounted: it no longer holds its frame. */
|
|
315
|
+
releaseBlobConsumer: (mockupId: string, consumer: string) => void;
|
|
313
316
|
}
|
|
314
317
|
|
|
315
318
|
const RealtimeContext = createContext<RealtimeContextValue | undefined>(
|
|
@@ -513,7 +516,11 @@ export function RealtimeProvider({
|
|
|
513
516
|
getLastSentVersion,
|
|
514
517
|
getLatestSentVersions,
|
|
515
518
|
rerender: rerenderRaw,
|
|
519
|
+
isFrameSuperseded,
|
|
516
520
|
} = realtimeMockup;
|
|
521
|
+
// Read through a ref: the composite pump's callbacks stay stable across renders.
|
|
522
|
+
const isFrameSupersededRef = useRef(isFrameSuperseded);
|
|
523
|
+
isFrameSupersededRef.current = isFrameSuperseded;
|
|
517
524
|
|
|
518
525
|
// PERFORMANCE: Store mockup results in ref only - NO STATE UPDATES
|
|
519
526
|
// This eliminates re-render cascades entirely. Components that need mockup results
|
|
@@ -581,7 +588,8 @@ export function RealtimeProvider({
|
|
|
581
588
|
const registeredBlobUrlsRef = useRef<Set<string>>(new Set());
|
|
582
589
|
// Composited frames per picture (newest first), and the one its <img> shows: revoked only when that picture moves on.
|
|
583
590
|
const blobsByOwnerRef = useRef<Map<string, string[]>>(new Map());
|
|
584
|
-
|
|
591
|
+
// Per picture: what EACH of its elements shows (a gallery can render one picture twice: the phone carousel does).
|
|
592
|
+
const shownBlobByOwnerRef = useRef<Map<string, Map<string, string>>>(new Map());
|
|
585
593
|
|
|
586
594
|
// Track which placements have pending exports (for shimmer state)
|
|
587
595
|
const pendingPlacementsRef = useRef<Set<string>>(new Set());
|
|
@@ -677,9 +685,9 @@ export function RealtimeProvider({
|
|
|
677
685
|
const url = URL.createObjectURL(blobOrUrl);
|
|
678
686
|
if (owner) {
|
|
679
687
|
const list = [url, ...(blobsByOwnerRef.current.get(owner) ?? [])]; // newest first
|
|
680
|
-
const shown = shownBlobByOwnerRef.current.get(owner);
|
|
688
|
+
const shown = new Set(shownBlobByOwnerRef.current.get(owner)?.values() ?? []);
|
|
681
689
|
while (list.length > MAX_FRAMES_PER_PICTURE) {
|
|
682
|
-
const victimIdx = [...list.keys()].reverse().find((i) => i > 0 && list[i]
|
|
690
|
+
const victimIdx = [...list.keys()].reverse().find((i) => i > 0 && !shown.has(list[i]));
|
|
683
691
|
if (victimIdx === undefined) break;
|
|
684
692
|
const [victim] = list.splice(victimIdx, 1);
|
|
685
693
|
console.error(
|
|
@@ -698,17 +706,40 @@ export function RealtimeProvider({
|
|
|
698
706
|
return url;
|
|
699
707
|
}, [revokeBlobUrl]);
|
|
700
708
|
|
|
701
|
-
/**
|
|
702
|
-
const
|
|
709
|
+
/** Release a picture's frames that are older than what EVERY one of its elements shows. */
|
|
710
|
+
const releaseOlderFrames = useCallback((owner: string) => {
|
|
703
711
|
const list = blobsByOwnerRef.current.get(owner);
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
712
|
+
const shown = shownBlobByOwnerRef.current.get(owner);
|
|
713
|
+
if (!list || !shown || shown.size === 0) return;
|
|
714
|
+
// The oldest frame any element still shows: everything older is unreachable.
|
|
715
|
+
const oldestShownIdx = Math.max(...[...shown.values()].map((u) => list.indexOf(u)));
|
|
716
|
+
if (oldestShownIdx < 0) return;
|
|
717
|
+
list.slice(oldestShownIdx + 1).forEach(revokeBlobUrl);
|
|
718
|
+
blobsByOwnerRef.current.set(owner, list.slice(0, oldestShownIdx + 1));
|
|
710
719
|
}, [revokeBlobUrl]);
|
|
711
720
|
|
|
721
|
+
/**
|
|
722
|
+
* An element of a picture (`consumer`: one per <img>) has loaded `url`. A picture can be rendered by more than one
|
|
723
|
+
* element (the phone carousel renders each twice): releasing on the FIRST element's load revoked the frame its twin
|
|
724
|
+
* was still loading, so the twin kept its old image for good (prod phone, 2026-11-11, after #4390). Frames go only
|
|
725
|
+
* when they are older than what every element shows.
|
|
726
|
+
*/
|
|
727
|
+
const markBlobShown = useCallback((owner: string, url: string, consumer = "default") => {
|
|
728
|
+
const list = blobsByOwnerRef.current.get(owner);
|
|
729
|
+
if (!list || list.indexOf(url) < 0) return;
|
|
730
|
+
const shown = shownBlobByOwnerRef.current.get(owner) ?? new Map<string, string>();
|
|
731
|
+
shown.set(consumer, url);
|
|
732
|
+
shownBlobByOwnerRef.current.set(owner, shown);
|
|
733
|
+
releaseOlderFrames(owner);
|
|
734
|
+
}, [releaseOlderFrames]);
|
|
735
|
+
|
|
736
|
+
/** An element of a picture is gone (unmount): it no longer holds a frame. */
|
|
737
|
+
const releaseBlobConsumer = useCallback((owner: string, consumer: string) => {
|
|
738
|
+
const shown = shownBlobByOwnerRef.current.get(owner);
|
|
739
|
+
if (!shown?.delete(consumer)) return;
|
|
740
|
+
releaseOlderFrames(owner);
|
|
741
|
+
}, [releaseOlderFrames]);
|
|
742
|
+
|
|
712
743
|
const croppedDeliveryEnabled = delivery !== "full-frame";
|
|
713
744
|
|
|
714
745
|
/** Re-send the last config with/without overlayCrop (delivery flip only). */
|
|
@@ -1018,6 +1049,13 @@ export function RealtimeProvider({
|
|
|
1018
1049
|
) {
|
|
1019
1050
|
return;
|
|
1020
1051
|
}
|
|
1052
|
+
// The SDK revoked this frame because the mockup's NEXT frame has
|
|
1053
|
+
// arrived. React only learns of it with that batch's
|
|
1054
|
+
// all_mockups_rendered, which on a 31-picture product can come well
|
|
1055
|
+
// after the 2 s grace below (prod phone case, 2026-11-11, rendercenter
|
|
1056
|
+
// 20d5eaf: v3 batch 2.3 s, the grace fired first, cropped delivery went
|
|
1057
|
+
// off and a visible picture stayed at v2). The SDK knows at once.
|
|
1058
|
+
if (isFrameSupersededRef.current?.(result.imageUrl)) return;
|
|
1021
1059
|
// The FRAME (not the background) failed to load from a revoked blob:
|
|
1022
1060
|
// URL. The SDK revokes a mockup's frame only when that mockup's next
|
|
1023
1061
|
// frame arrives, so the newer frame exists even if React state hasn't
|
|
@@ -1828,6 +1866,7 @@ export function RealtimeProvider({
|
|
|
1828
1866
|
isMockupLoading,
|
|
1829
1867
|
registerBlobUrl,
|
|
1830
1868
|
markBlobShown,
|
|
1869
|
+
releaseBlobConsumer,
|
|
1831
1870
|
// WebRTC streaming
|
|
1832
1871
|
subscribeRTCTiming,
|
|
1833
1872
|
}),
|
|
@@ -1862,6 +1901,7 @@ export function RealtimeProvider({
|
|
|
1862
1901
|
isMockupLoading,
|
|
1863
1902
|
registerBlobUrl,
|
|
1864
1903
|
markBlobShown,
|
|
1904
|
+
releaseBlobConsumer,
|
|
1865
1905
|
]
|
|
1866
1906
|
);
|
|
1867
1907
|
|
|
@@ -211,4 +211,61 @@ describe("composited frames per picture (prod desktop 2026-11-11: 15 ERR_FILE_NO
|
|
|
211
211
|
capturedCtx.markBlobShown("m0", second);
|
|
212
212
|
expect(revoked).toEqual([firstUrls[0]]);
|
|
213
213
|
});
|
|
214
|
+
|
|
215
|
+
it("a picture rendered by N elements (the phone page renders each twice): a frame goes only when EVERY element has moved past it", async () => {
|
|
216
|
+
const revoked: string[] = [];
|
|
217
|
+
globalThis.URL.revokeObjectURL = vi.fn((u: string) => { revoked.push(u); });
|
|
218
|
+
const { rerender } = render(<Harness version={1} />);
|
|
219
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
220
|
+
h.state.results = IDS.map((id) => fullResult(id, `https://r/${id}-full`));
|
|
221
|
+
await act(async () => rerender(<Harness version={2} />));
|
|
222
|
+
await waitFor(() => expect(sentConfigs().some((c) => c.overlayCrop === "auto")).toBe(true));
|
|
223
|
+
h.state.results = IDS.map((id) => croppedResult(id, `https://r/${id}-crop-1`));
|
|
224
|
+
await act(async () => rerender(<Harness version={3} />));
|
|
225
|
+
await waitFor(() => expect(h.compositeCroppedFrame).toHaveBeenCalledTimes(31));
|
|
226
|
+
await act(async () => { await new Promise((r) => setTimeout(r, 50)); });
|
|
227
|
+
const f1 = capturedCtx.getMockupUrl("m0")!;
|
|
228
|
+
// N elements of m0 (the phone page mounts the carousel twice; a third surface for the general case) show frame 1.
|
|
229
|
+
for (const el of ["hero", "twin", "third"]) capturedCtx.markBlobShown("m0", f1, el);
|
|
230
|
+
h.state.results = IDS.map((id) => croppedResult(id, `https://r/${id}-crop-2`));
|
|
231
|
+
await act(async () => rerender(<Harness version={4} />));
|
|
232
|
+
await waitFor(() => expect(h.compositeCroppedFrame).toHaveBeenCalledTimes(62));
|
|
233
|
+
await act(async () => { await new Promise((r) => setTimeout(r, 50)); });
|
|
234
|
+
const f2 = capturedCtx.getMockupUrl("m0")!;
|
|
235
|
+
// The hero shows frame 2 first; the twin still shows (and may be reloading) frame 1: it must stay alive.
|
|
236
|
+
capturedCtx.markBlobShown("m0", f2, "hero");
|
|
237
|
+
expect(revoked).toEqual([]);
|
|
238
|
+
// A second element catches up: the third still shows frame 1, so it stays.
|
|
239
|
+
capturedCtx.markBlobShown("m0", f2, "twin");
|
|
240
|
+
expect(revoked).toEqual([]);
|
|
241
|
+
// The last element catches up: now frame 1 is unreachable and goes.
|
|
242
|
+
capturedCtx.markBlobShown("m0", f2, "third");
|
|
243
|
+
expect(revoked).toEqual([f1]);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("an element that unmounts stops holding its frame", async () => {
|
|
247
|
+
const revoked: string[] = [];
|
|
248
|
+
globalThis.URL.revokeObjectURL = vi.fn((u: string) => { revoked.push(u); });
|
|
249
|
+
const { rerender } = render(<Harness version={1} />);
|
|
250
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
251
|
+
h.state.results = IDS.map((id) => fullResult(id, `https://r/${id}-full`));
|
|
252
|
+
await act(async () => rerender(<Harness version={2} />));
|
|
253
|
+
await waitFor(() => expect(sentConfigs().some((c) => c.overlayCrop === "auto")).toBe(true));
|
|
254
|
+
h.state.results = IDS.map((id) => croppedResult(id, `https://r/${id}-crop-1`));
|
|
255
|
+
await act(async () => rerender(<Harness version={3} />));
|
|
256
|
+
await waitFor(() => expect(h.compositeCroppedFrame).toHaveBeenCalledTimes(31));
|
|
257
|
+
await act(async () => { await new Promise((r) => setTimeout(r, 50)); });
|
|
258
|
+
const f1 = capturedCtx.getMockupUrl("m0")!;
|
|
259
|
+
capturedCtx.markBlobShown("m0", f1, "hero");
|
|
260
|
+
capturedCtx.markBlobShown("m0", f1, "twin");
|
|
261
|
+
h.state.results = IDS.map((id) => croppedResult(id, `https://r/${id}-crop-2`));
|
|
262
|
+
await act(async () => rerender(<Harness version={4} />));
|
|
263
|
+
await waitFor(() => expect(h.compositeCroppedFrame).toHaveBeenCalledTimes(62));
|
|
264
|
+
await act(async () => { await new Promise((r) => setTimeout(r, 50)); });
|
|
265
|
+
capturedCtx.markBlobShown("m0", capturedCtx.getMockupUrl("m0")!, "hero");
|
|
266
|
+
expect(revoked).toEqual([]);
|
|
267
|
+
capturedCtx.releaseBlobConsumer("m0", "twin");
|
|
268
|
+
expect(revoked).toEqual([f1]);
|
|
269
|
+
});
|
|
214
270
|
});
|
|
271
|
+
|
|
@@ -43,6 +43,7 @@ const h = vi.hoisted(() => {
|
|
|
43
43
|
getLastSentVersion: vi.fn(() => 0),
|
|
44
44
|
getLatestSentVersions: vi.fn(() => ({})),
|
|
45
45
|
rerender: vi.fn((_ids: string[]) => true),
|
|
46
|
+
isFrameSuperseded: vi.fn((_url: string) => false),
|
|
46
47
|
opts: null as null | { onError?: (msg: string, detail: Record<string, unknown>) => void },
|
|
47
48
|
};
|
|
48
49
|
});
|
|
@@ -68,6 +69,7 @@ vi.mock("@snowcone-app/sdk/react", () => ({
|
|
|
68
69
|
getLastSentVersion: h.getLastSentVersion,
|
|
69
70
|
getLatestSentVersions: h.getLatestSentVersions,
|
|
70
71
|
rerender: h.rerender,
|
|
72
|
+
isFrameSuperseded: h.isFrameSuperseded,
|
|
71
73
|
};
|
|
72
74
|
},
|
|
73
75
|
}));
|
|
@@ -435,6 +437,31 @@ describe("RealtimeProvider cropped delivery", () => {
|
|
|
435
437
|
errSpy.mockRestore();
|
|
436
438
|
});
|
|
437
439
|
|
|
440
|
+
it("a frame the SDK superseded is dropped at once, even when the newer batch reaches React after the grace (prod phone case, 20d5eaf, 2026-11-11)", async () => {
|
|
441
|
+
const { rerender } = render(<Harness version={1} />);
|
|
442
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
443
|
+
await seedAndFlip(rerender);
|
|
444
|
+
h.sendConfig.mockClear();
|
|
445
|
+
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
446
|
+
// v3's frame arrived in the SDK (v2 revoked) at the START of a 31-picture batch; the batch's
|
|
447
|
+
// all_mockups_rendered — the only thing that updates React — came 2.3 s later.
|
|
448
|
+
h.isFrameSuperseded.mockImplementation((url: string) => url === "blob:v2-m1");
|
|
449
|
+
h.compositeCroppedFrame.mockImplementationOnce(() =>
|
|
450
|
+
Promise.reject(new Error("E_OVERLAY_FRAME_LOAD: image failed to load: blob:v2-m1"))
|
|
451
|
+
);
|
|
452
|
+
h.state.results = [croppedResult("m1", "blob:v2-m1"), fullResult("m2", "https://r/m2-full")];
|
|
453
|
+
await act(async () => rerender(<Harness version={4} />));
|
|
454
|
+
await act(async () => { await new Promise((r) => setTimeout(r, 2300)); });
|
|
455
|
+
expect(h.sendConfig).not.toHaveBeenCalled(); // cropped delivery stays on: no fallback config
|
|
456
|
+
expect(h.rerender).not.toHaveBeenCalled();
|
|
457
|
+
expect(errSpy.mock.calls.some((c) => String(c[0]).includes("E_OVERLAY_COMPOSITE_FAILED"))).toBe(false);
|
|
458
|
+
// The late batch composites normally.
|
|
459
|
+
h.state.results = [croppedResult("m1", "blob:v3-m1"), fullResult("m2", "https://r/m2-full")];
|
|
460
|
+
await act(async () => rerender(<Harness version={5} />));
|
|
461
|
+
await waitFor(() => expect(h.compositeCroppedFrame).toHaveBeenLastCalledWith(expect.objectContaining({ frameUrl: "blob:v3-m1" })));
|
|
462
|
+
errSpy.mockRestore();
|
|
463
|
+
});
|
|
464
|
+
|
|
438
465
|
it("a revoked frame with no newer frame within the grace window is a real failure: full-frame re-render", async () => {
|
|
439
466
|
const { rerender } = render(<Harness version={1} />);
|
|
440
467
|
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|