forked from MapComplete/MapComplete
Further stabilization of wikipedia box
This commit is contained in:
parent
393d5d8932
commit
a89d303ecd
15 changed files with 169 additions and 109 deletions
|
@ -1,4 +1,5 @@
|
|||
import {Utils} from "../Utils";
|
||||
import * as Events from "events";
|
||||
|
||||
export class UIEventSource<T> {
|
||||
|
||||
|
@ -32,14 +33,14 @@ export class UIEventSource<T> {
|
|||
return [];
|
||||
}
|
||||
|
||||
public static flatten<X>(source: UIEventSource<UIEventSource<X>>, possibleSources: UIEventSource<any>[]): UIEventSource<X> {
|
||||
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) {
|
||||
for (const possibleSource of possibleSources ?? []) {
|
||||
possibleSource?.addCallback(() => {
|
||||
sink.setData(source.data?.data);
|
||||
})
|
||||
|
@ -186,6 +187,25 @@ export class UIEventSource<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Monadic bind function
|
||||
*/
|
||||
public bind<X>(f: ((t: T) => UIEventSource<X>)): UIEventSource<X>{
|
||||
const sink = new UIEventSource<X>( undefined )
|
||||
const seenEventSources = new Set<UIEventSource<X>>();
|
||||
this.addCallbackAndRun(data => {
|
||||
const eventSource = f(data)
|
||||
if(eventSource === undefined){
|
||||
sink.setData(undefined)
|
||||
}else if(!seenEventSources.has(eventSource)){
|
||||
eventSource.addCallbackAndRun(mappedData => sink.setData(mappedData))
|
||||
seenEventSources.add(eventSource)
|
||||
}
|
||||
})
|
||||
|
||||
return sink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Monoidal map:
|
||||
* Given a function 'f', will construct a new UIEventSource where the contents will always be "f(this.data)'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue