Skip to content

Commit

Permalink
Store a boolean instead of a string to flag if trailing
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Jul 21, 2022
1 parent 6e42c14 commit 02503bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/polling/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export namespace IPoll {
*
* @typeparam V - Arguments for the underlying function. Defaults to any[].
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface IRateLimiter<T = any, U = any, V extends any[] = any[]>
extends IDisposable {
/**
Expand Down
16 changes: 9 additions & 7 deletions packages/polling/src/ratelimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,20 @@ export class Throttler<
options?: Throttler.IOptions | number
) {
super(fn, typeof options === 'number' ? options : options && options.limit);
if (typeof options !== 'number') {
options = options || {};
this._edge = 'edge' in options ? options.edge! : this._edge;
}
this._interval = this._edge === 'trailing' ? this.limit : Poll.IMMEDIATE;
this._trailing = !!(
options &&
typeof options !== 'number' &&
options.edge &&
options.edge === 'trailing'
);
this._interval = this._trailing ? this.limit : Poll.IMMEDIATE;
}

/**
* Throttles function invocations if one is currently in flight.
*/
invoke(...args: V): Promise<T> {
if (this._edge === 'trailing') {
if (this._trailing) {
this.args = args;
}
if (this.poll.state.phase !== 'invoked') {
Expand All @@ -185,8 +187,8 @@ export class Throttler<
return this.payload!.promise;
}

private _edge: 'leading' | 'trailing' = 'leading';
private _interval: number;
private _trailing: boolean;
}

/**
Expand Down

0 comments on commit 02503bc

Please sign in to comment.