@thingts/execution - v1.0.0
    Preparing search index...

    Function throttle

    • Creates a throttler that wraps a given function fn to produce a throttled version. The throttled version ensures that when invoked repeatedly, fn is called at most once every delay milliseconds.

      Throttling works by creating a window that closes after delay milliseconds. All calls made within the throttle window return the exact same Promise, that resolves (or rejects) with the value returned (or error thrown) by invoking and awaiting the original function.

      Parameters

      • delay: DelaySpec

        The throttle delay in milliseconds, or a function that returns it. When throttle used as a method decorator, the function is passed the object instance as its first (only) parameter.

      • Optionaloptions: { sequence?: ThrottleSequence }
        • sequence: How to handle long-running executions:
          • 'serial': (default) keep the window open until the execution settles. Once it finishes, a new call can start a new window immediately. (Maximum throughput, up to the throttle delay.)
          • 'gap': keep the window open until delay milliseconds after the execution settles. Once that delay expires a new call can start a new window. (Fixed minimum delay between calls.)
          • 'concurrent': allow overlapping execution; a new call may start once the delay expires, even if the previous is still executing.

      Returns Throttler

      import { throttle } from '@thingts/execution'

      // Basic functional form:
      const updateThrottled = throttle(200, { sequence: 'gap' })(async function update(...) { ... })

      // With a throttler object
      const throttler = throttle(200)
      const onScroll = throttler(function handleScroll(...) { ... })
      const onResize = throttler(function handleResize(...) { ... })

      // As a method decorator
      class EditWindow {
      @throttle(300)
      async onScroll(...) { ... }
      }
      }

      // With dynamic delay
      class ApiClient {
      private rateLimitMs = 500

      @throttle((self: ApiClient) => return self.rateLimitMs)
      async fetchData(...) { ... }
      }