Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

707 B
Raw Blame
import { useRefWithInit } from '@langgenius/dify-ui/use-ref-with-init'
import { useCallback } from 'react'

export const useSerialAsyncCallback = <Args extends unknown[], Result = void>(
  fn: (...args: Args) => Promise<Result> | Result,
  shouldSkip?: () => boolean,
) => {
  const queueRef = useRefWithInit<Promise<unknown>>(() => Promise.resolve())

  return useCallback(
    (...args: Args) => {
      if (shouldSkip?.()) return Promise.resolve(undefined as Result)

      const lastPromise = queueRef.current.catch(() => undefined)
      const nextPromise = lastPromise.then(() => fn(...args))
      queueRef.current = nextPromise

      return nextPromise
    },
    [fn, shouldSkip, queueRef],
  )
}