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

Commit

Permalink
feat: make listeners reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
osdnk authored and satya164 committed Aug 18, 2019
1 parent 1a14c22 commit 73b8d22
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions packages/stack/src/views/Stack/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Props = ViewProps & {
current: Animated.Value<number>;
layout: Layout;
direction: 'horizontal' | 'vertical';
onOpen: () => void;
onClose: () => void;
onOpen: (isFinished: boolean) => void;
onClose: (isFinished: boolean) => void;
onTransitionStart?: (props: { closing: boolean }) => void;
onGestureBegin?: () => void;
onGestureCanceled?: () => void;
Expand Down Expand Up @@ -121,6 +121,7 @@ export default class Card extends React.Component<Props> {
private nextIsVisible = new Value<Binary | -1>(UNSET);

private isClosing = new Value<Binary>(FALSE);
private isRunningAnimation = false;

private clock = new Clock();

Expand Down Expand Up @@ -186,7 +187,7 @@ export default class Card extends React.Component<Props> {
startClock(this.clock),
call([this.isVisible], ([value]: ReadonlyArray<Binary>) => {
const { onTransitionStart } = this.props;

this.isRunningAnimation = true;
onTransitionStart && onTransitionStart({ closing: !value });
}),
]),
Expand Down Expand Up @@ -225,11 +226,11 @@ export default class Card extends React.Component<Props> {
call([this.isVisible], ([value]: ReadonlyArray<Binary>) => {
const isOpen = Boolean(value);
const { onOpen, onClose } = this.props;

this.isRunningAnimation = false;
if (isOpen) {
onOpen();
onOpen(true);
} else {
onClose();
onClose(true);
}
}),
]),
Expand All @@ -245,7 +246,10 @@ export default class Card extends React.Component<Props> {
this.nextIsVisible,
cond(neq(this.nextIsVisible, UNSET), [
// Stop any running animations
cond(clockRunning(this.clock), stopClock(this.clock)),
cond(clockRunning(this.clock), [
call([], () => (this.isRunningAnimation = false)),
stopClock(this.clock),
]),
set(this.gesture, 0),
// Update the index to trigger the transition
set(this.isVisible, this.nextIsVisible),
Expand Down Expand Up @@ -301,6 +305,17 @@ export default class Card extends React.Component<Props> {
)
),
// Stop animations while we're dragging
cond(
clockRunning(this.clock),
call([this.toValue], ([target]) => {
this.isRunningAnimation = false;
if (target) {
this.props.onOpen(false);
} else {
this.props.onClose(false);
}
})
),
stopClock(this.clock),
],
[
Expand Down Expand Up @@ -363,6 +378,12 @@ export default class Card extends React.Component<Props> {
},
]);

componentWillUnmount(): void {
if (this.isRunningAnimation) {
this.props.onClose(false);
}
}

// We need to ensure that this style doesn't change unless absolutely needs to
// Changing it too often will result in huge frame drops due to detaching and attaching
// Changing it during an animations can result in unexpected results
Expand Down

0 comments on commit 73b8d22

Please sign in to comment.