@@ -8,6 +8,7 @@ import { SHADOW_READY_BUDGETS, SHADOW_READY_TIMEOUT_MS } from "./useShadowPrevie
88import { refusedPreviewMessage } from "@hyperframes/core/studio-preview-mark" ;
99import { usePlayerStore } from "../store/playerStore" ;
1010import { isPreviewChanging } from "../previewReloading" ;
11+ import { studioWriteHeaders } from "../../utils/studioFileVersion" ;
1112import { NLEProvider , useNLEContext , type NLEContextValue } from "../../components/nle/NLEContext" ;
1213import {
1314 makeAdapterWindow ,
@@ -939,6 +940,32 @@ function stubVisibility(initial: DocumentVisibilityState) {
939940 } ;
940941}
941942
943+ describe ( "a shadow reload loading while a write lands" , ( ) => {
944+ it ( "loads the file again instead of promoting the one it asked for" , ( ) => {
945+ const { getApi, root } = renderTimelinePlayerHarness ( ) ;
946+ const liveIframe = makeLiveIframe ( ) ;
947+ act ( ( ) => {
948+ getApi ( ) . iframeRef . current = liveIframe ;
949+ getApi ( ) . onIframeLoad ( ) ;
950+ } ) ;
951+ act ( ( ) => getApi ( ) . refreshPlayer ( ) ) ;
952+ const asked = getApi ( ) . previewSlots . find ( ( slot ) => slot . role === "shadow" ) ! ;
953+ act ( ( ) => getApi ( ) . setShadowIframeNode ( makeLiveIframe ( ) ) ) ;
954+
955+ // An undo writes while that shadow is still loading.
956+ studioWriteHeaders ( ) ;
957+ act ( ( ) => {
958+ getApi ( ) . onShadowIframeLoad ( asked . gen ) ;
959+ getApi ( ) . onShadowReadyChange ( asked . gen , true ) ;
960+ } ) ;
961+
962+ expect ( getApi ( ) . iframeRef . current ) . toBe ( liveIframe ) ;
963+ const loading = getApi ( ) . previewSlots . find ( ( slot ) => slot . role === "shadow" ) ! ;
964+ expect ( loading . gen ) . not . toBe ( asked . gen ) ;
965+ unmount ( root ) ;
966+ } ) ;
967+ } ) ;
968+
942969describe ( "useTimelinePlayer scene swap" , ( ) => {
943970 function liveFilm ( swap ?: ( html : string ) => Promise < void > ) {
944971 const { adapter, win } = makeAdapterWindow ( ) ;
@@ -985,6 +1012,46 @@ describe("useTimelinePlayer scene swap", () => {
9851012 expect ( isPreviewChanging ( ) ) . toBe ( false ) ;
9861013 } ) ;
9871014
1015+ it ( "swaps the composition as it is now when a write lands while the HTML is loading" , async ( ) => {
1016+ const swap = vi . fn ( async ( ) => { } ) ;
1017+ const replies : Array < ( r : Response ) => void > = [ ] ;
1018+ const bodies = [ "<html>undone-once</html>" , "<html>undone-twice</html>" ] ;
1019+ vi . spyOn ( globalThis , "fetch" ) . mockImplementation (
1020+ ( ) => new Promise < Response > ( ( resolve ) => replies . push ( resolve ) ) ,
1021+ ) ;
1022+ const { getApi } = liveFilm ( swap ) ;
1023+
1024+ act ( ( ) => getApi ( ) . refreshPlayer ( ) ) ;
1025+ // A second undo writes while the first one's preview HTML is still loading.
1026+ studioWriteHeaders ( ) ;
1027+ replies [ 0 ] ! ( new Response ( bodies [ 0 ] ) ) ;
1028+ await settle ( ) ;
1029+
1030+ expect ( swap ) . not . toHaveBeenCalledWith ( bodies [ 0 ] , expect . anything ( ) ) ;
1031+ expect ( replies ) . toHaveLength ( 2 ) ;
1032+ replies [ 1 ] ! ( new Response ( bodies [ 1 ] ) ) ;
1033+ await settle ( ) ;
1034+ expect ( swap ) . toHaveBeenCalledWith ( bodies [ 1 ] , expect . anything ( ) ) ;
1035+ } ) ;
1036+
1037+ it ( "gives up refetching and swaps what it has when writes never stop" , async ( ) => {
1038+ const swap = vi . fn ( async ( ) => { } ) ;
1039+ let fetches = 0 ;
1040+ // The project stops moving well past the bound, so a swap that never gave up still ends.
1041+ vi . spyOn ( globalThis , "fetch" ) . mockImplementation ( async ( ) => {
1042+ fetches += 1 ;
1043+ if ( fetches < 20 ) studioWriteHeaders ( ) ;
1044+ return new Response ( "<html>vN</html>" ) ;
1045+ } ) ;
1046+ const { getApi } = liveFilm ( swap ) ;
1047+
1048+ act ( ( ) => getApi ( ) . refreshPlayer ( ) ) ;
1049+ await settle ( ) ;
1050+
1051+ expect ( fetches ) . toBe ( 6 ) ;
1052+ expect ( swap ) . toHaveBeenCalledWith ( "<html>vN</html>" , expect . anything ( ) ) ;
1053+ } ) ;
1054+
9881055 it ( "falls back to the full reload when the preview refuses the swap" , async ( ) => {
9891056 vi . spyOn ( globalThis , "fetch" ) . mockResolvedValue ( new Response ( "<html>v2</html>" ) ) ;
9901057 const { getApi } = liveFilm ( async ( ) => {
0 commit comments