refactor: move status functions to footer view
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user