Skip to content

Commit

Permalink
optimize throttler
Browse files Browse the repository at this point in the history
add key for IThrottler to remove IThrottler from map when it was executed to avoid memory leak.
  • Loading branch information
Sczlog authored and netweng committed Jan 14, 2022
1 parent 51a5a3d commit e44f82b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/util/throttler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IThrottler {
invoker: (() => void) | null;
timeout: number | null;
previous: number;
key: symbol;
}

export class ThrottleManager {
Expand All @@ -32,6 +33,7 @@ export class ThrottleManager {
invoker: null,
timeout: null,
previous: 0,
key,
};
manager.throttlerMap.set(key, newThrottler);
}
Expand All @@ -51,6 +53,7 @@ export class ThrottleManager {
throttler.previous = now;
func.apply(this, args);
manager.pendingFnSet.delete(throttler.invoker);
manager.throttlerMap.delete(throttler.key);
throttler.invoker = null;
} else if (!throttler.timeout && options.trailing !== false) {
const debouncedFn = () => {
Expand All @@ -59,6 +62,7 @@ export class ThrottleManager {
throttler.timeout = null;
func.apply(this, args);
manager.pendingFnSet.delete(throttler.invoker);
manager.throttlerMap.delete(throttler.key);
throttler.invoker = null;
};
throttler.timeout = window.setTimeout(debouncedFn, remaining);
Expand Down
1 change: 1 addition & 0 deletions typings/util/throttler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IThrottler {
invoker: (() => void) | null;
timeout: number | null;
previous: number;
key: symbol;
}
export declare class ThrottleManager {
private throttlerMap;
Expand Down

0 comments on commit e44f82b

Please sign in to comment.