Refactor code to comply with ESLint rules
Major refactoring using ESLint with rules from AirBnb and Vue. Enable most of the ESLint rules and do necessary linting in the code. Also add more information for rules that are disabled to describe what they are and why they are disabled. Allow logging (`console.log`) in test files, and in development mode (e.g. when working with `npm run serve`), but disable it when environment is production (as pre-configured by Vue). Also add flag (`--mode production`) in `lint:eslint` command so production linting is executed earlier in lifecycle. Disable rules that requires a separate work. Such as ESLint rules that are broken in TypeScript: no-useless-constructor (eslint/eslint#14118) and no-shadow (eslint/eslint#13014).
This commit is contained in:
@@ -1,52 +1,62 @@
|
||||
export type CallbackType = (..._: any[]) => void;
|
||||
export type CallbackType = (..._: unknown[]) => void;
|
||||
|
||||
export function throttle(
|
||||
callback: CallbackType, waitInMs: number,
|
||||
timer: ITimer = NodeTimer): CallbackType {
|
||||
const throttler = new Throttler(timer, waitInMs, callback);
|
||||
return (...args: any[]) => throttler.invoke(...args);
|
||||
callback: CallbackType,
|
||||
waitInMs: number,
|
||||
timer: ITimer = NodeTimer,
|
||||
): CallbackType {
|
||||
const throttler = new Throttler(timer, waitInMs, callback);
|
||||
return (...args: unknown[]) => throttler.invoke(...args);
|
||||
}
|
||||
|
||||
// Allows aligning with both NodeJs (NodeJs.Timeout) and Window type (number)
|
||||
export type TimeoutType = ReturnType<typeof setTimeout>;
|
||||
|
||||
export interface ITimer {
|
||||
setTimeout: (callback: () => void, ms: number) => ReturnType<typeof setTimeout>;
|
||||
clearTimeout: (timeoutId: ReturnType<typeof setTimeout>) => void;
|
||||
dateNow(): number;
|
||||
setTimeout: (callback: () => void, ms: number) => TimeoutType;
|
||||
clearTimeout: (timeoutId: TimeoutType) => void;
|
||||
dateNow(): number;
|
||||
}
|
||||
|
||||
const NodeTimer: ITimer = {
|
||||
setTimeout: (callback, ms) => setTimeout(callback, ms),
|
||||
clearTimeout: (timeoutId) => clearTimeout(timeoutId),
|
||||
dateNow: () => Date.now(),
|
||||
setTimeout: (callback, ms) => setTimeout(callback, ms),
|
||||
clearTimeout: (timeoutId) => clearTimeout(timeoutId),
|
||||
dateNow: () => Date.now(),
|
||||
};
|
||||
|
||||
interface IThrottler {
|
||||
invoke: CallbackType;
|
||||
invoke: CallbackType;
|
||||
}
|
||||
|
||||
class Throttler implements IThrottler {
|
||||
private queuedToRun: ReturnType<typeof setTimeout>;
|
||||
private previouslyRun: number;
|
||||
constructor(
|
||||
private readonly timer: ITimer,
|
||||
private readonly waitInMs: number,
|
||||
private readonly callback: CallbackType) {
|
||||
if (!timer) { throw new Error('undefined timer'); }
|
||||
if (!waitInMs) { throw new Error('no delay to throttle'); }
|
||||
if (waitInMs < 0) { throw new Error('negative delay'); }
|
||||
if (!callback) { throw new Error('undefined callback'); }
|
||||
private queuedExecutionId: TimeoutType;
|
||||
|
||||
private previouslyRun: number;
|
||||
|
||||
constructor(
|
||||
private readonly timer: ITimer,
|
||||
private readonly waitInMs: number,
|
||||
private readonly callback: CallbackType,
|
||||
) {
|
||||
if (!timer) { throw new Error('undefined timer'); }
|
||||
if (!waitInMs) { throw new Error('no delay to throttle'); }
|
||||
if (waitInMs < 0) { throw new Error('negative delay'); }
|
||||
if (!callback) { throw new Error('undefined callback'); }
|
||||
}
|
||||
|
||||
public invoke(...args: unknown[]): void {
|
||||
const now = this.timer.dateNow();
|
||||
if (this.queuedExecutionId !== undefined) {
|
||||
this.timer.clearTimeout(this.queuedExecutionId);
|
||||
this.queuedExecutionId = undefined;
|
||||
}
|
||||
public invoke(...args: any[]): void {
|
||||
const now = this.timer.dateNow();
|
||||
if (this.queuedToRun) {
|
||||
this.queuedToRun = this.timer.clearTimeout(this.queuedToRun) as undefined;
|
||||
}
|
||||
if (!this.previouslyRun || (now - this.previouslyRun >= this.waitInMs)) {
|
||||
this.callback(...args);
|
||||
this.previouslyRun = now;
|
||||
} else {
|
||||
const nextCall = () => this.invoke(...args);
|
||||
const nextCallDelayInMs = this.waitInMs - (now - this.previouslyRun);
|
||||
this.queuedToRun = this.timer.setTimeout(nextCall, nextCallDelayInMs);
|
||||
}
|
||||
if (!this.previouslyRun || (now - this.previouslyRun >= this.waitInMs)) {
|
||||
this.callback(...args);
|
||||
this.previouslyRun = now;
|
||||
} else {
|
||||
const nextCall = () => this.invoke(...args);
|
||||
const nextCallDelayInMs = this.waitInMs - (now - this.previouslyRun);
|
||||
this.queuedExecutionId = this.timer.setTimeout(nextCall, nextCallDelayInMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user