refactor: move status functions to footer view

This commit is contained in:
Baptiste Grob
2020-09-29 12:08:19 +02:00
parent 9019dda003
commit 212a65f463
7 changed files with 155 additions and 199 deletions

View File

@@ -1,54 +1,30 @@
import { removeFromArray } from 'snjs';
import { FooterStatus } from '@/types';
type StatusCallback = (string: string) => void
type StatusCallback = (string: string) => void;
export class StatusManager {
private _message = '';
private observers: StatusCallback[] = [];
private statuses: FooterStatus[] = []
private observers: StatusCallback[] = []
replaceStatusWithString(status: FooterStatus, string: string) {
this.removeStatus(status);
return this.addStatusFromString(string);
get message(): string {
return this._message;
}
addStatusFromString(string: string) {
const status = { string };
this.statuses.push(status);
setMessage(message: string) {
this._message = message;
this.notifyObservers();
return status;
}
removeStatus(status: FooterStatus) {
removeFromArray(this.statuses, status);
this.notifyObservers();
return undefined;
}
addStatusObserver(callback: StatusCallback) {
onStatusChange(callback: StatusCallback) {
this.observers.push(callback);
return () => {
removeFromArray(this.observers, callback);
}
};
}
private notifyObservers() {
for(const observer of this.observers) {
observer(this.getStatusString());
for (const observer of this.observers) {
observer(this._message);
}
}
private getStatusString() {
let result = '';
this.statuses.forEach((status, index) => {
if(index > 0) {
result += ' ';
}
result += status.string;
});
return result;
}
}