<commit_before>import { currentState } from '../helpers';
import { events, state } from '../shape';

const init = ( playable, options ) => {
  const { timeline, state } = playable;
  const { duration: durationDefault, keyframes } = timeline;

  const now = Date.now();

  const {
    alternate,
    delay,
    duration = durationDefault,
    easing,
    finish,
    rate,
    start,
    update,
  } = {
    ...optionDefaults,
    ...options,
  };

  let { currentProgress, currentReverse, iterationsComplete } = currentState( playable );

  let reverse = currentReverse;
  let reverseChanged = false;

  if ( typeof options.reverse !== 'undefined' ) {
    currentReverse = options.reverse;
    reverse = options.reverse;

    if ( state.animation.started ) {
      reverseChanged = options.reverse !== currentReverse;
    }
  }

  let initialProgress =
    ( reverse && !reverseChanged ) ||
    ( !reverse && reverseChanged ) ?
      1 - currentProgress : currentProgress;

  if ( typeof options.initialProgress !== 'undefined' ) {
    initialProgress = options.initialProgress;
  }

  let iterations = reverse ? initialProgress : 1 - initialProgress;

  if ( state.animation.started ) {
    iterations = state.animation.iterations - iterationsComplete;
  }

  if ( typeof options.iterations !== 'undefined' ) {
    iterations = options.iterations;
  }

  if ( typeof start === 'function' ) {
    start();
  }

  playable.state.animation = {
    alternate,
    currentProgress,
    currentReverse,
    duration: duration / rate,
    easing,
    finish,
    finished: false,
    initialProgress,
    iterations,
    iterationsComplete,
    keyframes: keyframes.map(() => ({
      finished: false,
      reverse: currentReverse,
      started: false,
    })),
    play: now + delay,
    reverse,
    start,
    started: true,
    update,
  };
};

const optionDefaults = {
  alternate: false,
  delay: 0,
  easing: 'easeInOutQuad',
  rate: 1,
};

const play = ( playable, options = {}, t = tick ) => {
  init( playable, options );
  t( playable );
};

const tick = playable => {
  state( playable );
  events( playable );
};

export { tick };
export default play;
<commit_msg>fix(play): take into account previous state of animation<commit_after>import { currentState } from '../helpers';
import { events, state } from '../shape';

const init = ( playable, options ) => {
  const { timeline, state = {}} = playable;
  const { animation = {}} = state;
  const { duration: durationDefault, keyframes } = timeline;

  const now = Date.now();

  const {
    alternate,
    delay,
    duration = durationDefault,
    easing,
    finish,
    rate,
    start,
    update,
  } = {
    ...optionDefaults,
    ...animation,
    ...options,
  };

  let { currentProgress, currentReverse, iterationsComplete } = currentState( animation );
  let reverse = currentReverse;
  let reverseChanged = false;

  if ( typeof options.reverse !== 'undefined' ) {
    currentReverse = options.reverse;
    reverse = options.reverse;

    if ( animation.started ) {
      reverseChanged = options.reverse !== currentReverse;
    }
  }

  let initialProgress =
    ( reverse && !reverseChanged ) ||
    ( !reverse && reverseChanged ) ?
      1 - currentProgress : currentProgress;

  if ( typeof options.initialProgress !== 'undefined' ) {
    initialProgress = options.initialProgress;
  }

  let iterations = reverse ? initialProgress : 1 - initialProgress;

  if ( animation.started ) {
    iterations = animation.iterations - iterationsComplete;
  }

  if ( typeof options.iterations !== 'undefined' ) {
    iterations = options.iterations;
  }

  if ( typeof start === 'function' ) {
    start();
  }

  state.animation = {
    alternate,
    currentProgress,
    currentReverse,
    duration: duration / rate,
    easing,
    finish,
    finished: false,
    initialProgress,
    iterations,
    iterationsComplete,
    keyframes: keyframes.map(() => ({
      finished: false,
      reverse: currentReverse,
      started: false,
    })),
    play: now + delay,
    reverse,
    start,
    started: true,
    update,
  };
};

const optionDefaults = {
  alternate: false,
  delay: 0,
  easing: 'easeInOutQuad',
  rate: 1,
};

const play = ( playable, options = {}, t = tick ) => {
  init( playable, options );
  t( playable );
};

const tick = playable => {
  state( playable );
  events( playable );
};

export { tick };
export default play;

{'fraction_non_alphanumeric': 0.12785698121747002, 'fraction_numerical': 0.0018103643358225842, 'mean_word_length': 2.846823324630113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '200518', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1296, 'n_words': 335}