Skip to content

Commit fa97675

Browse files
authored
feat: removing debouncing mechanism from ToastManager. (hashgraph#2524)
Removed debouncing mechanism from ToastManager. Signed-off-by: Eric Le Ponner <eric.leponner@icloud.com>
1 parent 9f817a7 commit fa97675

2 files changed

Lines changed: 5 additions & 29 deletions

File tree

front-end/src/renderer/utils/ToastManager.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ export class ToastManager {
55
private static readonly injectKey = Symbol();
66

77
private readonly toast = useToast();
8-
private readonly timers = new Map<string, number>();
9-
private readonly duplicateTimeout = 700; // If two same errors occurred during that time, second is a duplicate
8+
private readonly displayedErrors = new Set<string>();
109
private readonly maxDisplayedErrorCount = 4;
11-
private displayedErrorCount = 0;
1210

1311
//
1412
// Public
@@ -33,15 +31,15 @@ export class ToastManager {
3331
}
3432

3533
public error(message: string) {
36-
if (this.isDuplicate(message) || this.displayedErrorCount >= this.maxDisplayedErrorCount) {
34+
if (this.displayedErrors.has(message) || this.displayedErrors.size >= this.maxDisplayedErrorCount) {
3735
// We display message in console
3836
console.log('Hidden error message: "' + message + '"');
3937
} else {
40-
this.displayedErrorCount++;
38+
this.displayedErrors.add(message);
4139
this.toast.error(message, {
4240
duration: 0,
4341
onDismiss: () => {
44-
this.displayedErrorCount--;
42+
this.displayedErrors.delete(message);
4543
},
4644
});
4745
}
@@ -59,25 +57,4 @@ export class ToastManager {
5957
const defaultFactory = () => new ToastManager();
6058
return inject<ToastManager>(ToastManager.injectKey, defaultFactory, true);
6159
}
62-
63-
//
64-
// Private
65-
//
66-
67-
private isDuplicate(message: string): boolean {
68-
let result: boolean;
69-
const tid = this.timers.get(message);
70-
if (tid !== undefined) {
71-
// message is a duplicate
72-
result = true;
73-
clearTimeout(tid);
74-
} else {
75-
result = false;
76-
}
77-
const newTID = window.setTimeout(() => {
78-
this.timers.delete(message);
79-
}, this.duplicateTimeout);
80-
this.timers.set(message, newTID);
81-
return result;
82-
}
8360
}

front-end/src/tests/renderer/utils/ToastManager.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ describe('ToastManager', () => {
5353
const toastManager = new ToastManager();
5454
toastManager.error('Nice error message');
5555
expect(toastErrorSpy).toHaveBeenCalledTimes(1);
56-
vi.advanceTimersByTime(200);
5756
toastManager.error('Nice error message');
5857
expect(toastErrorSpy).toHaveBeenCalledTimes(1);
5958
vi.advanceTimersByTime(800);
6059
toastManager.error('Nice error message');
61-
expect(toastErrorSpy).toHaveBeenCalledTimes(2);
60+
expect(toastErrorSpy).toHaveBeenCalledTimes(1);
6261
});
6362
});
6463

0 commit comments

Comments
 (0)