forked from MapComplete/MapComplete
Add MapComplete
This commit is contained in:
commit
6187122294
61 changed files with 107059 additions and 0 deletions
44
UI/UIEventSource.ts
Normal file
44
UI/UIEventSource.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
export class UIEventSource<T>{
|
||||
|
||||
public data : T;
|
||||
private _callbacks = [];
|
||||
|
||||
constructor(data: T) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
|
||||
public addCallback(callback: (() => void)) {
|
||||
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 (let i in this._callbacks) {
|
||||
this._callbacks[i]();
|
||||
}
|
||||
}
|
||||
|
||||
public map<J>(f: ((T) => J)): UIEventSource<J> {
|
||||
const newSource = new UIEventSource<J>(
|
||||
f(this.data)
|
||||
);
|
||||
const self = this;
|
||||
this.addCallback(function () {
|
||||
newSource.setData(f(self.data));
|
||||
});
|
||||
|
||||
return newSource;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue