Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

fix: wrap reset and resetRoot inside transaction #189

Merged
merged 2 commits into from
Nov 29, 2019
Merged
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
93 changes: 50 additions & 43 deletions packages/core/src/NavigationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,53 @@ const Container = React.forwardRef(function NavigationContainer(
const isFirstMountRef = React.useRef<boolean>(true);
const skipTrackingRef = React.useRef<boolean>(false);

const reset = React.useCallback((state: NavigationState) => {
skipTrackingRef.current = true;
setNavigationState(state);
const performTransaction = React.useCallback((callback: () => void) => {
if (isTransactionActiveRef.current) {
throw new Error(
"Only one transaction can be active at a time. Did you accidentally nest 'performTransaction'?"
);
}

setNavigationState((navigationState: State) => {
isTransactionActiveRef.current = true;
transactionStateRef.current = navigationState;

callback();

isTransactionActiveRef.current = false;

return transactionStateRef.current;
});
}, []);

const getState = React.useCallback(
() =>
transactionStateRef.current !== null
? transactionStateRef.current
: navigationStateRef.current,
[]
);

const setState = React.useCallback((navigationState: State) => {
if (transactionStateRef.current === null) {
throw new Error(
"Any 'setState' calls need to be done inside 'performTransaction'"
);
}

transactionStateRef.current = navigationState;
}, []);

const reset = React.useCallback(
(state: NavigationState) => {
performTransaction(() => {
skipTrackingRef.current = true;
setState(state);
});
},
[performTransaction, setState]
);

const { trackState, trackAction } = useDevTools({
name: '@react-navigation',
reset,
Expand Down Expand Up @@ -147,10 +189,12 @@ const Container = React.forwardRef(function NavigationContainer(

const resetRoot = React.useCallback(
(state?: PartialState<NavigationState> | NavigationState) => {
trackAction('@@RESET_ROOT');
setNavigationState(state);
performTransaction(() => {
trackAction('@@RESET_ROOT');
setState(state);
});
},
[trackAction]
[performTransaction, setState, trackAction]
);

const getRootState = React.useCallback(() => {
Expand Down Expand Up @@ -185,43 +229,6 @@ const Container = React.forwardRef(function NavigationContainer(
[addFocusedListener, trackAction, addStateGetter]
);

const performTransaction = React.useCallback((callback: () => void) => {
if (isTransactionActiveRef.current) {
throw new Error(
"Only one transaction can be active at a time. Did you accidentally nest 'performTransaction'?"
);
}

setNavigationState((navigationState: State) => {
isTransactionActiveRef.current = true;
transactionStateRef.current = navigationState;

callback();

isTransactionActiveRef.current = false;

return transactionStateRef.current;
});
}, []);

const getState = React.useCallback(
() =>
transactionStateRef.current !== null
? transactionStateRef.current
: navigationStateRef.current,
[]
);

const setState = React.useCallback((navigationState: State) => {
if (transactionStateRef.current === null) {
throw new Error(
"Any 'setState' calls need to be done inside 'performTransaction'"
);
}

transactionStateRef.current = navigationState;
}, []);

const context = React.useMemo(
() => ({
state,
Expand Down