forked from MapComplete/MapComplete
More work on refactoring the changes handling
This commit is contained in:
parent
42391b4ff1
commit
b55f9a25c6
19 changed files with 181 additions and 105 deletions
|
@ -6,7 +6,7 @@ export class UIEventSource<T> {
|
|||
public data: T;
|
||||
public trace: boolean;
|
||||
private readonly tag: string;
|
||||
private _callbacks = [];
|
||||
private _callbacks: ((t: T) => (boolean | void | any)) [] = [];
|
||||
|
||||
constructor(data: T, tag: string = "") {
|
||||
this.tag = tag;
|
||||
|
@ -31,7 +31,7 @@ export class UIEventSource<T> {
|
|||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public static flatten<X>(source: UIEventSource<UIEventSource<X>>, possibleSources: UIEventSource<any>[]): UIEventSource<X> {
|
||||
const sink = new UIEventSource<X>(source.data?.data);
|
||||
|
||||
|
@ -63,7 +63,13 @@ export class UIEventSource<T> {
|
|||
|
||||
}
|
||||
|
||||
public addCallback(callback: ((latestData: T) => void)): UIEventSource<T> {
|
||||
/**
|
||||
* Adds a callback
|
||||
*
|
||||
* If the result of the callback is 'true', the callback is considered finished and will be removed again
|
||||
* @param callback
|
||||
*/
|
||||
public addCallback(callback: ((latestData: T) => (boolean | void | any))): UIEventSource<T> {
|
||||
if (callback === console.log) {
|
||||
// This ^^^ actually works!
|
||||
throw "Don't add console.log directly as a callback - you'll won't be able to find it afterwards. Wrap it in a lambda instead."
|
||||
|
@ -90,8 +96,21 @@ export class UIEventSource<T> {
|
|||
}
|
||||
|
||||
public ping(): void {
|
||||
let toDelete = undefined
|
||||
for (const callback of this._callbacks) {
|
||||
callback(this.data);
|
||||
if (callback(this.data) === true) {
|
||||
// This callback wants to be deleted
|
||||
if (toDelete === undefined) {
|
||||
toDelete = [callback]
|
||||
} else {
|
||||
toDelete.push(callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (toDelete !== undefined) {
|
||||
for (const toDeleteElement of toDelete) {
|
||||
this._callbacks.splice(this._callbacks.indexOf(toDeleteElement), 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue