summaryrefslogtreecommitdiff
path: root/src/utils/index.ts
blob: d969cb6693ba61caeddf684293a13028323b016e (plain)
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
import { logError } from "../api";

/**
 * Utility for creating a timer that automatically clears after specified timeout
 * @param callback Function to call when timer completes
 * @param timeout Timeout in milliseconds
 * @returns Cleanup function that can be used in useEffect
 */
export const createAutoCleanupTimer = (callback: () => void, timeout: number): (() => void) => {
  const timer = setTimeout(callback, timeout);
  return () => clearTimeout(timer);
};

/**
 * Safe wrapper for async operations to handle errors consistently
 * @param operation Async operation to perform
 * @param errorContext Context string for error logging
 */
export const safeAsyncOperation = async <T,>(
  operation: () => Promise<T>, 
  errorContext: string
): Promise<T | undefined> => {
  try {
    return await operation();
  } catch (e) {
    logError(`${errorContext}: ${String(e)}`);
    console.error(e);
    return undefined;
  }
};