Iterate toast count indicator more logically

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2020-05-22 14:28:01 +01:00
parent a822ab49d5
commit c464abaa49
2 changed files with 25 additions and 3 deletions

View file

@ -32,6 +32,9 @@ export interface IToast<C extends keyof JSX.IntrinsicElements | JSXElementConstr
*/
export default class ToastStore extends EventEmitter {
private toasts: IToast<any>[] = [];
// The count of toasts which have been seen & dealt with in this stack
// where the count resets when the stack of toasts clears.
private countSeen: number = 0;
static sharedInstance() {
if (!window.mx_ToastStore) window.mx_ToastStore = new ToastStore();
@ -40,6 +43,7 @@ export default class ToastStore extends EventEmitter {
reset() {
this.toasts = [];
this.countSeen = 0;
}
/**
@ -67,6 +71,12 @@ export default class ToastStore extends EventEmitter {
const length = this.toasts.length;
this.toasts = this.toasts.filter(t => t.key !== key);
if (length !== this.toasts.length) {
if (this.toasts.length === 0) {
this.countSeen = 0;
} else {
this.countSeen++;
}
this.emit('update');
}
}
@ -74,4 +84,8 @@ export default class ToastStore extends EventEmitter {
getToasts() {
return this.toasts;
}
getCountSeen() {
return this.countSeen;
}
}