@snowcone-app/ui 0.10.0 → 0.10.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/CHANGELOG.md +28 -0
- package/dist/index.cjs +99 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +100 -24
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/composed/HeroProductImage.tsx +13 -1
- package/src/composed/carousels/MobileProductCarousel.tsx +2 -0
- package/src/composed/carousels/types.ts +7 -0
- package/src/patterns/RealtimeProvider.tsx +117 -23
- package/src/patterns/__tests__/RealtimeProvider.blobLifecycle.test.tsx +1 -1
- package/src/patterns/__tests__/RealtimeProvider.cropDelivery.test.tsx +1 -1
- package/src/patterns/__tests__/RealtimeProvider.frameAcks.test.tsx +99 -0
- package/src/patterns/__tests__/RealtimeProvider.sessionShape.test.tsx +37 -1
- package/src/patterns/__tests__/RealtimeProvider.sizePicture.test.tsx +75 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/ui",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.3",
|
|
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.27.0"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
109
|
"@chromatic-com/storybook": "^4.1.2",
|
|
@@ -591,6 +591,8 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
591
591
|
const getLatestSentVersions = realtimeContext?.getLatestSentVersions;
|
|
592
592
|
const refusalCodeRef = useRef<string | null>(null);
|
|
593
593
|
refusalCodeRef.current = unavailable?.code ?? null;
|
|
594
|
+
const frameFailedRef = useRef(realtimeContext?.frameFailed);
|
|
595
|
+
frameFailedRef.current = realtimeContext?.frameFailed;
|
|
594
596
|
const sessionErrorCodeRef = useRef<string | null>(null);
|
|
595
597
|
sessionErrorCodeRef.current = realtimeContext?.renderError?.code ?? null;
|
|
596
598
|
const getCanvasVersion = realtimeContext?.getCanvasVersion;
|
|
@@ -598,6 +600,14 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
598
600
|
const applyRenderStamp = useCallback(() => {
|
|
599
601
|
const node = containerRef.current;
|
|
600
602
|
if (!node) return;
|
|
603
|
+
// A static picture is not rendered by the realtime session: it is not behind the canvas, it is not live. Say so
|
|
604
|
+
// (never "stale" forever, never "current" over an old render).
|
|
605
|
+
if (mode === "static") {
|
|
606
|
+
delete node.dataset.canvasVersion;
|
|
607
|
+
delete node.dataset.renderVersion;
|
|
608
|
+
node.dataset.renderState = "static";
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
601
611
|
const canvas = getCanvasVersion?.() ?? 0;
|
|
602
612
|
if (canvas <= 0) {
|
|
603
613
|
delete node.dataset.canvasVersion;
|
|
@@ -632,7 +642,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
632
642
|
node.dataset.wantedWidth = String(shape.width);
|
|
633
643
|
node.dataset.sessionPhase = shape.phase;
|
|
634
644
|
}
|
|
635
|
-
}, [getCanvasVersion, getLatestSentVersions, getSessionShape]);
|
|
645
|
+
}, [getCanvasVersion, getLatestSentVersions, getSessionShape, mode]);
|
|
636
646
|
const applyRenderStampRef = useRef(applyRenderStamp);
|
|
637
647
|
applyRenderStampRef.current = applyRenderStamp;
|
|
638
648
|
// Re-apply after every render (the root node can change; refusals are state).
|
|
@@ -1136,6 +1146,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
1136
1146
|
// its data-render-version stays behind, so the picture reads stale, never current over an old image.
|
|
1137
1147
|
preload.onerror = () => {
|
|
1138
1148
|
frameErrorRef.current = "E_FRAME_UNAVAILABLE";
|
|
1149
|
+
frameFailedRef.current?.(displayUrl); // done with it (frame flow control): the box may send the next one
|
|
1139
1150
|
applyRenderStampRef.current?.();
|
|
1140
1151
|
};
|
|
1141
1152
|
|
|
@@ -1271,6 +1282,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
1271
1282
|
// image is still available to keep).
|
|
1272
1283
|
setImgFailed(true);
|
|
1273
1284
|
setFirstImageLoaded(true);
|
|
1285
|
+
if (renderedUrl) realtimeContext?.frameFailed?.(renderedUrl);
|
|
1274
1286
|
onError?.();
|
|
1275
1287
|
// Dev-only guidance for the most common signed-shop trap: a mockup
|
|
1276
1288
|
// <img> that loads an UNSIGNED resolver URL gets a 403 "Missing
|
|
@@ -888,6 +888,8 @@ export const MobileProductCarousel = memo(function MobileProductCarousel({
|
|
|
888
888
|
<div
|
|
889
889
|
key={index}
|
|
890
890
|
className="w-full h-full flex-shrink-0 relative select-none"
|
|
891
|
+
data-gallery-role={image.galleryRole}
|
|
892
|
+
data-mockup-id={image.galleryRole ? image.mockupId : undefined}
|
|
891
893
|
>
|
|
892
894
|
{!isWithinDecodeWindow ? (
|
|
893
895
|
<div className="w-full h-full bg-muted" />
|
|
@@ -50,6 +50,13 @@ export interface CarouselImage {
|
|
|
50
50
|
|
|
51
51
|
/** Aspect ratio of the mockup (e.g., 1.78 for 16:9) */
|
|
52
52
|
aspectRatio?: number;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What this picture is in the gallery. 'size-picture': the picture of the chosen size (exactly one, last). The
|
|
56
|
+
* realtime session renders it like any picture (the box renders each mockup at the variant it lists, rc#858).
|
|
57
|
+
* Stamped data-gallery-role on the slide.
|
|
58
|
+
*/
|
|
59
|
+
galleryRole?: "size-picture";
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
/**
|
|
@@ -37,7 +37,7 @@ import React, {
|
|
|
37
37
|
useMemo,
|
|
38
38
|
} from "react";
|
|
39
39
|
import { useRealtimeMockup } from "@snowcone-app/sdk/react";
|
|
40
|
-
import { resolvePreviewVariantId, resolveVariantId as resolveVariantIdUtil } from "@snowcone-app/sdk";
|
|
40
|
+
import { gallerySizePicture, mockupRole, resolvePreviewVariantId, resolveVariantId as resolveVariantIdUtil } from "@snowcone-app/sdk";
|
|
41
41
|
import type {
|
|
42
42
|
RealtimeRenderError,
|
|
43
43
|
MockupResult as SdkMockupResult,
|
|
@@ -75,6 +75,8 @@ const FRAME_SUPERSEDE_GRACE_MS = 2000;
|
|
|
75
75
|
const RERENDER_MIN_INTERVAL_MS = 550;
|
|
76
76
|
/** Quiet time after the pipeline settles before an edit counts as stopped (the catch-up renders every picture). */
|
|
77
77
|
const EDIT_QUIET_MS = 600;
|
|
78
|
+
/** An announced export that has not gone out after this long is lost: it stops holding the edit open. */
|
|
79
|
+
const EXPORT_PENDING_MAX_MS = 5000;
|
|
78
80
|
|
|
79
81
|
/** `current` in order, then any of `extra` not already in it. */
|
|
80
82
|
function mergeMockupIds(current: readonly string[], extra: readonly string[]): string[] {
|
|
@@ -327,6 +329,9 @@ export interface RealtimeContextValue {
|
|
|
327
329
|
|
|
328
330
|
/** A picture's <img> loaded `url` (from getMockupUrl): its older composited frames are released. */
|
|
329
331
|
markBlobShown: (mockupId: string, url: string, consumer?: string) => void;
|
|
332
|
+
/** A picture's img failed to load a delivered frame. With frame flow control, every frame must be reported done
|
|
333
|
+
* (drawn via markBlobShown, or failed here), or the box waits for it until its ack timeout (E_RT_ACK_TIMEOUT). */
|
|
334
|
+
frameFailed: (url: string) => void;
|
|
330
335
|
|
|
331
336
|
/** An element that called markBlobShown unmounted: it no longer holds its frame. */
|
|
332
337
|
releaseBlobConsumer: (mockupId: string, consumer: string) => void;
|
|
@@ -443,6 +448,8 @@ export function RealtimeProvider({
|
|
|
443
448
|
|
|
444
449
|
// Track last sent variantId to detect when we need to re-send config
|
|
445
450
|
const lastSentVariantIdRef = useRef<string | null>(null);
|
|
451
|
+
// The size picture in the session (the chosen size's; null = none): a size change re-sends the config.
|
|
452
|
+
const lastSentSizePictureRef = useRef<string | null>(null);
|
|
446
453
|
const lastSentBgRef = useRef<string | undefined>(undefined);
|
|
447
454
|
// Spread crop geometry (ADR-0060) declared via setSpreadCrops. Ref holds
|
|
448
455
|
// the payload; the version state re-runs the config effect on change.
|
|
@@ -544,6 +551,7 @@ export function RealtimeProvider({
|
|
|
544
551
|
getLatestSentVersions,
|
|
545
552
|
rerender: rerenderRaw,
|
|
546
553
|
isFrameSuperseded,
|
|
554
|
+
ackFrame,
|
|
547
555
|
} = realtimeMockup;
|
|
548
556
|
// Read through a ref: the composite pump's callbacks stay stable across renders.
|
|
549
557
|
const isFrameSupersededRef = useRef(isFrameSuperseded);
|
|
@@ -599,7 +607,19 @@ export function RealtimeProvider({
|
|
|
599
607
|
// it (+ when it went out). "Settled" fires once per version, when its last
|
|
600
608
|
// mockup lands, so the export service can keep N edits in flight (pipeline
|
|
601
609
|
// depth) and count landings exactly. Blob sends keep the throttle-queue rule.
|
|
602
|
-
const dueByVersionRef = useRef<Map<number, { due: number; at: number }>>(new Map());
|
|
610
|
+
const dueByVersionRef = useRef<Map<number, { due: number; at: number; seen?: Set<string> }>>(new Map());
|
|
611
|
+
// Frame flow control: the frame (mockup, seq) behind every URL this provider delivered, so the moment a picture
|
|
612
|
+
// draws it (markBlobShown) or fails it (frameFailed) the box hears "done". Bounded.
|
|
613
|
+
const frameByUrlRef = useRef<Map<string, { mockupId: string; seq: number }>>(new Map());
|
|
614
|
+
const ackFrameRef = useRef<((mockupId: string, seq: number) => boolean) | null>(null);
|
|
615
|
+
ackFrameRef.current = ackFrame;
|
|
616
|
+
const ackUrl = useCallback((url: string) => {
|
|
617
|
+
const f = frameByUrlRef.current.get(url);
|
|
618
|
+
if (f) ackFrameRef.current?.(f.mockupId, f.seq);
|
|
619
|
+
}, []);
|
|
620
|
+
const ackResultFrame = useCallback((result: { mockupId: string; seq?: number }) => {
|
|
621
|
+
if (typeof result.seq === "number") ackFrameRef.current?.(result.mockupId, result.seq);
|
|
622
|
+
}, []);
|
|
603
623
|
const configuredMockupIdsRef = useRef<string[]>([]);
|
|
604
624
|
|
|
605
625
|
// Centralized mockup URL cache - stores URLs by mockupId
|
|
@@ -665,6 +685,11 @@ export function RealtimeProvider({
|
|
|
665
685
|
const pictureViewsRef = useRef<Map<string, PictureView>>(new Map());
|
|
666
686
|
const editLiveRef = useRef(false);
|
|
667
687
|
const editQuietTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
688
|
+
// An export announced (notifyPendingExport) but not sent yet: when it started, or 0. While it is pending, the edit is
|
|
689
|
+
// still going, whatever older version settles meanwhile (slow phone link, 2026-11-11: the editor-open frames were
|
|
690
|
+
// still landing when the drag began; their settle + 600 ms quiet fired a 31-picture catch-up mid-gesture, and the
|
|
691
|
+
// edit's own frames queued behind it for 21 s). Older than EXPORT_PENDING_MAX_MS = lost: it no longer holds the edit.
|
|
692
|
+
const exportPendingSinceRef = useRef(0);
|
|
668
693
|
const shapeApplyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
669
694
|
const lastShapeRef = useRef<SessionShape | null>(null);
|
|
670
695
|
const applySessionShapeRef = useRef<(() => void) | null>(null);
|
|
@@ -768,13 +793,19 @@ export function RealtimeProvider({
|
|
|
768
793
|
* when they are older than what every element shows.
|
|
769
794
|
*/
|
|
770
795
|
const markBlobShown = useCallback((owner: string, url: string, consumer = "default") => {
|
|
796
|
+
ackUrl(url); // drawn: the box may send this picture's next frame
|
|
771
797
|
const list = blobsByOwnerRef.current.get(owner);
|
|
772
798
|
if (!list || list.indexOf(url) < 0) return;
|
|
773
799
|
const shown = shownBlobByOwnerRef.current.get(owner) ?? new Map<string, string>();
|
|
774
800
|
shown.set(consumer, url);
|
|
775
801
|
shownBlobByOwnerRef.current.set(owner, shown);
|
|
776
802
|
releaseOlderFrames(owner);
|
|
777
|
-
}, [releaseOlderFrames]);
|
|
803
|
+
}, [releaseOlderFrames, ackUrl]);
|
|
804
|
+
|
|
805
|
+
/** A picture could not load a delivered frame (its img errored): the client is done with it (frame flow control). */
|
|
806
|
+
const frameFailed = useCallback((url: string) => {
|
|
807
|
+
ackUrl(url);
|
|
808
|
+
}, [ackUrl]);
|
|
778
809
|
|
|
779
810
|
/** An element of a picture is gone (unmount): it no longer holds a frame. */
|
|
780
811
|
const releaseBlobConsumer = useCallback((owner: string, consumer: string) => {
|
|
@@ -1048,6 +1079,19 @@ export function RealtimeProvider({
|
|
|
1048
1079
|
pendingRerenderRef.current = pendingRerenderRef.current.filter((id) => !delivered.has(id));
|
|
1049
1080
|
}
|
|
1050
1081
|
|
|
1082
|
+
// Frame flow control: remember which frame each URL is, and say "done" at once for the frames no element will
|
|
1083
|
+
// draw: no picture shows this mockup, or the picture already shows this exact URL (nothing new to load).
|
|
1084
|
+
results.forEach((result) => {
|
|
1085
|
+
const seq = (result as { seq?: number }).seq;
|
|
1086
|
+
if (typeof seq !== "number") return;
|
|
1087
|
+
const byUrl = frameByUrlRef.current;
|
|
1088
|
+
byUrl.set(result.imageUrl, { mockupId: result.mockupId, seq });
|
|
1089
|
+
if (byUrl.size > 1024) byUrl.delete(byUrl.keys().next().value as string);
|
|
1090
|
+
const shownUrl = mockupUrlCacheRef.current.get(result.mockupId)?.url;
|
|
1091
|
+
const watched = (mockupSubscribersByIdRef.current.get(result.mockupId)?.size ?? 0) > 0;
|
|
1092
|
+
if (!watched || shownUrl === result.imageUrl) ackFrameRef.current?.(result.mockupId, seq);
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1051
1095
|
const clearedPlacements = new Set<string>();
|
|
1052
1096
|
results.forEach((result) => {
|
|
1053
1097
|
const wasPending = pendingMockupIdsRef.current.has(result.mockupId);
|
|
@@ -1090,18 +1134,26 @@ export function RealtimeProvider({
|
|
|
1090
1134
|
// frames never all arrive (a failed mockup) is forgotten after 10 s so
|
|
1091
1135
|
// it cannot wedge the policy. Blob sends keep the throttle-queue rule.
|
|
1092
1136
|
let settledVersions = 0;
|
|
1137
|
+
let newestSettled = 0;
|
|
1093
1138
|
const due = dueByVersionRef.current;
|
|
1139
|
+
// A frame at version w answers every due version <= w for its mockup, once per mockup: with frame flow
|
|
1140
|
+
// control (and the box's render coalescing) a mockup's intermediate versions never arrive, only its newest.
|
|
1094
1141
|
results.forEach((result) => {
|
|
1095
|
-
const
|
|
1096
|
-
if (
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1142
|
+
const w = result.requestVersion;
|
|
1143
|
+
if (w === undefined) return;
|
|
1144
|
+
due.forEach((entry, v) => {
|
|
1145
|
+
if (v > w) return;
|
|
1146
|
+
const seen = (entry.seen ??= new Set<string>());
|
|
1147
|
+
if (seen.has(result.mockupId)) return;
|
|
1148
|
+
seen.add(result.mockupId);
|
|
1149
|
+
if (entry.due <= 1) {
|
|
1150
|
+
due.delete(v);
|
|
1151
|
+
settledVersions += 1;
|
|
1152
|
+
newestSettled = Math.max(newestSettled, v);
|
|
1153
|
+
} else {
|
|
1154
|
+
entry.due -= 1;
|
|
1155
|
+
}
|
|
1156
|
+
});
|
|
1105
1157
|
});
|
|
1106
1158
|
due.forEach((entry, v) => {
|
|
1107
1159
|
if (now - entry.at > 10_000) due.delete(v);
|
|
@@ -1110,15 +1162,19 @@ export function RealtimeProvider({
|
|
|
1110
1162
|
if (!pipelineResetCooldownRef.current) {
|
|
1111
1163
|
hasBlobInFlightRef.current = false;
|
|
1112
1164
|
for (let i = 0; i < settledVersions; i++) pipelineSettledSubscribersRef.current.forEach((cb) => cb());
|
|
1113
|
-
|
|
1165
|
+
// The EDIT settles only when its newest version has landed and no further export is on its way: an older
|
|
1166
|
+
// version (the editor-open frames on a slow link) settling mid-gesture is not the edit stopping.
|
|
1167
|
+
const exportPending =
|
|
1168
|
+
exportPendingSinceRef.current > 0 && Date.now() - exportPendingSinceRef.current < EXPORT_PENDING_MAX_MS;
|
|
1169
|
+
if (!exportPending && newestSettled >= getLastSentVersion()) markEditSettled();
|
|
1114
1170
|
}
|
|
1115
1171
|
} else if (due.size === 0 && hasBlobInFlightRef.current && !hasPendingBlobs() && !pipelineResetCooldownRef.current) {
|
|
1116
1172
|
hasBlobInFlightRef.current = false;
|
|
1117
1173
|
pipelineSettledSubscribersRef.current.forEach((cb) => cb());
|
|
1118
|
-
markEditSettled();
|
|
1174
|
+
if (!(exportPendingSinceRef.current > 0 && Date.now() - exportPendingSinceRef.current < EXPORT_PENDING_MAX_MS)) markEditSettled();
|
|
1119
1175
|
}
|
|
1120
1176
|
},
|
|
1121
|
-
[hasPendingBlobs, markEditSettled]
|
|
1177
|
+
[hasPendingBlobs, markEditSettled, getLastSentVersion]
|
|
1122
1178
|
);
|
|
1123
1179
|
|
|
1124
1180
|
/** Composite one cropped frame over its frozen background and deliver it. */
|
|
@@ -1134,6 +1190,7 @@ export function RealtimeProvider({
|
|
|
1134
1190
|
console.error(
|
|
1135
1191
|
`[RealtimeProvider] E_OVERLAY_NO_BACKGROUND: cropped frame for ${result.mockupId} has no frozen background — falling back to full-frame and re-rendering it.`
|
|
1136
1192
|
);
|
|
1193
|
+
ackResultFrame(result); // never drawn; the re-render comes as a new frame
|
|
1137
1194
|
rerenderFullFrame(result.mockupId);
|
|
1138
1195
|
return;
|
|
1139
1196
|
}
|
|
@@ -1149,10 +1206,15 @@ export function RealtimeProvider({
|
|
|
1149
1206
|
rect: result.cropRect!,
|
|
1150
1207
|
canvas: compositeCanvasRef.current ?? undefined,
|
|
1151
1208
|
});
|
|
1152
|
-
// Scene changed while compositing (variant/bg flip) — stale, drop.
|
|
1153
|
-
if (gen !== sceneGenRef.current)
|
|
1209
|
+
// Scene changed while compositing (variant/bg flip) — stale, drop (and done with it: flow control).
|
|
1210
|
+
if (gen !== sceneGenRef.current) {
|
|
1211
|
+
ackResultFrame(result);
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1154
1214
|
deliverResults([{ ...result, imageUrl: registerBlobUrl(blob, undefined, result.mockupId) }]);
|
|
1155
1215
|
} catch (err) {
|
|
1216
|
+
// Whatever happens next, this frame will not be drawn: done with it (flow control; a re-render is a new frame).
|
|
1217
|
+
ackResultFrame(result);
|
|
1156
1218
|
// A newer frame for this mockup already arrived: the SDK revokes a
|
|
1157
1219
|
// mockup's previous blob: frame the moment the next one lands, so a
|
|
1158
1220
|
// composite still loading the old frame fails mid-drag. That is
|
|
@@ -1209,7 +1271,7 @@ export function RealtimeProvider({
|
|
|
1209
1271
|
rerenderFullFrame(result.mockupId);
|
|
1210
1272
|
}
|
|
1211
1273
|
},
|
|
1212
|
-
[deliverResults, registerBlobUrl, rerenderFullFrame]
|
|
1274
|
+
[deliverResults, registerBlobUrl, rerenderFullFrame, ackResultFrame]
|
|
1213
1275
|
);
|
|
1214
1276
|
|
|
1215
1277
|
/** Serial per-mockup composite pump (latest-wins). */
|
|
@@ -1306,6 +1368,7 @@ export function RealtimeProvider({
|
|
|
1306
1368
|
baseThrottleMs?: number
|
|
1307
1369
|
) => {
|
|
1308
1370
|
blobQueuedGenRef.current = pipelineGenRef.current;
|
|
1371
|
+
exportPendingSinceRef.current = 0;
|
|
1309
1372
|
const result = sendCanvasBlobRaw(
|
|
1310
1373
|
placement,
|
|
1311
1374
|
blob,
|
|
@@ -1334,6 +1397,7 @@ export function RealtimeProvider({
|
|
|
1334
1397
|
baseThrottleMs?: number
|
|
1335
1398
|
) => {
|
|
1336
1399
|
blobQueuedGenRef.current = pipelineGenRef.current;
|
|
1400
|
+
exportPendingSinceRef.current = 0;
|
|
1337
1401
|
const result = sendCanvasStateRaw(
|
|
1338
1402
|
placement,
|
|
1339
1403
|
state,
|
|
@@ -1425,6 +1489,12 @@ export function RealtimeProvider({
|
|
|
1425
1489
|
const isFirstSend = !configSentRef.current;
|
|
1426
1490
|
const variantChanged = lastSentVariantIdRef.current !== null &&
|
|
1427
1491
|
lastSentVariantIdRef.current !== variantId;
|
|
1492
|
+
// The ONE size picture the gallery shows (the SELECTED size's, not the preview variant's): the session renders it
|
|
1493
|
+
// too. The box renders each mockup at the variant it lists (rendercenter rc#858), so a size picture renders at its
|
|
1494
|
+
// own size in a session whose variant is another size.
|
|
1495
|
+
const sizePicture = gallerySizePicture(product.mockups, resolveVariantIdUtil(productContext, undefined, undefined));
|
|
1496
|
+
const sizePictureId = sizePicture?.id ?? null;
|
|
1497
|
+
const sizePictureChanged = configSentRef.current && lastSentSizePictureRef.current !== sizePictureId;
|
|
1428
1498
|
// 3. Background changed (theme flip swaps the MockupBackground hex)
|
|
1429
1499
|
const bgChanged = configSentRef.current && lastSentBgRef.current !== mockupBg;
|
|
1430
1500
|
// 4. Spread crop geometry changed (spread-mode editor mounted/unmounted)
|
|
@@ -1432,7 +1502,7 @@ export function RealtimeProvider({
|
|
|
1432
1502
|
const spreadCropsChanged =
|
|
1433
1503
|
configSentRef.current && lastSentSpreadCropsRef.current !== spreadCropsJson;
|
|
1434
1504
|
|
|
1435
|
-
if (!isFirstSend && !variantChanged && !bgChanged && !spreadCropsChanged) {
|
|
1505
|
+
if (!isFirstSend && !variantChanged && !bgChanged && !spreadCropsChanged && !sizePictureChanged) {
|
|
1436
1506
|
return; // Nothing changed, skip
|
|
1437
1507
|
}
|
|
1438
1508
|
|
|
@@ -1444,15 +1514,22 @@ export function RealtimeProvider({
|
|
|
1444
1514
|
invalidateBackgrounds();
|
|
1445
1515
|
}
|
|
1446
1516
|
|
|
1447
|
-
// Get mockup IDs from product.mockups
|
|
1517
|
+
// Get mockup IDs from product.mockups: every ordinary picture, plus the chosen size's picture (the other sizes'
|
|
1518
|
+
// pictures are never shown, so never rendered).
|
|
1448
1519
|
const mockupIds: string[] = [];
|
|
1449
1520
|
if (product.mockups && Array.isArray(product.mockups)) {
|
|
1450
1521
|
product.mockups.forEach((mockup: any) => {
|
|
1451
|
-
if (mockup.id) {
|
|
1522
|
+
if (mockup.id && mockupRole(mockup) === "ordinary") {
|
|
1452
1523
|
mockupIds.push(mockup.id);
|
|
1453
1524
|
}
|
|
1454
1525
|
});
|
|
1455
1526
|
}
|
|
1527
|
+
if (sizePictureId) mockupIds.push(sizePictureId);
|
|
1528
|
+
// A newly configured picture has no frozen cropped-delivery background yet: send this config full-frame so it
|
|
1529
|
+
// seeds one; maybeFlipCrop re-arms cropped delivery once every configured picture has.
|
|
1530
|
+
if (croppedActiveRef.current && mockupIds.some((id) => !backgroundsRef.current.has(id))) {
|
|
1531
|
+
croppedActiveRef.current = false;
|
|
1532
|
+
}
|
|
1456
1533
|
configuredMockupIdsRef.current = mockupIds;
|
|
1457
1534
|
// The viewport policy shapes even the first config (sessionShape.ts):
|
|
1458
1535
|
// with no gallery opted in it is every picture at mockupWidth (today).
|
|
@@ -1473,6 +1550,10 @@ export function RealtimeProvider({
|
|
|
1473
1550
|
...(mockupBg ? { bg: mockupBg } : {}),
|
|
1474
1551
|
...(spreadCropsRef.current ? { spreadCrops: spreadCropsRef.current } : {}),
|
|
1475
1552
|
placementSettings: { ...placementSettingsOverridesRef.current },
|
|
1553
|
+
// Frame flow control: this provider acks every frame it is done with (drawn / superseded / not shown /
|
|
1554
|
+
// failed), so the box can coalesce instead of queueing a backlog behind Cloudflare. Dormant on boxes
|
|
1555
|
+
// that don't negotiate it.
|
|
1556
|
+
acks: true,
|
|
1476
1557
|
}
|
|
1477
1558
|
: {
|
|
1478
1559
|
productId,
|
|
@@ -1484,6 +1565,10 @@ export function RealtimeProvider({
|
|
|
1484
1565
|
...(mockupBg ? { bg: mockupBg } : {}),
|
|
1485
1566
|
...(spreadCropsRef.current ? { spreadCrops: spreadCropsRef.current } : {}),
|
|
1486
1567
|
placementSettings: { ...placementSettingsOverridesRef.current },
|
|
1568
|
+
// Frame flow control: this provider acks every frame it is done with (drawn / superseded / not shown /
|
|
1569
|
+
// failed), so the box can coalesce instead of queueing a backlog behind Cloudflare. Dormant on boxes
|
|
1570
|
+
// that don't negotiate it.
|
|
1571
|
+
acks: true,
|
|
1487
1572
|
};
|
|
1488
1573
|
|
|
1489
1574
|
// If variant changed, trigger shimmer on all image placements
|
|
@@ -1508,6 +1593,7 @@ export function RealtimeProvider({
|
|
|
1508
1593
|
|
|
1509
1594
|
configSentRef.current = true;
|
|
1510
1595
|
lastSentVariantIdRef.current = variantId;
|
|
1596
|
+
lastSentSizePictureRef.current = sizePictureId;
|
|
1511
1597
|
lastSentBgRef.current = mockupBg;
|
|
1512
1598
|
lastSentSpreadCropsRef.current = spreadCropsJson;
|
|
1513
1599
|
|
|
@@ -1765,6 +1851,7 @@ export function RealtimeProvider({
|
|
|
1765
1851
|
// This fires immediately when a change is detected, before the export completes
|
|
1766
1852
|
const notifyPendingExport = useCallback(
|
|
1767
1853
|
(placement: string) => {
|
|
1854
|
+
exportPendingSinceRef.current = Date.now();
|
|
1768
1855
|
// Skip if this placement is already pending — avoids spamming subscribers
|
|
1769
1856
|
// on every canvas change during drag
|
|
1770
1857
|
if (pendingPlacementsRef.current.has(placement)) {
|
|
@@ -1778,6 +1865,8 @@ export function RealtimeProvider({
|
|
|
1778
1865
|
const pendingIds: string[] = [];
|
|
1779
1866
|
if (productContext?.product?.mockups) {
|
|
1780
1867
|
productContext.product.mockups.forEach((mockup: any) => {
|
|
1868
|
+
// Only the size picture the session renders (the chosen size's) can be pending; the others never render.
|
|
1869
|
+
if (mockupRole(mockup) !== "ordinary" && !configuredMockupIdsRef.current.includes(mockup.id)) return;
|
|
1781
1870
|
if (mockup.placement === placement || !mockup.placement) {
|
|
1782
1871
|
pendingMockupIdsRef.current.add(mockup.id);
|
|
1783
1872
|
pendingIds.push(mockup.id);
|
|
@@ -1797,6 +1886,9 @@ export function RealtimeProvider({
|
|
|
1797
1886
|
// notifies cancellation subscribers so loading UI can be dismissed.
|
|
1798
1887
|
const notifyPendingExportCancelled = useCallback(
|
|
1799
1888
|
(placement: string) => {
|
|
1889
|
+
// The export turned out a no-op: it no longer holds the edit open; let it settle.
|
|
1890
|
+
exportPendingSinceRef.current = 0;
|
|
1891
|
+
markEditSettled();
|
|
1800
1892
|
// If we never marked this placement as pending, nothing to do.
|
|
1801
1893
|
if (!pendingPlacementsRef.current.has(placement)) {
|
|
1802
1894
|
return;
|
|
@@ -1821,7 +1913,7 @@ export function RealtimeProvider({
|
|
|
1821
1913
|
|
|
1822
1914
|
pendingExportCancelledSubscribersRef.current.forEach((cb) => cb(placement));
|
|
1823
1915
|
},
|
|
1824
|
-
[productContext?.product?.mockups]
|
|
1916
|
+
[productContext?.product?.mockups, markEditSettled]
|
|
1825
1917
|
);
|
|
1826
1918
|
|
|
1827
1919
|
// ============================================================================
|
|
@@ -2013,6 +2105,7 @@ export function RealtimeProvider({
|
|
|
2013
2105
|
isMockupLoading,
|
|
2014
2106
|
registerBlobUrl,
|
|
2015
2107
|
markBlobShown,
|
|
2108
|
+
frameFailed,
|
|
2016
2109
|
releaseBlobConsumer,
|
|
2017
2110
|
// WebRTC streaming
|
|
2018
2111
|
subscribeRTCTiming,
|
|
@@ -2050,6 +2143,7 @@ export function RealtimeProvider({
|
|
|
2050
2143
|
isMockupLoading,
|
|
2051
2144
|
registerBlobUrl,
|
|
2052
2145
|
markBlobShown,
|
|
2146
|
+
frameFailed,
|
|
2053
2147
|
releaseBlobConsumer,
|
|
2054
2148
|
]
|
|
2055
2149
|
);
|
|
@@ -72,7 +72,7 @@ vi.mock("@snowcone-app/sdk/react", () => ({
|
|
|
72
72
|
},
|
|
73
73
|
}));
|
|
74
74
|
|
|
75
|
-
vi.mock("@snowcone-app/sdk", () => ({
|
|
75
|
+
vi.mock("@snowcone-app/sdk", () => ({ mockupRole: (m: any) => (m?.linkedAs === "size-picture" ? "size-picture" : "ordinary"), gallerySizePicture: () => null,
|
|
76
76
|
resolveVariantId: () => "gv1",
|
|
77
77
|
resolvePreviewVariantId: (_context: unknown, variantId: string) => variantId,
|
|
78
78
|
}));
|
|
@@ -74,7 +74,7 @@ vi.mock("@snowcone-app/sdk/react", () => ({
|
|
|
74
74
|
},
|
|
75
75
|
}));
|
|
76
76
|
|
|
77
|
-
vi.mock("@snowcone-app/sdk", () => ({
|
|
77
|
+
vi.mock("@snowcone-app/sdk", () => ({ mockupRole: (m: any) => (m?.linkedAs === "size-picture" ? "size-picture" : "ordinary"), gallerySizePicture: () => null,
|
|
78
78
|
resolveVariantId: () => "gv1",
|
|
79
79
|
resolvePreviewVariantId: (_context: unknown, variantId: string) => variantId,
|
|
80
80
|
}));
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frame flow control (wire agreed with rendercenter, 2026-11-11): the provider tells the box it is done with every
|
|
3
|
+
* frame — drawn (an element's img load), not shown (no element watches the mockup, or the picture already shows that
|
|
4
|
+
* exact URL), failed (img error / composite failure) — and a mockup's skipped (coalesced) versions still settle.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it, beforeEach, vi } from "vitest";
|
|
7
|
+
import React from "react";
|
|
8
|
+
import { render, waitFor, act, cleanup } from "@testing-library/react";
|
|
9
|
+
import { RealtimeProvider, useRealtime, type RealtimeContextValue } from "../RealtimeProvider";
|
|
10
|
+
|
|
11
|
+
const h = vi.hoisted(() => ({
|
|
12
|
+
state: { results: [] as Array<Record<string, unknown>> },
|
|
13
|
+
version: 0,
|
|
14
|
+
ackFrame: vi.fn((_m: string, _s: number) => true),
|
|
15
|
+
sendConfig: vi.fn(),
|
|
16
|
+
sendCanvasState: vi.fn(() => true),
|
|
17
|
+
stable: {
|
|
18
|
+
sendCanvasBlob: vi.fn(), sendColorBlob: vi.fn(), updateMockupIds: vi.fn(), updatePlacementSettings: vi.fn(),
|
|
19
|
+
connect: vi.fn(), disconnect: vi.fn(), flushPendingBlobs: vi.fn(), hasPendingBlobs: vi.fn(() => false),
|
|
20
|
+
getLatestSentVersions: vi.fn(() => ({})), rerender: vi.fn(() => true), isFrameSuperseded: vi.fn(() => false),
|
|
21
|
+
},
|
|
22
|
+
}));
|
|
23
|
+
vi.mock("@snowcone-app/sdk/react", () => ({
|
|
24
|
+
useRealtimeMockup: () => ({
|
|
25
|
+
isConnected: true, isConfigured: true, renderError: null, acks: { window: 2, timeoutMs: 5000, timeouts: 0, badAcks: 0 },
|
|
26
|
+
mockupResults: h.state.results, sendConfig: h.sendConfig, sendCanvasState: h.sendCanvasState,
|
|
27
|
+
getLastSentVersion: () => h.version, ackFrame: h.ackFrame, ...h.stable,
|
|
28
|
+
}),
|
|
29
|
+
}));
|
|
30
|
+
vi.mock("@snowcone-app/sdk", () => ({ mockupRole: (m: any) => (m?.linkedAs === "size-picture" ? "size-picture" : "ordinary"), gallerySizePicture: () => null, resolveVariantId: () => "gv1", resolvePreviewVariantId: (_c: unknown, v: string) => v }));
|
|
31
|
+
vi.mock("../Product", () => {
|
|
32
|
+
const product = { id: "p1", placements: [{ label: "Front", type: "image" }], mockups: [{ id: "A" }, { id: "B" }] };
|
|
33
|
+
return { useProductOptional: () => ({ product, selection: {} }) };
|
|
34
|
+
});
|
|
35
|
+
vi.mock("../MockupBackground", () => ({ useMockupBackgroundOptional: () => undefined }));
|
|
36
|
+
|
|
37
|
+
let ctx: RealtimeContextValue;
|
|
38
|
+
function Capture() { ctx = useRealtime(); return null; }
|
|
39
|
+
function Harness({ v }: { v: number }) {
|
|
40
|
+
void v;
|
|
41
|
+
return <RealtimeProvider autoEnable delivery="full-frame"><Capture /></RealtimeProvider>;
|
|
42
|
+
}
|
|
43
|
+
const frame = (id: string, v: number, seq: number) =>
|
|
44
|
+
({ mockupId: id, imageUrl: `https://r/${id}-v${v}`, renderUrl: `https://r/${id}-v${v}`, imageSize: 1, placement: "Front", requestVersion: v, seq });
|
|
45
|
+
const acked = () => h.ackFrame.mock.calls.map(([m, s]) => `${m}#${s}`);
|
|
46
|
+
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
cleanup();
|
|
49
|
+
vi.clearAllMocks();
|
|
50
|
+
h.state.results = [];
|
|
51
|
+
h.version = 0;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("frame flow control (acks)", () => {
|
|
55
|
+
it("asks the box for acks in its config", async () => {
|
|
56
|
+
render(<Harness v={1} />);
|
|
57
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
58
|
+
expect(h.sendConfig.mock.calls[0][0]).toMatchObject({ acks: true });
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("acks a frame when an element DRAWS it, not before; a mockup nobody shows is acked at delivery", async () => {
|
|
62
|
+
const { rerender } = render(<Harness v={1} />);
|
|
63
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
64
|
+
const unsub = ctx.subscribeMockupResultById("A", () => {});
|
|
65
|
+
h.state.results = [frame("A", 1, 7), frame("B", 1, 8)];
|
|
66
|
+
await act(async () => rerender(<Harness v={2} />));
|
|
67
|
+
expect(acked()).toEqual(["B#8"]); // B: no element watches it
|
|
68
|
+
await act(async () => ctx.markBlobShown("A", "https://r/A-v1", "el-1"));
|
|
69
|
+
expect(acked()).toEqual(["B#8", "A#7"]);
|
|
70
|
+
unsub();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("acks a frame whose img failed to load", async () => {
|
|
74
|
+
const { rerender } = render(<Harness v={1} />);
|
|
75
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
76
|
+
const unsub = ctx.subscribeMockupResultById("A", () => {});
|
|
77
|
+
const unsubB = ctx.subscribeMockupResultById("B", () => {});
|
|
78
|
+
h.state.results = [frame("A", 1, 3), frame("B", 1, 4)];
|
|
79
|
+
await act(async () => rerender(<Harness v={2} />));
|
|
80
|
+
expect(acked()).toEqual([]);
|
|
81
|
+
await act(async () => ctx.frameFailed("https://r/A-v1"));
|
|
82
|
+
expect(acked()).toEqual(["A#3"]);
|
|
83
|
+
unsub(); unsubB();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("a version the box coalesced away still settles once every mockup has a NEWER frame", async () => {
|
|
87
|
+
const { rerender } = render(<Harness v={1} />);
|
|
88
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
89
|
+
const settled = vi.fn();
|
|
90
|
+
ctx.subscribePipelineSettled(settled);
|
|
91
|
+
await act(async () => { h.version = 1; ctx.sendCanvasState("Front", { a: 1 }); });
|
|
92
|
+
await act(async () => { h.version = 2; ctx.sendCanvasState("Front", { a: 2 }); });
|
|
93
|
+
await act(async () => { h.version = 3; ctx.sendCanvasState("Front", { a: 3 }); });
|
|
94
|
+
// Only v3 arrives (v1, v2 dropped by the box's window).
|
|
95
|
+
h.state.results = [frame("A", 3, 10), frame("B", 3, 11)];
|
|
96
|
+
await act(async () => rerender(<Harness v={2} />));
|
|
97
|
+
expect(settled).toHaveBeenCalledTimes(3); // v1, v2, v3 all answered by the v3 frames
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -28,7 +28,7 @@ vi.mock("@snowcone-app/sdk/react", () => ({
|
|
|
28
28
|
sendConfig: h.sendConfig, sendCanvasState: h.sendCanvasState, getLastSentVersion: () => h.version, ...h.stable,
|
|
29
29
|
}),
|
|
30
30
|
}));
|
|
31
|
-
vi.mock("@snowcone-app/sdk", () => ({ resolveVariantId: () => "gv1", resolvePreviewVariantId: (_c: unknown, v: string) => v }));
|
|
31
|
+
vi.mock("@snowcone-app/sdk", () => ({ mockupRole: (m: any) => (m?.linkedAs === "size-picture" ? "size-picture" : "ordinary"), gallerySizePicture: () => null, resolveVariantId: () => "gv1", resolvePreviewVariantId: (_c: unknown, v: string) => v }));
|
|
32
32
|
const IDS = ["M0", "M1", "M2", "M3", "M4", "M5"];
|
|
33
33
|
vi.mock("../Product", () => {
|
|
34
34
|
const product = { id: "p1", placements: [{ label: "Front", type: "image" }], mockups: ["M0", "M1", "M2", "M3", "M4", "M5"].map((id) => ({ id })) };
|
|
@@ -114,6 +114,42 @@ describe("viewport policy (phone gallery)", () => {
|
|
|
114
114
|
expect(shapes()).toEqual([`${IDS.join(",")}@1200`]);
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
+
it("no catch-up mid-gesture: an OLDER version settling while the edit's export is pending is not the edit stopping (slow phone link, 2026-11-11)", async () => {
|
|
118
|
+
const { rerender } = render(<Harness v={1} />);
|
|
119
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
120
|
+
await act(async () => phoneGallery());
|
|
121
|
+
await wait(150);
|
|
122
|
+
await act(async () => edit()); // v1 (the editor-open state) goes out; its frames are slow to land
|
|
123
|
+
h.sendConfig.mockClear();
|
|
124
|
+
// The drag begins: its export is announced, but the page takes 2.7 s to produce it (starved phone).
|
|
125
|
+
await act(async () => ctx.notifyPendingExport("Front"));
|
|
126
|
+
// Meanwhile v1's frames land (all of its set) and v1 settles.
|
|
127
|
+
h.state.results = IDS.map((id) => frame(id, 1));
|
|
128
|
+
await act(async () => rerender(<Harness v={2} />));
|
|
129
|
+
await wait(800);
|
|
130
|
+
expect(shapes()).toEqual([]); // no 6-picture catch-up while the gesture's export is on its way
|
|
131
|
+
await act(async () => edit()); // v2: the drag's state finally goes out
|
|
132
|
+
h.state.results = [frame("M0", 2), frame("M1", 2)];
|
|
133
|
+
await act(async () => rerender(<Harness v={3} />));
|
|
134
|
+
await wait(800);
|
|
135
|
+
expect(shapes()).toEqual([`${IDS.join(",")}@1200`]); // the edit's own frames landed: now catch up
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("an export that turns out a no-op releases the edit: the catch-up still happens", async () => {
|
|
139
|
+
const { rerender } = render(<Harness v={1} />);
|
|
140
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
141
|
+
await act(async () => phoneGallery());
|
|
142
|
+
await wait(150);
|
|
143
|
+
await act(async () => edit());
|
|
144
|
+
h.sendConfig.mockClear();
|
|
145
|
+
await act(async () => ctx.notifyPendingExport("Front"));
|
|
146
|
+
h.state.results = IDS.map((id) => frame(id, 1));
|
|
147
|
+
await act(async () => rerender(<Harness v={2} />));
|
|
148
|
+
await act(async () => ctx.notifyPendingExportCancelled("Front"));
|
|
149
|
+
await wait(800);
|
|
150
|
+
expect(shapes()).toEqual([`${IDS.join(",")}@1200`]);
|
|
151
|
+
});
|
|
152
|
+
|
|
117
153
|
it("Done: the slide on screen at hero width, in ONE config", async () => {
|
|
118
154
|
const { rerender } = render(<Harness v={1} />);
|
|
119
155
|
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|