perf(settings): batch store registrations and defer font enumeration (#4058)

Settings pages, parent action proxies and template actions registered one by one, cloning the store map and notifying subscribers per item (~1,650 action and ~172 page updates per Settings open). registerActions now disposes in one update too, and the new registerSettingsPages batches page registration and disposal; both keep later per-id overrides. Disabled font pickers no longer enumerate system fonts; enabled ones defer it to idle and load at once on interaction, and the icon-font probe waits past startup.

Co-authored-by: dengliu <[email protected]>
Цей коміт міститься в:
Deng Liuтаdengliu authored and GitHub committed 2026-11-18 09:10:40 +08:00
1 джерело 0e169cc653
коміт 606a66b5dd
14 files changed
+290 -103

No files matched your search

+13 -6
Переглянути файл
@@ -100,21 +100,28 @@ it("checks argument-specific enablement before dispatch", async () => {
}
});
it("registers a batch with one store update and disposes every action", () => {
it("registers and disposes a batch with one store update each while keeping later overrides", () => {
const updates: number[] = [];
const off = useActions.subscribe((s) => updates.push(s.actions.size));
const dispose = registerActions([
{ id: "test.batch.one", title: "One", run: () => {} },
{ id: "test.batch.two", title: "Two", run: () => {} },
{ id: "test.batch.three", title: "Three", run: () => {} },
]);
const disposeOverride = registerAction({ id: "test.batch.two", title: "Overridden", run: () => {} });
try {
expect(updates).toHaveLength(1);
expect(updates).toHaveLength(2);
expect(getAction("test.batch.one")?.title).toBe("One");
expect(getAction("test.batch.two")?.title).toBe("Two");
} finally {
expect(getAction("test.batch.two")?.title).toBe("Overridden");
expect(getAction("test.batch.three")?.title).toBe("Three");
dispose();
// Disposing the 3-action batch fires only 1 store update and leaves the overridden id intact.
expect(updates).toHaveLength(3);
expect(getAction("test.batch.one")).toBeUndefined();
expect(getAction("test.batch.two")?.title).toBe("Overridden");
expect(getAction("test.batch.three")).toBeUndefined();
} finally {
disposeOverride();
off();
}
expect(getAction("test.batch.one")).toBeUndefined();
expect(getAction("test.batch.two")).toBeUndefined();
});
+21 -4
Переглянути файл
@@ -140,17 +140,34 @@ export function registerAction(action: Action): () => void {
}
export function registerActions(actions: Action[]): () => void {
const disposers = actions.map(addSource);
const owned = new Map<string, Action>();
for (const action of actions) {
sources.set(action.id, action);
owned.set(action.id, action);
}
// One state update for the batch: features register dozens of actions at start-up,
// and cloning the whole map per action made that O(n²).
if (actions.length) {
if (owned.size) {
useActions.setState((s) => {
const next = new Map(s.actions);
for (const action of actions) next.set(action.id, localize(action));
for (const [id, action] of owned) next.set(id, localize(action));
return { actions: next };
});
}
return () => disposers.forEach((d) => d());
return () => {
const removed: string[] = [];
for (const [id, action] of owned) {
if (sources.get(id) !== action) continue;
sources.delete(id);
removed.push(id);
}
if (!removed.length) return;
useActions.setState((s) => {
const next = new Map(s.actions);
for (const id of removed) next.delete(id);
return { actions: next };
});
};
}
export function getAction(id: string): Action | undefined {
+15 -3
Переглянути файл
@@ -62,10 +62,22 @@ export function loadSystemFonts(refresh = false): Promise<void> {
});
return pending;
}
export function useSystemFonts() {
export function useSystemFonts(enabled = true) {
const state = useSystemFontStore();
useEffect(() => {
void loadSystemFonts();
}, []);
if (!enabled || useSystemFontStore.getState().status !== "idle") return;
let idleId: number | undefined;
const timer = setTimeout(() => {
if (typeof requestIdleCallback === "function") {
idleId = requestIdleCallback(() => void loadSystemFonts(), { timeout: 1000 });
} else {
void loadSystemFonts();
}
}, 250);
return () => {
clearTimeout(timer);
if (idleId !== undefined && typeof cancelIdleCallback === "function") cancelIdleCallback(idleId);
};
}, [enabled]);
return state;
}