Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Jul 20, 2022
1 parent e60c936 commit 2ebd989
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
12 changes: 3 additions & 9 deletions packages/polling/src/ratelimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export abstract class RateLimiter<T, U, V extends any[]>
*
* @param limit - The rate limit; defaults to 500ms.
*/
constructor(fn: RateLimiter.Function<T, V>, limit = 500) {
constructor(fn: (...args: V) => T | Promise<T>, 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'
});
Expand Down Expand Up @@ -103,12 +103,6 @@ export abstract class RateLimiter<T, U, V extends any[]>
protected poll: Poll<T, U, 'invoked'>;
}

export namespace RateLimiter {
export type Function<T, V extends any[]> =
| ((...args: V) => T | Promise<T>)
| (() => T | Promise<T>);
}

/**
* Wraps and debounces a function that can be called multiple times and only
* executes the underlying function one `interval` after the last invocation.
Expand Down Expand Up @@ -161,7 +155,7 @@ export class Throttler<
* The `edge` defaults to `leading`; the `limit` defaults to `500`.
*/
constructor(
fn: RateLimiter.Function<T, V>,
fn: (...args: V) => T | Promise<T>,
options?: Throttler.IOptions | number
) {
super(fn, typeof options === 'number' ? options : options && options.limit);
Expand Down
1 change: 1 addition & 0 deletions packages/polling/tests/src/ratelimiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('Throttler', () => {
};
throttler = new Throttler<void, any, [number]>(fn);
expect(sum).to.equal(0);
void throttler.invoke();
await throttler.invoke(1);
expect(sum).to.equal(1);
void throttler.invoke(10);
Expand Down

0 comments on commit 2ebd989

Please sign in to comment.