:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(signals): prevent stale inbox visibility callbacks (#99667)
Mikayla Thompson6d6b6afa678b
diff --git a/products/signals/frontend/inbox/inboxSceneLogic.test.ts b/products/signals/frontend/inbox/inboxSceneLogic.test.tsindex 311d3ab44202..630884f42ab7 100644--- a/products/signals/frontend/inbox/inboxSceneLogic.test.ts+++ b/products/signals/frontend/inbox/inboxSceneLogic.test.ts@@ -411,6 +411,33 @@ describe('inboxSceneLogic routing', () => {expect(reportGet).toHaveBeenCalledTimes(1)})+ it('ignores a visibility callback captured before the scene unmounts', async () => {+ const reportGet = mockReportGet()+ const addEventListenerSpy = jest.spyOn(document, 'addEventListener')+ const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')+ try {+ mountWithRedesign(true)+ logic.actions.setSelectedReportId('report-1')+ await expectLogic(logic).toDispatchActions(['loadSelectedReportSuccess'])++ const onVisibilityChange = addEventListenerSpy.mock.calls.find(+ ([eventName]) => eventName === 'visibilitychange'+ )?.[1]+ if (typeof onVisibilityChange !== 'function') {+ throw new Error('Expected the report refresh visibility listener to be registered')+ }++ logic.unmount()+ expect(removeEventListenerSpy).toHaveBeenCalledWith('visibilitychange', onVisibilityChange)+ expect(() => onVisibilityChange(new Event('visibilitychange'))).not.toThrow()+ await expectLogic(logic).toNotHaveDispatchedActions(['loadSelectedReport'])+ expect(reportGet).toHaveBeenCalledTimes(1)+ } finally {+ addEventListenerSpy.mockRestore()+ removeEventListenerSpy.mockRestore()+ }+ })+// The lists behind the detail pane reconcile only on `reportStateChanged`, so a refresh that// lands a new status has to broadcast it. An unconditional broadcast is just as wrong: every// tab return would refresh every mounted section and the refund summary.diff --git a/products/signals/frontend/inbox/inboxSceneLogic.ts b/products/signals/frontend/inbox/inboxSceneLogic.tsindex 260629ccefc8..ea5dabdff766 100644--- a/products/signals/frontend/inbox/inboxSceneLogic.ts+++ b/products/signals/frontend/inbox/inboxSceneLogic.ts@@ -1008,9 +1008,13 @@ export const inboxSceneLogic = kea<inboxSceneLogicType>([// Same opt-out as above: the listener must be alive while hidden to see the return.cache.disposables.add(() => {+ const disposables = cache.disposablesconst onVisibilityChange = (): void => {+ if (document.visibilityState !== 'visible' || disposables.isDisposed) {+ return+ }const id = values.selectedReportId- if (document.visibilityState === 'visible' && id) {+ if (id) {actions.loadSelectedReport({ id })}}