All files / src/app/editor utils.ts

16.67% Statements 2/12
100% Branches 0/0
0% Functions 0/6
22.22% Lines 2/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                                              1x         1x  
/**
 * Repeat an action at given intervals and return a function to be called to stop the repetition.
 *
 * @param action The function called repeatedly at the given intervals.
 * @param repetitionInterval The interval for the repetitions in ms.
 * @param delay The delay before starting to repeat the action in ms.
 *
 * @returns A function to stop the repetition of the action.
 */
export function repeatAction(action: () => void, repetitionInterval: number, delay: number): () => void {
  let stopRepetition: () => void;
 
  action();
 
  const timeoutId = setTimeout(() => {
    const intervalId = setInterval(() => {
      action();
    }, repetitionInterval);
 
    stopRepetition = () => clearInterval(intervalId);
  }, delay);
 
  stopRepetition = () => clearTimeout(timeoutId);
 
  return () => stopRepetition();
}
 
/**
 * Default interval between repetitions of an action in the interface
 * (such as movement on the map) in ms.
 */
export const defaultInterval = 100;
 
/**
 * Default delay before an action is triggered in the interface in ms.
 */
export const defaultDelay = 500;