The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 17:00 UTC
refactor(web): the theme store and diff worker pool stop syncing the theme through effects (#25007)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 25, 2026 at 15:31 UTC
1c10563ddb33dcdb2cc87ef480da390dc171d4b5
3 files changed
+85
-81
apps/web/src/components/DiffWorkerPoolProvider.tsx
+69
-75
@@ -3,9 +3,8 @@ import { WorkerPoolManager } from "@pierre/diffs/worker";
3
import DiffsWorker from "@pierre/diffs/worker/worker.js?worker";
4
import * as Schema from "effect/Schema";
5
import { useEffect, useState, useSyncExternalStore, type ReactNode } from "react";
6
-import { useStableCallback } from "../hooks/useStableCallback";
7
-import { useRenderedAppearance } from "../hooks/useTheme";
8
-import { resolveDiffThemeName, type DiffThemeName } from "../lib/diffRendering";
6
+import { getRenderedAppearance, subscribeRenderedAppearance } from "../hooks/useTheme";
7
+import { resolveDiffThemeName } from "../lib/diffRendering";
8
import { PREFERRED_HIGHLIGHTER } from "../lib/syntaxHighlighting";
9
10
export class DiffWorkerError extends Schema.TaggedError<DiffWorkerError>()("DiffWorkerError", {
@@ -27,71 +26,66 @@ function resolveDiffWorkerPoolSize() {
26
let sharedWorkerPool:
27
| {
28
readonly pool: WorkerPoolManager;
29
+ readonly unsubscribeTheme: () => void;
30
consumers: number;
31
idleTimer: ReturnType<typeof setTimeout> | undefined;
32
}
33
| undefined;
34
35
-function acquireDiffWorkerPool(themeName: DiffThemeName, poolSize: number) {
36
- const entry = (sharedWorkerPool ??= {
37
- pool: new WorkerPoolManager(
38
- {
39
- workerFactory: () => {
40
- try {
41
- return new DiffsWorker();
42
- } catch (cause) {
43
- throw new DiffWorkerError({ operation: "create-worker", themeName, cause });
44
- }
45
- },
46
- poolSize,
47
- totalASTLRUCacheSize: 240,
48
- },
49
- {
50
- theme: themeName,
51
- preferredHighlighter: PREFERRED_HIGHLIGHTER,
52
- tokenizeMaxLineLength: 1_000,
53
- useTokenTransformer: true,
35
+function applyDiffWorkerTheme(pool: WorkerPoolManager) {
36
+ const themeName = resolveDiffThemeName(getRenderedAppearance());
37
+ let operation: DiffWorkerError["operation"] = "get-render-options";
38
+ void (async () => {
39
+ try {
40
+ const current = pool.getDiffRenderOptions();
41
+ if (current.theme === themeName) return;
42
+ operation = "set-render-options";
43
+ await pool.setRenderOptions({ ...current, theme: themeName });
44
+ } catch (cause) {
45
+ console.error(new DiffWorkerError({ operation, themeName, cause }));
46
+ }
47
+ })();
48
+}
49
+
50
+// The pool follows the rendered appearance for as long as it lives, so it never depends on a
51
+// mounted provider to carry a theme change across.
52
+function createDiffWorkerPool(poolSize: number) {
53
+ const themeName = resolveDiffThemeName(getRenderedAppearance());
54
+ const pool = new WorkerPoolManager(
55
+ {
56
+ workerFactory: () => {
57
+ try {
58
+ return new DiffsWorker();
59
+ } catch (cause) {
60
+ throw new DiffWorkerError({ operation: "create-worker", themeName, cause });
61
+ }
62
},
55
- ),
63
+ poolSize,
64
+ totalASTLRUCacheSize: 240,
65
+ },
66
+ {
67
+ theme: themeName,
68
+ preferredHighlighter: PREFERRED_HIGHLIGHTER,
69
+ tokenizeMaxLineLength: 1_000,
70
+ useTokenTransformer: true,
71
+ },
72
+ );
73
+ return {
74
+ pool,
75
+ unsubscribeTheme: subscribeRenderedAppearance(() => applyDiffWorkerTheme(pool)),
76
consumers: 0,
77
idleTimer: undefined,
58
- });
78
+ };
79
+}
80
+
81
+function acquireDiffWorkerPool(poolSize: number) {
82
+ const entry = (sharedWorkerPool ??= createDiffWorkerPool(poolSize));
83
clearTimeout(entry.idleTimer);
84
entry.idleTimer = undefined;
85
entry.consumers += 1;
86
return entry;
87
}
88
65
-function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) {
66
- const workerPool = useWorkerPool();
67
-
68
- useEffect(() => {
69
- if (!workerPool) {
70
- return;
71
- }
72
-
73
- let operation: DiffWorkerError["operation"] = "get-render-options";
74
- void (async () => {
75
- try {
76
- const current = workerPool.getDiffRenderOptions();
77
- if (current.theme === themeName) {
78
- return;
79
- }
80
-
81
- operation = "set-render-options";
82
- await workerPool.setRenderOptions({
83
- ...current,
84
- theme: themeName,
85
- });
86
- } catch (cause) {
87
- console.error(new DiffWorkerError({ operation, themeName, cause }));
88
- }
89
- })();
90
- }, [themeName, workerPool]);
91
-
92
- return null;
93
-}
94
-
89
// Plain-text views do not queue a highlight task that could retry a blank first render.
90
function DiffWorkerReady({ children }: { children?: ReactNode }) {
91
const workerPool = useWorkerPool();
@@ -125,37 +119,37 @@ function DiffWorkerReady({ children }: { children?: ReactNode }) {
119
);
120
}
121
128
-export function useDiffWorkerPool(diffThemeName: DiffThemeName) {
129
- const [workerPoolSize] = useState(resolveDiffWorkerPoolSize);
130
- const subscribe = useStableCallback((onStoreChange: () => void) => {
131
- if (typeof window === "undefined") return () => {};
132
- const entry = acquireDiffWorkerPool(diffThemeName, workerPoolSize);
133
- onStoreChange();
134
- return () => {
135
- entry.consumers -= 1;
136
- if (entry.consumers !== 0) return;
137
- entry.idleTimer = setTimeout(() => {
138
- entry.idleTimer = undefined;
139
- entry.pool.terminate();
140
- if (sharedWorkerPool === entry) sharedWorkerPool = undefined;
141
- }, DIFF_WORKER_IDLE_TTL_MS);
142
- };
143
- });
122
+// Module-level so the subscribe identity is stable: a new one would release and reacquire
123
+// the shared pool on every render.
124
+function subscribeSharedWorkerPool(onStoreChange: () => void) {
125
+ if (typeof window === "undefined") return () => {};
126
+ const entry = acquireDiffWorkerPool(resolveDiffWorkerPoolSize());
127
+ onStoreChange();
128
+ return () => {
129
+ entry.consumers -= 1;
130
+ if (entry.consumers !== 0) return;
131
+ entry.idleTimer = setTimeout(() => {
132
+ entry.idleTimer = undefined;
133
+ entry.unsubscribeTheme();
134
+ entry.pool.terminate();
135
+ if (sharedWorkerPool === entry) sharedWorkerPool = undefined;
136
+ }, DIFF_WORKER_IDLE_TTL_MS);
137
+ };
138
+}
139
+
140
+export function useDiffWorkerPool() {
141
return useSyncExternalStore(
145
- subscribe,
142
+ subscribeSharedWorkerPool,
143
() => sharedWorkerPool?.pool,
144
() => undefined,
145
);
146
}
147
148
export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) {
152
- const resolvedTheme = useRenderedAppearance();
153
- const diffThemeName = resolveDiffThemeName(resolvedTheme);
154
- const workerPool = useDiffWorkerPool(diffThemeName);
149
+ const workerPool = useDiffWorkerPool();
150
151
return (
152
<WorkerPoolContext value={workerPool}>
158
- <DiffWorkerThemeSync themeName={diffThemeName} />
153
<DiffWorkerReady>{children}</DiffWorkerReady>
154
</WorkerPoolContext>
155
);
apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+1
-1
@@ -278,7 +278,7 @@ function PullRequestCodeTab({
278
}) {
279
const resolvedTheme = useRenderedAppearance();
280
// Initialize the shared tokenizer and workers while the diff request is still pending.
281
- useDiffWorkerPool(resolveDiffThemeName(resolvedTheme));
281
+ useDiffWorkerPool();
282
const settings = useClientSettings((settings) => ({
283
diffFilesCollapsed: settings.diffFilesCollapsed,
284
diffIgnoreWhitespace: settings.diffIgnoreWhitespace,
apps/web/src/hooks/useTheme.ts
+15
-5
@@ -1,7 +1,7 @@
1
import type { DesktopBridge } from "@t3tools/contracts";
2
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
3
import * as Schema from "effect/Schema";
4
-import { useEffect, useSyncExternalStore } from "react";
4
+import { useSyncExternalStore } from "react";
5
import {
6
applyThemePalette,
7
CUSTOM_THEMES_STORAGE_KEY,
@@ -485,6 +485,20 @@ function subscribe(listener: () => void): () => void {
485
};
486
}
487
488
+/** For code outside React (worker pools) that follows the rendered appearance. */
489
+export function subscribeRenderedAppearance(listener: () => void): () => void {
490
+ const unsubscribeTheme = subscribe(listener);
491
+ const unsubscribePreview = subscribeToThemePreview(listener);
492
+ return () => {
493
+ unsubscribeTheme();
494
+ unsubscribePreview();
495
+ };
496
+}
497
+
498
+export function getRenderedAppearance(): ThemeAppearance {
499
+ return getThemePreviewAppearance() ?? getSnapshot().resolvedTheme;
500
+}
501
+
502
export function useRenderedAppearance() {
503
const { resolvedTheme } = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
504
const previewAppearance = useSyncExternalStore(
@@ -633,10 +647,6 @@ export function useTheme() {
647
return setAppearanceMode(nextMode);
648
};
649
636
- useEffect(() => {
637
- applyTheme(snapshot.theme);
638
- }, [snapshot]);
639
-
650
return {
651
theme,
652
setTheme,