index.d.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. export = limitFactory
  2. declare namespace limitFactory {}
  3. /**
  4. * Returns a function that can be used to wrap promise returning functions,
  5. * limiting them to concurrency outstanding calls.
  6. * @param concurrency the concurrency, i.e. 1 will limit calls to one at a
  7. * time, effectively in sequence or serial. 2 will allow two at a time, etc.
  8. * 0 or undefined specify no limit, and all calls will be run in parallel.
  9. */
  10. declare function limitFactory<T>(concurrency?: number): limit<T>
  11. declare type limit<T> = limitFunc<T> & limitInterface<T>
  12. declare interface limitInterface<T> {
  13. /**
  14. * Maps an array of items using mapper, but limiting the number of concurrent
  15. * calls to mapper with the concurrency of limit. If at least one call to
  16. * mapper returns a rejected promise, the result of map is a the same rejected
  17. * promise, and no further calls to mapper are made.
  18. * @param items any items
  19. * @param mapper iterator
  20. */
  21. map<U>(items: ReadonlyArray<T>, mapper: (value: T) => Promise<U>): Promise<U[]>
  22. /**
  23. * Returns the queue length, the number of jobs that are waiting to be started.
  24. * You could use this to throttle incoming jobs, so the queue doesn't
  25. * overwhealm the available memory - for e.g. pause() a stream.
  26. */
  27. queue: number
  28. }
  29. /**
  30. * A function that limits calls to fn, based on concurrency above. Returns a
  31. * promise that resolves or rejects the same value or error as fn. All functions
  32. * are executed in the same order in which they were passed to limit. fn must
  33. * return a promise.
  34. * @param fn a function that is called with no arguments and returns a promise.
  35. * You can pass arguments to your function by putting it inside another function,
  36. * i.e. `() -> myfunc(a, b, c)`.
  37. */
  38. declare type limitFunc<T> = (fn: () => Promise<T>) => Promise<T>