forked from MapComplete/MapComplete
38 lines
1 KiB
TypeScript
38 lines
1 KiB
TypeScript
import { FeatureSource } from "../FeatureSource"
|
|
import { Feature } from "geojson"
|
|
import { Store, UIEventSource } from "../../UIEventSource"
|
|
|
|
export class IfVisibleFeatureSource<T extends Feature> implements FeatureSource<T> {
|
|
|
|
private readonly _features: UIEventSource<T[]> = new UIEventSource<T[]>([])
|
|
public readonly features: Store<T[]> = this._features
|
|
|
|
constructor(upstream: FeatureSource<T>, visible: Store<boolean>) {
|
|
|
|
let dirty = false
|
|
upstream.features.addCallbackAndRun(features => {
|
|
if (!visible.data) {
|
|
dirty = true
|
|
this._features.set([])
|
|
return
|
|
}
|
|
this._features.set(features)
|
|
dirty = false
|
|
})
|
|
|
|
visible.addCallbackAndRun(isVisible => {
|
|
if (isVisible && dirty) {
|
|
this._features.set(upstream.features.data)
|
|
dirty = false
|
|
}
|
|
if (!visible) {
|
|
this._features.set([])
|
|
}
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|