From 2ebd989bfab803854e4199393ae36f5923059089 Mon Sep 17 00:00:00 2001 From: "Afshin T. Darian" Date: Wed, 20 Jul 2022 17:33:47 +0100 Subject: [PATCH] Address review comments --- packages/polling/src/ratelimiter.ts | 12 +++--------- packages/polling/tests/src/ratelimiter.spec.ts | 1 + 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/polling/src/ratelimiter.ts b/packages/polling/src/ratelimiter.ts index 296237270..0699c62f5 100644 --- a/packages/polling/src/ratelimiter.ts +++ b/packages/polling/src/ratelimiter.ts @@ -25,11 +25,11 @@ export abstract class RateLimiter * * @param limit - The rate limit; defaults to 500ms. */ - constructor(fn: RateLimiter.Function, limit = 500) { + constructor(fn: (...args: V) => T | Promise, limit = 500) { this.limit = limit; this.poll = new Poll({ auto: false, - factory: async () => (this.args ? fn(...this.args!) : fn()), + factory: async () => fn(...this.args!), frequency: { backoff: false, interval: Poll.NEVER, max: Poll.NEVER }, standby: 'never' }); @@ -103,12 +103,6 @@ export abstract class RateLimiter protected poll: Poll; } -export namespace RateLimiter { - export type Function = - | ((...args: V) => T | Promise) - | (() => T | Promise); -} - /** * Wraps and debounces a function that can be called multiple times and only * executes the underlying function one `interval` after the last invocation. @@ -161,7 +155,7 @@ export class Throttler< * The `edge` defaults to `leading`; the `limit` defaults to `500`. */ constructor( - fn: RateLimiter.Function, + fn: (...args: V) => T | Promise, options?: Throttler.IOptions | number ) { super(fn, typeof options === 'number' ? options : options && options.limit); diff --git a/packages/polling/tests/src/ratelimiter.spec.ts b/packages/polling/tests/src/ratelimiter.spec.ts index 34c36a54a..8434169c2 100644 --- a/packages/polling/tests/src/ratelimiter.spec.ts +++ b/packages/polling/tests/src/ratelimiter.spec.ts @@ -88,6 +88,7 @@ describe('Throttler', () => { }; throttler = new Throttler(fn); expect(sum).to.equal(0); + void throttler.invoke(); await throttler.invoke(1); expect(sum).to.equal(1); void throttler.invoke(10);