Skip to content

Commit 89ab6cd

Browse files
perf(web): useActiveThreadRef compiles without manual memoization
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a702563 commit 89ab6cd

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3-
import { useMemo } from "react";
3+
import { useState } from "react";
4+
5+
function toThreadRef(
6+
environmentId: EnvironmentId | null,
7+
threadId: ThreadId | null,
8+
): ScopedThreadRef | null {
9+
return environmentId !== null && threadId !== null
10+
? scopeThreadRef(environmentId, threadId)
11+
: null;
12+
}
413

514
/**
615
* Takes the thread object but keys on its ids: the shell changes identity on every update
@@ -11,9 +20,11 @@ export function useActiveThreadRef(
1120
): ScopedThreadRef | null {
1221
const environmentId = thread?.environmentId ?? null;
1322
const threadId = thread?.id ?? null;
14-
return useMemo(
15-
() =>
16-
environmentId !== null && threadId !== null ? scopeThreadRef(environmentId, threadId) : null,
17-
[environmentId, threadId],
18-
);
23+
const [ref, setRef] = useState(() => toThreadRef(environmentId, threadId));
24+
if ((ref?.environmentId ?? null) !== environmentId || (ref?.threadId ?? null) !== threadId) {
25+
const next = toThreadRef(environmentId, threadId);
26+
setRef(next);
27+
return next;
28+
}
29+
return ref;
1930
}

0 commit comments

Comments
 (0)