mirror of
https://github.com/zadam/trilium.git
synced 2025-11-21 16:14:23 +01:00
client/refactor: extract the debouncer to a separate module
This commit is contained in:
parent
5ecd8b41e5
commit
45747183e7
@ -2,6 +2,7 @@ import "./NoteColorPickerMenuItem.css";
|
|||||||
import { useEffect, useRef, useState} from "preact/hooks";
|
import { useEffect, useRef, useState} from "preact/hooks";
|
||||||
import {ComponentChildren} from "preact";
|
import {ComponentChildren} from "preact";
|
||||||
import attributes from "../../services/attributes";
|
import attributes from "../../services/attributes";
|
||||||
|
import Debouncer from "../../utils/debouncer";
|
||||||
import FNote from "../../entities/fnote";
|
import FNote from "../../entities/fnote";
|
||||||
import froca from "../../services/froca";
|
import froca from "../../services/froca";
|
||||||
|
|
||||||
@ -107,36 +108,3 @@ function CustomColorCell(props: ColorCellProps) {
|
|||||||
</ColorCell>
|
</ColorCell>
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
|
|
||||||
type DebouncerCallback<T> = (value: T) => void;
|
|
||||||
|
|
||||||
class Debouncer<T> {
|
|
||||||
|
|
||||||
private debounceInterval: number;
|
|
||||||
private callback: DebouncerCallback<T>;
|
|
||||||
private lastValue: T | undefined;
|
|
||||||
private timeoutId: any | null = null;
|
|
||||||
|
|
||||||
constructor(debounceInterval: number, onUpdate: DebouncerCallback<T>) {
|
|
||||||
this.debounceInterval = debounceInterval;
|
|
||||||
this.callback = onUpdate;
|
|
||||||
}
|
|
||||||
|
|
||||||
updateValue(value: T) {
|
|
||||||
this.lastValue = value;
|
|
||||||
this.destroy();
|
|
||||||
this.timeoutId = setTimeout(this.reportUpdate.bind(this), this.debounceInterval);
|
|
||||||
}
|
|
||||||
|
|
||||||
destroy() {
|
|
||||||
if (this.timeoutId !== null) {
|
|
||||||
clearTimeout(this.timeoutId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private reportUpdate() {
|
|
||||||
if (this.lastValue !== undefined) {
|
|
||||||
this.callback(this.lastValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
32
apps/client/src/utils/debouncer.ts
Normal file
32
apps/client/src/utils/debouncer.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
export type DebouncerCallback<T> = (value: T) => void;
|
||||||
|
|
||||||
|
export default class Debouncer<T> {
|
||||||
|
|
||||||
|
private debounceInterval: number;
|
||||||
|
private callback: DebouncerCallback<T>;
|
||||||
|
private lastValue: T | undefined;
|
||||||
|
private timeoutId: any | null = null;
|
||||||
|
|
||||||
|
constructor(debounceInterval: number, onUpdate: DebouncerCallback<T>) {
|
||||||
|
this.debounceInterval = debounceInterval;
|
||||||
|
this.callback = onUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateValue(value: T) {
|
||||||
|
this.lastValue = value;
|
||||||
|
this.destroy();
|
||||||
|
this.timeoutId = setTimeout(this.reportUpdate.bind(this), this.debounceInterval);
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy() {
|
||||||
|
if (this.timeoutId !== null) {
|
||||||
|
clearTimeout(this.timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private reportUpdate() {
|
||||||
|
if (this.lastValue !== undefined) {
|
||||||
|
this.callback(this.lastValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user