Footer TS
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
export class StatusManager {
|
||||
constructor() {
|
||||
this.statuses = [];
|
||||
this.observers = [];
|
||||
}
|
||||
|
||||
statusFromString(string) {
|
||||
return {string: string};
|
||||
}
|
||||
|
||||
replaceStatusWithString(status, string) {
|
||||
this.removeStatus(status);
|
||||
return this.addStatusFromString(string);
|
||||
}
|
||||
|
||||
addStatusFromString(string) {
|
||||
return this.addStatus(this.statusFromString(string));
|
||||
}
|
||||
|
||||
addStatus(status) {
|
||||
if(typeof status !== "object") {
|
||||
console.error("Attempting to set non-object status", status);
|
||||
return;
|
||||
}
|
||||
|
||||
this.statuses.push(status);
|
||||
this.notifyObservers();
|
||||
return status;
|
||||
}
|
||||
|
||||
removeStatus(status) {
|
||||
_.pull(this.statuses, status);
|
||||
this.notifyObservers();
|
||||
return null;
|
||||
}
|
||||
|
||||
getStatusString() {
|
||||
let result = "";
|
||||
this.statuses.forEach((status, index) => {
|
||||
if(index > 0) {
|
||||
result += " ";
|
||||
}
|
||||
result += status.string;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
notifyObservers() {
|
||||
for(const observer of this.observers) {
|
||||
observer(this.getStatusString());
|
||||
}
|
||||
}
|
||||
|
||||
addStatusObserver(callback) {
|
||||
this.observers.push(callback);
|
||||
}
|
||||
|
||||
removeStatusObserver(callback) {
|
||||
_.pull(this.statuses, callback);
|
||||
}
|
||||
}
|
||||
60
app/assets/javascripts/services/statusManager.ts
Normal file
60
app/assets/javascripts/services/statusManager.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { removeFromArray } from 'snjs';
|
||||
import { FooterStatus } from './../types';
|
||||
|
||||
type StatusCallback = (string: string) => void
|
||||
|
||||
export class StatusManager {
|
||||
|
||||
private statuses: FooterStatus[] = []
|
||||
private observers: StatusCallback[] = []
|
||||
|
||||
statusFromString(string: string) {
|
||||
return {string: string};
|
||||
}
|
||||
|
||||
replaceStatusWithString(status: FooterStatus, string: string) {
|
||||
this.removeStatus(status);
|
||||
return this.addStatusFromString(string);
|
||||
}
|
||||
|
||||
addStatusFromString(string: string) {
|
||||
return this.addStatus(this.statusFromString(string));
|
||||
}
|
||||
|
||||
addStatus(status: FooterStatus) {
|
||||
this.statuses.push(status);
|
||||
this.notifyObservers();
|
||||
return status;
|
||||
}
|
||||
|
||||
removeStatus(status: FooterStatus) {
|
||||
removeFromArray(this.statuses, status);
|
||||
this.notifyObservers();
|
||||
return undefined;
|
||||
}
|
||||
|
||||
getStatusString() {
|
||||
let result = '';
|
||||
this.statuses.forEach((status, index) => {
|
||||
if(index > 0) {
|
||||
result += ' ';
|
||||
}
|
||||
result += status.string;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
notifyObservers() {
|
||||
for(const observer of this.observers) {
|
||||
observer(this.getStatusString());
|
||||
}
|
||||
}
|
||||
|
||||
addStatusObserver(callback: StatusCallback) {
|
||||
this.observers.push(callback);
|
||||
return () => {
|
||||
removeFromArray(this.observers, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user