Function Factory

Higher-order functions that wrap your functions with extra behavior.

defineDebounceFn

Debounce: only the last call within delay runs.

import { defineDebounceFn } from '@cc-heart/utils'

const onInput = defineDebounceFn((v) => search(v), 300)

defineThrottleFn

Throttle: at most one call per delay window.

import { defineThrottleFn } from '@cc-heart/utils'

const onScroll = defineThrottleFn(() => update(), 200)

defineOnceFn

The wrapped function can only be executed once.

import { defineOnceFn } from '@cc-heart/utils'

const init = defineOnceFn(() => setup())
init() // runs
init() // no-op, returns cached result

defineSinglePromiseFn

Concurrent calls share a single in-flight promise.

import { defineSinglePromiseFn } from '@cc-heart/utils'

const loadUser = defineSinglePromiseFn(() => fetchUser())
await Promise.all([loadUser(), loadUser()]) // one request only

Demo

Click fast and compare raw vs debounced vs throttled call counts:

Loading...