Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/hooks/useSingleExecution/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default function useSingleExecution() {
useEffect(
() => () => {
transitionHandleRef.current?.cancel();
transitionHandleRef.current = null;
isExecutingRef.current = false;
setIsExecuting(false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prevent stale async completions from unlocking newer executions

When an async action has already reached the transition callback, its execution.finally() remains active. If Activity then hides the hook, this cleanup resets the guard; after the Activity becomes visible, a second async action can start, but completion of the first action will still call setIsExecuting(false), prematurely enabling the pressable and allowing a third invocation while the second action is pending. Invalidate completion callbacks across this reset or associate them with an execution generation.

Useful? React with 👍 / 👎.

},
[],
);
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/hooks/useSingleExecutionTest.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {act, renderHook} from '@testing-library/react-native';

import TransitionTracker from '@libs/Navigation/TransitionTracker';

import type {PropsWithChildren} from 'react';

import {Activity, createElement} from 'react';

type NavigationListener = (event: {data: Record<string, unknown>}) => void;
type UseSingleExecution = () => {
isExecuting: boolean;
Expand Down Expand Up @@ -99,6 +103,37 @@ describe('useSingleExecution (native)', () => {
expect(action).toHaveBeenCalledTimes(1);
});

it('resets execution when Activity hides the hook before the unlock callback runs', () => {
const action = jest.fn();
let activityMode: 'visible' | 'hidden' = 'visible';
const wrapper = ({children}: PropsWithChildren) => {
const activityProps = {mode: activityMode, children};
return createElement(Activity, activityProps);
};
const {result, rerender} = renderHook(() => useSingleExecution(), {wrapper});

act(() => {
result.current.singleExecution(action)();
});

expect(result.current.isExecuting).toBe(true);

activityMode = 'hidden';
rerender({});

activityMode = 'visible';
rerender({});
act(() => {
result.current.singleExecution(action)();
});

expect(action).toHaveBeenCalledTimes(2);

act(() => {
jest.runOnlyPendingTimers();
});
});

it('cancels the pending transition handle on unmount', () => {
const cancelSpy = jest.fn();
const runAfterTransitionsSpy = jest.spyOn(TransitionTracker, 'runAfterTransitions').mockReturnValue({cancel: cancelSpy});
Expand Down
Loading