rusackas commented on code in PR #40292:
URL: https://github.com/apache/superset/pull/40292#discussion_r4129010739
##########
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:
latestStateRef.current gets reassigned on every render (lines 120-127,
outside the effect), so handleOAuthComplete always reads whatever's current
when it fires, not what was captured at mount. That's the whole point of the
ref instead of a dep array. Don't think this one's real.
##########
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,
+ };
+
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 || tabId === lastHandledTabIdRef.current) {
+ if (
+ tabId !== extra.tab_id ||
+ tabId === lastHandledTabIdRef.current ||
+ handled
+ ) {
return;
}
-
- if (errorMitigationFunction) {
- errorMitigationFunction();
- } else if (source === 'sqllab' && query) {
- dispatch(reRunQuery(query));
- } else if (source === 'explore') {
- dispatch(triggerQuery(true, chartId));
- } else if (source === 'dashboard') {
- dispatch(onRefresh(chartList.map(Number), true, 0, dashboardId));
- } else if (source === 'crud') {
+ const {
+ source: src,
+ query: q,
+ chartId: cId,
+ chartList: cList,
+ dashboardId: dId,
+ errorMitigationFunction: mitigate,
+ } = latestStateRef.current;
+ // `handled`/`lastHandledTabIdRef` are only set below, after this whole
+ // chain, and only reached when a branch actually dispatches — so a
+ // signal that arrives before state is ready (e.g. `query` still null
+ // in SQL Lab) falls through to the catch-all `return` and a later
+ // fallback signal can still succeed.
+ if (mitigate) {
+ mitigate();
+ } else if (src === 'sqllab' && q) {
+ dispatch(reRunQuery(q));
+ } else if (src === 'explore') {
+ dispatch(triggerQuery(true, cId));
+ } else if (src === 'dashboard') {
+ dispatch(onRefresh(cList.map(Number), true, 0, dId));
Review Comment:
dashboardId can't actually be undefined here in practice. refreshChart
unconditionally reads dashboardInfo.common.conf before it does anything, so
nothing in this chain can fire before dashboardInfo's loaded. Think this one's
a false positive too.
--
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]