mapcomplete/UI/UIEventSource.ts

81 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-06-24 00:35:19 +02:00
export class UIEventSource<T>{
public data: T;
2020-06-24 00:35:19 +02:00
private _callbacks = [];
constructor(data: T) {
this.data = data;
}
2020-06-29 03:12:44 +02:00
public addCallback(callback: ((latestData : T) => void)) {
2020-06-24 00:35:19 +02:00
this._callbacks.push(callback);
return this;
}
public setData(t: T): void {
if (this.data === t) {
return;
}
this.data = t;
this.ping();
}
public ping(): void {
for (const callback of this._callbacks) {
callback(this.data);
2020-06-24 00:35:19 +02:00
}
}
2020-07-20 13:28:45 +02:00
public static flatten<X>(source: UIEventSource<UIEventSource<X>>, possibleSources: UIEventSource<any>[]): UIEventSource<X> {
const sink = new UIEventSource<X>(source.data?.data);
source.addCallback((latestData) => {
sink.setData(latestData?.data);
});
for (const possibleSource of possibleSources) {
possibleSource.addCallback(() => {
sink.setData(source.data?.data);
})
}
return sink;
}
2020-07-08 11:23:36 +02:00
public map<J>(f: ((T) => J),
2020-07-20 13:28:45 +02:00
extraSources: UIEventSource<any>[] = []): UIEventSource<J> {
2020-06-24 00:35:19 +02:00
const self = this;
2020-07-20 13:28:45 +02:00
2020-07-08 11:23:36 +02:00
const update = function () {
2020-06-24 00:35:19 +02:00
newSource.setData(f(self.data));
newSource.ping();
2020-07-08 11:23:36 +02:00
}
2020-07-20 13:28:45 +02:00
2020-07-08 11:23:36 +02:00
this.addCallback(update);
for (const extraSource of extraSources) {
extraSource.addCallback(update);
}
const newSource = new UIEventSource<J>(
f(this.data)
);
2020-07-08 11:23:36 +02:00
2020-06-24 00:35:19 +02:00
return newSource;
}
2020-07-21 00:07:04 +02:00
public syncWith(otherSource: UIEventSource<T>){
this.addCallback((latest) => otherSource.setData(latest));
const self = this;
otherSource.addCallback((latest) => self.setData(latest));
if(this.data === undefined){
this.setData(otherSource.data);
}else{
otherSource.setData(this.data);
}
}
2020-06-24 00:35:19 +02:00
}