-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathuseSdkSession.ts
More file actions
101 lines (93 loc) · 3.73 KB
/
Copy pathuseSdkSession.ts
File metadata and controls
101 lines (93 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { useState, useEffect } from "react";
import { openComposition } from "@hyperframes/sdk";
import { createHttpAdapter } from "@hyperframes/sdk/adapters/http";
import type { Composition } from "@hyperframes/sdk";
import { readStudioFileChangePath } from "../components/editor/manualEdits";
/**
* True when an external file-change payload targets the active composition and
* the SDK session must be re-opened to pick up the new content.
*/
export function shouldReloadSdkSession(payload: unknown, activeCompPath: string | null): boolean {
if (!activeCompPath) return false;
return readStudioFileChangePath(payload) === activeCompPath;
}
/**
* Stage 7 Step 3a — SDK session wired to the active composition.
*
* Creates an SDK Composition backed by createHttpAdapter on every
* (projectId, activeCompPath) change, disposes the old one on cleanup, and
* re-opens it when the active composition file changes on disk (code editor,
* agent, or server-side patch) so the in-memory linkedom document never goes
* stale.
*
* Opened WITHOUT a persist queue: this session is shadow-telemetry +
* selection-sync only — it reads from the server but must NEVER write back.
* Shadow dispatch ops mutate the in-memory model and are discarded on the next
* reload-on-change (the studio's own authoritative write triggers it). Routing
* authoritative writes through this session (cutover, Step 3c+) must re-add
* persist TOGETHER WITH self-write suppression — without it, the SDK's
* serialize() output races and clobbers the studio's authoritative write.
*/
export function useSdkSession(
projectId: string | null,
activeCompPath: string | null,
): Composition | null {
const [session, setSession] = useState<Composition | null>(null);
const [reloadToken, setReloadToken] = useState(0);
// ── Re-open on external change to the active composition ──
useEffect(() => {
if (!activeCompPath) return;
// Pre-existing clone of the file-change reload handler (usePreviewPersistence);
// surfaced by this PR's adjacent edits, not introduced by it.
// fallow-ignore-next-line code-duplication
const handler = (payload?: unknown) => {
if (shouldReloadSdkSession(payload, activeCompPath)) {
setReloadToken((t) => t + 1);
}
};
if (import.meta.hot) {
import.meta.hot.on("hf:file-change", handler);
return () => import.meta.hot?.off?.("hf:file-change", handler);
}
// SSE fallback for the embedded studio server.
const es = new EventSource("/api/events");
es.addEventListener("file-change", handler);
return () => es.close();
}, [activeCompPath]);
// ── Open / re-open the session ──
useEffect(() => {
if (!projectId || !activeCompPath) {
setSession(null);
return;
}
let cancelled = false;
let comp: Composition | null = null;
const adapter = createHttpAdapter({
projectFilesUrl: `/api/projects/${projectId}`,
});
adapter
.read(activeCompPath)
.then(async (content) => {
if (cancelled || typeof content !== "string") return;
// No persist — shadow/selection only; see the hook docstring. The SDK
// must not write back to the server while it shadows the authoritative
// studio path.
comp = await openComposition(content);
// Cleanup may have fired while openComposition was awaited; dispose immediately.
if (cancelled) {
comp.dispose();
return;
}
setSession(comp);
})
.catch(() => {
if (!cancelled) setSession(null);
});
return () => {
cancelled = true;
const c = comp;
if (c) void c.flush().finally(() => c.dispose());
};
}, [projectId, activeCompPath, reloadToken]);
return session;
}