Skip to content

Commit

Permalink
Allow Transitioner to queue single animation (react-navigation#191)
Browse files Browse the repository at this point in the history
remove some extra spaces

use navigationScene type

fix index race condition
  • Loading branch information
dmhood authored and sourcecode911 committed Mar 9, 2020
1 parent b206fc4 commit f64e222
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/views/Transitioner.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class Transitioner extends React.Component<*, Props, State> {
this._prevTransitionProps = null;
this._transitionProps = buildTransitionProps(props, this.state);
this._isMounted = false;
this._isTransitionRunning = false;
this._queuedTransition = null;
}

componentWillMount(): void {
Expand All @@ -121,6 +123,16 @@ class Transitioner extends React.Component<*, Props, State> {
return;
}

const indexHasChanged = nextProps.navigation.state.index !== this.props.navigation.state.index;
if (this._isTransitionRunning) {
this._queuedTransition = { nextProps, nextScenes, indexHasChanged };
return;
}

this._startTransition(nextProps, nextScenes, indexHasChanged);
}

_startTransition(nextProps: Props, nextScenes: Array<NavigationScene>, indexHasChanged: boolean) {
const nextState = {
...this.state,
scenes: nextScenes,
Expand Down Expand Up @@ -162,7 +174,7 @@ class Transitioner extends React.Component<*, Props, State> {
),
];

if (nextProps.navigation.state.index !== this.props.navigation.state.index) {
if (indexHasChanged) {
animations.push(
timing(
position,
Expand All @@ -173,8 +185,8 @@ class Transitioner extends React.Component<*, Props, State> {
),
);
}

// update scenes and play the transition
this._isTransitionRunning = true;
this.setState(nextState, () => {
nextProps.onTransitionStart && nextProps.onTransitionStart(
this._transitionProps,
Expand Down Expand Up @@ -239,6 +251,16 @@ class Transitioner extends React.Component<*, Props, State> {
this._transitionProps,
prevTransitionProps,
);
if (this._queuedTransition) {
this._startTransition(
this._queuedTransition.nextProps,
this._queuedTransition.nextScenes,
this._queuedTransition.indexHasChanged
);
this._queuedTransition = null;
} else {
this._isTransitionRunning = false;
}
});
}
}
Expand Down

0 comments on commit f64e222

Please sign in to comment.