rusackas commented on code in PR #42602:
URL: https://github.com/apache/superset/pull/42602#discussion_r3709797127
##########
superset-frontend/packages/superset-core/src/theme/Theme.tsx:
##########
@@ -185,6 +185,24 @@ export class Theme {
newConfig.algorithm = newAlgorithm;
}
+ // Skip the update (and the notifyProviders fan-out it triggers) if the
+ // theme is already in the requested mode. Docs pages mount one
+ // dark-mode-sync bridge per live component demo (see
+ // docs/src/components/StorybookWrapper.jsx's ThemeSync), so a single
+ // toggle event calls this once per demo on the page. Without this
+ // check, every one of those calls would recompute the theme and
+ // notify every mounted provider, turning a single real toggle into
+ // O(n^2) provider notifications across n demos.
+ const currentAlgorithm = this.antdConfig.algorithm;
+ const algorithmUnchanged = Array.isArray(newConfig.algorithm)
+ ? Array.isArray(currentAlgorithm) &&
+ newConfig.algorithm.length === currentAlgorithm.length &&
+ newConfig.algorithm.every((alg, i) => alg === currentAlgorithm[i])
+ : newConfig.algorithm === currentAlgorithm;
+ if (algorithmUnchanged) {
+ return;
Review Comment:
Confirmed, fixed by comparing the algorithm arrays as sets instead of
positionally — `newConfig.algorithm.every(alg =>
currentAlgorithm.includes(alg))`. `[compact, dark]` now correctly no-ops
against `[dark, compact]`.
##########
superset-frontend/packages/superset-core/src/theme/Theme.test.tsx:
##########
@@ -206,6 +206,47 @@ test('Theme.toggleDarkMode preserves other algorithms when
toggling dark mode',
expect(serialized.algorithm).not.toContain(ThemeAlgorithm.DARK);
});
+test('Theme.toggleDarkMode is a no-op when the requested mode is already
active', () => {
+ // Pages with many live component demos (see docs/src/components/
+ // StorybookWrapper.jsx's ThemeSync) mount one dark-mode-sync bridge per
+ // demo, so a single toggle event can call toggleDarkMode once per demo
+ // with the same isDark value. Only the first of those calls should
+ // actually recompute the theme and fan out to providers.
+ const theme = Theme.fromConfig();
+ const setConfigSpy = jest.spyOn(theme, 'setConfig');
+
+ theme.toggleDarkMode(true);
+ expect(setConfigSpy).toHaveBeenCalledTimes(1);
+
+ // Repeating the same toggle should not recompute the theme again.
+ theme.toggleDarkMode(true);
+ theme.toggleDarkMode(true);
+ expect(setConfigSpy).toHaveBeenCalledTimes(1);
+
+ // Toggling to the other mode should still go through.
+ theme.toggleDarkMode(false);
+ expect(setConfigSpy).toHaveBeenCalledTimes(2);
+
+ setConfigSpy.mockRestore();
+});
+
+test('Theme.toggleDarkMode no-op check accounts for other algorithms in the
array', () => {
+ // Start already in dark mode alongside a non-mode algorithm (compact).
+ const theme = Theme.fromConfig({
+ algorithm: [
+ antdThemeImport.compactAlgorithm,
+ antdThemeImport.darkAlgorithm,
+ ],
+ });
+ const setConfigSpy = jest.spyOn(theme, 'setConfig');
+
+ // Already dark, so this should be a no-op rather than reordering the array.
+ theme.toggleDarkMode(true);
Review Comment:
Same bug, fixed — the no-op check now compares algorithm arrays as sets
rather than positionally.
##########
superset-frontend/packages/superset-core/src/theme/SupersetThemeProvider.test.tsx:
##########
@@ -0,0 +1,205 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { useLayoutEffect, type ReactNode } from 'react';
+import { render, screen, act } from '@testing-library/react';
+import { theme as antdThemeImport } from 'antd';
+import { Theme } from './Theme';
+
+// SupersetThemeProvider stores theme state via React.useState, then
+// registers a listener (in a useEffect, on mount) that calls setThemeState
+// whenever a *later* call to setConfig/toggleDarkMode runs on the same
Review Comment:
Fixed the comment — it said the listener registers via `useEffect`, but the
code above it registers via `useLayoutEffect`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]