import { AnyFunction } from '@codeleap/types' import uuid from 'react-native-uuid' /** * Timer maps are module-level singletons keyed by caller-supplied `ref` strings so that disparate call sites (e.g. a button handler and an effect) can share and cancel the same logical timer without passing the id through props. If no `ref` is provided a UUID is generated, making each call site independent — callers must capture and reuse the returned ref to cancel or coalesce subsequent calls. */ const throttleTimerId = {} export function throttle(func: AnyFunction, ref?: string, delay = 200) { if (!ref) { ref = uuid.v4() as string } if (throttleTimerId[ref]) { return } throttleTimerId[ref] = setTimeout(function () { func() throttleTimerId[ref] = undefined }, delay) return ref } const debounceTimerId = {} export function debounce(func: AnyFunction, ref?: string, delay = 200) { if (!ref) { ref = uuid.v4() as string } if (debounceTimerId[ref]) { clearTimeout(debounceTimerId[ref]) } debounceTimerId[ref] = setTimeout(function () { func() debounceTimerId[ref] = undefined }, delay) return ref }