Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
working w/ memoized values
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Smith authored and Andrew Smith committed Oct 18, 2019
1 parent f9519a2 commit 7ee2856
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 52 deletions.
4 changes: 2 additions & 2 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ const App = () => {
<View style={{flex: 1, backgroundColor: 'white'}}>
<Pager
pageInterpolation={stackConfig}
clamp={{prev: 0.4, next: 1}}
clampDrag={{next: 0}}
// clamp={{prev: 0.4, next: 1}}
// clampDrag={{next: 0}}
activeIndex={activeIndex}
onChange={onChange}>
<Slide index={0} />
Expand Down
108 changes: 58 additions & 50 deletions src/pager-2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ const {
max,
min,
greaterThan,
greaterOrEq,
lessOrEq,
and,
abs,
lessThan,
ceil,
Expand Down Expand Up @@ -128,27 +131,30 @@ const minMax = proc((value, minimum, maximum) =>
);

function Pager({
children,
panProps,
style,
type = 'horizontal',
containerStyle,
adjacentChildOffset,
activeIndex: parentActiveIndex,
onChange: parentOnChange,
initialIndex,
threshold = 0.1,
initialIndex = 0,
children,
springConfig,
clampDrag = {
next: REALLY_BIG_NUMBER,
prev: REALLY_BIG_NUMBER,
},
panProps = {},
pageSize = 1,
threshold = 0.1,
minIndex = 0,
maxIndex: parentMax,
adjacentChildOffset = 10,
style,
containerStyle,
type = 'horizontal',
pageInterpolation,
clamp = {
prev: REALLY_BIG_NUMBER,
next: REALLY_BIG_NUMBER,
},
clampDrag = {
prev: REALLY_BIG_NUMBER,
next: REALLY_BIG_NUMBER,
},

pageInterpolation,
animatedIndex: parentAnimatedIndex,
}: iPager) {
const isControlled = parentActiveIndex !== undefined;

Expand All @@ -158,6 +164,13 @@ function Pager({
? (parentActiveIndex as number)
: (_activeIndex as number);

const numberOfScreens = Children.count(children);

const maxIndex =
parentMax === undefined
? Math.ceil((numberOfScreens - 1) / pageSize)
: parentMax;

const onChange = isControlled ? (parentOnChange as any) : (_onChange as any);

const dragX = memoize(new Value(0));
Expand Down Expand Up @@ -193,7 +206,6 @@ function Pager({
)
);

const numberOfScreens = Children.count(children);
const [width, setWidth] = useState(-1);
const [height, setHeight] = useState(-1);

Expand All @@ -220,25 +232,39 @@ function Pager({
[width, height]
);

const dragStart = memoize(new Value(0));
const swiping = memoize(new Value(0));
const position = memoize(new Value(activeIndex));
const nextIndex = memoize(new Value(activeIndex));
const change = memoize(new Value(0));
const absChange = memoize(new Value(0));
const indexChange = memoize(new Value(0));
const clamped = memoize(new Value(0));
const clock = memoize(new Clock());
const shouldTransition = memoize(new Value(0));

// props that might change over time should be reactive:
const animatedThreshold = useAnimatedValue(threshold);
const animatedActiveIndex = useAnimatedValue(activeIndex);
const clampDragPrev = useAnimatedValue(clampDrag.prev, REALLY_BIG_NUMBER);
const clampDragNext = useAnimatedValue(clampDrag.next, REALLY_BIG_NUMBER);
const animatedMaxIndex = useAnimatedValue(maxIndex);
const animatedMinIndex = useAnimatedValue(minIndex);

const dragStart = memoize(new Value(0));
const swiping = memoize(new Value(0));
const position = memoize(parentAnimatedIndex || new Value(activeIndex));
const nextIndex = memoize(new Value(activeIndex));

const change = memoize(sub(animatedActiveIndex, position));
const absChange = memoize(abs(change));
const shouldTransition = memoize(
and(
greaterThan(absChange, animatedThreshold),
lessOrEq(position, animatedMaxIndex),
greaterOrEq(position, animatedMinIndex)
)
);
const clamped = memoize(
minMax(divide(delta, dimension), multiply(clampDragNext, -1), clampDragPrev)
);

const indexChange = memoize(new Value(0));
const clock = memoize(new Clock());

useEffect(() => {
nextIndex.setValue(activeIndex);
if (activeIndex >= minIndex && activeIndex <= maxIndex) {
nextIndex.setValue(activeIndex);
}
}, [activeIndex]);

const animatedIndex = memoize(
Expand All @@ -249,30 +275,11 @@ function Pager({
cond(clockRunning(clock), stopClock(clock)),
cond(swiping, 0, [set(dragStart, position), set(swiping, 1)]),

set(change, sub(animatedActiveIndex, position)),
set(absChange, abs(change)),
set(shouldTransition, greaterThan(absChange, animatedThreshold)),

set(
clamped,
minMax(
divide(delta, dimension),
multiply(clampDragNext, -1),
clampDragPrev
)
),

debug('clamped', clamped),
debug('clampDragNext', clampDragNext),
debug('clampDragPrev', clampDragPrev),
debug('change', divide(delta, dimension)),

set(position, sub(dragStart, clamped)),
],
[
cond(swiping, [
set(swiping, 0),
set(nextIndex, animatedActiveIndex),
cond(shouldTransition, [
set(indexChange, ceil(absChange)),
set(
Expand Down Expand Up @@ -305,7 +312,10 @@ function Pager({
multiply(add(animatedIndex, clampNextValue), dimension)
);

const containerTranslation = memoize(multiply(animatedIndex, dimension, -1));
const animatedPageSize = useAnimatedValue(pageSize);
const containerTranslation = memoize(
multiply(animatedIndex, dimension, animatedPageSize, -1)
);

const adjacentChildren =
adjacentChildOffset !== undefined
Expand Down Expand Up @@ -408,9 +418,7 @@ function Page({
const offset = memoize(sub(index, animatedIndex));

// apply interpolation configs to <Page />
const interpolatedStyles = memoize(
interpolateWithConfig(offset, pageInterpolation)
);
const interpolatedStyles = interpolateWithConfig(offset, pageInterpolation);

// take out zIndex here as it needs to be applied to siblings, which inner containers
// are not, however the outer container requires the absolute translateX/Y to properly
Expand Down

0 comments on commit 7ee2856

Please sign in to comment.