rusackas commented on code in PR #40292:
URL: https://github.com/apache/superset/pull/40292#discussion_r4151870499
##########
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.tsx:
##########
@@ -99,29 +99,61 @@ export function OAuth2RedirectMessage({
const dispatch = useDispatch();
+ // `chartList` is rebuilt from `Object.keys` on every store update; keeping
+ // the listener state in a ref avoids tearing down/recreating the
+ // BroadcastChannel on each render and the race window that comes with it.
+ const latestStateRef = useRef({
+ source,
+ query,
+ chartId,
+ chartList,
+ dashboardId,
+ });
+ latestStateRef.current = {
+ source,
+ query,
+ chartId,
+ chartList,
+ dashboardId,
+ };
+
useEffect(() => {
+ // Guard against duplicate dispatches if both the BroadcastChannel and the
+ // storage fallback ever deliver the same completion.
+ let handled = false;
const handleOAuthComplete = (tabId?: string) => {
- if (tabId !== extra.tab_id) {
+ if (tabId !== extra.tab_id || handled) {
return;
}
- if (source === 'sqllab' && query) {
- dispatch(reRunQuery(query));
- } else if (source === 'explore' && chartId) {
- dispatch(triggerQuery(true, chartId));
- } else if (source === 'dashboard') {
- dispatch(onRefresh(chartList.map(Number), true, 0, dashboardId));
+ handled = true;
+ const {
+ source: src,
+ query: q,
+ chartId: cId,
+ chartList: cList,
+ dashboardId: dId,
+ } = latestStateRef.current;
+ if (src === 'sqllab' && q) {
+ dispatch(reRunQuery(q));
+ } else if (src === 'explore' && cId) {
+ dispatch(triggerQuery(true, cId));
+ } else if (src === 'dashboard') {
+ dispatch(onRefresh(cList.map(Number), true, 0, dId));
Review Comment:
Confirmed, `handled`/`lastHandledTabIdRef` are only set after a branch
actually dispatches now, so a signal that lands before state's ready falls
through and a later one can still land. This one's done.
##########
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.test.tsx:
##########
@@ -180,4 +181,78 @@ describe('OAuth2RedirectMessage Component', () => {
expect(reRunQuery).not.toHaveBeenCalled();
});
+
+ test('dispatches only once when both BroadcastChannel and storage signals
arrive', async () => {
+ render(setup());
+
+ simulateBroadcastMessage({ tabId: 'tabId' });
+ simulateStorageMessage({ tabId: 'tabId' });
+
+ await waitFor(() => {
+ expect(reRunQuery).toHaveBeenCalledTimes(1);
+ });
+ });
+
+ test('falls back to storage events when BroadcastChannel construction
throws', async () => {
+ (global as any).BroadcastChannel = jest.fn().mockImplementation(() => {
Review Comment:
Fixed in d038eb3, matches the `global as unknown as {...}` pattern already
used in RightMenu.test.tsx and chartActions.test.ts.
##########
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.test.tsx:
##########
@@ -180,4 +181,78 @@ describe('OAuth2RedirectMessage Component', () => {
expect(reRunQuery).not.toHaveBeenCalled();
});
+
+ test('dispatches only once when both BroadcastChannel and storage signals
arrive', async () => {
+ render(setup());
+
+ simulateBroadcastMessage({ tabId: 'tabId' });
+ simulateStorageMessage({ tabId: 'tabId' });
+
+ await waitFor(() => {
+ expect(reRunQuery).toHaveBeenCalledTimes(1);
+ });
+ });
+
+ test('falls back to storage events when BroadcastChannel construction
throws', async () => {
+ (global as any).BroadcastChannel = jest.fn().mockImplementation(() => {
+ throw new Error('blocked');
+ });
+
+ render(setup());
+
+ simulateStorageMessage({ tabId: 'tabId' });
+
+ await waitFor(() => {
+ expect(reRunQuery).toHaveBeenCalledWith({ sql: 'SELECT * FROM table' });
+ });
+ });
+
+ test('re-processes a later signal when the first arrived before state was
ready', async () => {
+ const initialState = {
+ sqlLab: {
+ queries: {},
+ queryEditors: [{ id: 'editor-id' }],
+ tabHistory: ['editor-id'],
+ },
+ explore: { slice: { slice_id: 123 } },
+ charts: { '1': {}, '2': {} },
+ dashboardInfo: { id: 'dashboard-id' },
+ };
+ const dynamicStore = createStore(
+ (state: any = initialState, action: any) => {
Review Comment:
Also fixed in d038eb3, typed as `DynamicStoreState`/`DynamicStoreAction`
instead.
##########
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.tsx:
##########
@@ -106,21 +106,60 @@ export function OAuth2RedirectMessage({
const dispatch = useDispatch();
const lastHandledTabIdRef = useRef<string>();
+ // `chartList` is rebuilt from `Object.keys` on every store update; keeping
+ // the listener state in a ref avoids tearing down/recreating the
+ // BroadcastChannel on each render and the race window that comes with it.
+ const latestStateRef = useRef({
+ source,
+ query,
+ chartId,
+ chartList,
+ dashboardId,
+ errorMitigationFunction,
+ });
+ latestStateRef.current = {
+ source,
+ query,
+ chartId,
+ chartList,
+ dashboardId,
+ errorMitigationFunction,
+ };
Review Comment:
Bito's reply doesn't actually engage with the point, it just restates the
original concern. Re-read the file to make sure: `latestStateRef.current` is
reassigned in the component body on every render, not inside the effect, so
there's nothing to re-subscribe to, `handleOAuthComplete` always reads whatever
the latest render produced when it fires. Closing this as a false positive.
--
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]