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 e1782ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 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
15 changes: 7 additions & 8 deletions packages/polling/src/ratelimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,29 +164,28 @@ 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;
if (typeof options !== 'number' && options && options.edge === 'trailing') {
this._trailing = true;
}
this._interval = this._edge === 'trailing' ? this.limit : Poll.IMMEDIATE;
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') {
const idle = this.poll.state.phase !== 'invoked';
if (idle || this._trailing) {
this.args = args;
}
if (this.poll.state.phase !== 'invoked') {
this.args = args;
if (idle) {
void this.poll.schedule({ interval: this._interval, phase: 'invoked' });
}
return this.payload!.promise;
}

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

/**
Expand Down

0 comments on commit e1782ff

Please sign in to comment.