From 73b8d220ada1212361df9e532f8c6eda62162708 Mon Sep 17 00:00:00 2001 From: osdnk Date: Tue, 11 Jun 2019 19:56:30 +0200 Subject: [PATCH] feat: make listeners reliable --- packages/stack/src/views/Stack/Card.tsx | 35 ++++++++++++++++++++----- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/stack/src/views/Stack/Card.tsx b/packages/stack/src/views/Stack/Card.tsx index e7c3e7a6..033c5efa 100755 --- a/packages/stack/src/views/Stack/Card.tsx +++ b/packages/stack/src/views/Stack/Card.tsx @@ -18,8 +18,8 @@ type Props = ViewProps & { current: Animated.Value; 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; @@ -121,6 +121,7 @@ export default class Card extends React.Component { private nextIsVisible = new Value(UNSET); private isClosing = new Value(FALSE); + private isRunningAnimation = false; private clock = new Clock(); @@ -186,7 +187,7 @@ export default class Card extends React.Component { startClock(this.clock), call([this.isVisible], ([value]: ReadonlyArray) => { const { onTransitionStart } = this.props; - + this.isRunningAnimation = true; onTransitionStart && onTransitionStart({ closing: !value }); }), ]), @@ -225,11 +226,11 @@ export default class Card extends React.Component { call([this.isVisible], ([value]: ReadonlyArray) => { const isOpen = Boolean(value); const { onOpen, onClose } = this.props; - + this.isRunningAnimation = false; if (isOpen) { - onOpen(); + onOpen(true); } else { - onClose(); + onClose(true); } }), ]), @@ -245,7 +246,10 @@ export default class Card extends React.Component { 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), @@ -301,6 +305,17 @@ export default class Card extends React.Component { ) ), // 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), ], [ @@ -363,6 +378,12 @@ export default class Card extends React.Component { }, ]); + 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