Skip to content

Commit

Permalink
feat(⏰): Interruptible loop() (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Jul 23, 2019
1 parent 8e50d2c commit 006177f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ const config = {
runTiming(clock, 0, config);
```

### `runLoop(duration: Node, easing: EasingFunction: boomerang? = false)`
### `loop({ clock: Clock, duration: Node, easing: EasingFunction: boomerang? = false, autoStart? = true })`

Returns an animated node that goes from `0` to `1` during the time set by `duration` continuously. If the `boomerang` option is set to `true`, the animation goes from `0` to `1` and then from `1` to `0` in the next cycle.

Expand Down
28 changes: 20 additions & 8 deletions src/AnimationRunners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const {
startClock,
clockRunning,
onChange,
not
not,
and
} = Animated;

export function runDecay(
Expand Down Expand Up @@ -124,12 +125,23 @@ export const runDelay = (node: Animated.Node<number>, duration: number) => {
]);
};

export const runLoop = (
clock: Animated.Clock,
duration: Animated.Adaptable<number>,
easing: Animated.EasingFunction,
boomerang: boolean = false
) => {
export interface LoopProps {
clock?: Animated.Clock;
easing?: Animated.EasingFunction;
duration?: number;
boomerang?: boolean;
autoStart?: boolean;
}

export const loop = (loopConfig: LoopProps) => {
const { clock, easing, duration, boomerang, autoStart } = {
clock: new Clock(),
easing: Easing.linear,
duration: 250,
boomerang: false,
autoStart: true,
...loopConfig
};
const state = {
finished: new Value(0),
position: new Value(0),
Expand All @@ -143,7 +155,7 @@ export const runLoop = (
};

return block([
cond(not(clockRunning(clock)), startClock(clock)),
cond(and(not(clockRunning(clock)), autoStart ? 1 : 0), startClock(clock)),
timing(clock, state, config),
cond(state.finished, [
set(state.finished, 0),
Expand Down

0 comments on commit 006177f

Please sign in to comment.