Merge branch 'pietervdvn:develop' into hackerspace-theme

This commit is contained in:
Thibault Molleman 2022-06-08 17:43:39 +02:00 committed by GitHub
commit 1c97efdff3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 21 deletions

View file

@ -305,7 +305,8 @@ export class ImmutableStore<T> extends Store<T> {
*/
class ListenerTracker<T> {
private readonly _callbacks: ((t: T) => (boolean | void | any)) [] = [];
public pingCount = 0;
/**
* Adds a callback which can be called; a function to unregister is returned
*/
@ -330,6 +331,7 @@ class ListenerTracker<T> {
* Returns the number of registered callbacks
*/
public ping(data: T): number {
this.pingCount ++;
let toDelete = undefined
let startTime = new Date().getTime() / 1000;
for (const callback of this._callbacks) {
@ -368,7 +370,10 @@ class ListenerTracker<T> {
class MappedStore<TIn, T> extends Store<T> {
private _upstream: Store<TIn>;
private _upstreamCallbackHandler: ListenerTracker<TIn>;
private _upstreamPingCount: number = -1;
private _unregisterFromUpstream: (() => void)
private _f: (t: TIn) => T;
private readonly _extraStores: Store<any>[] | undefined;
private _unregisterFromExtraStores: (() => void)[] | undefined
@ -378,18 +383,39 @@ class MappedStore<TIn, T> extends Store<T> {
private static readonly pass: () => {}
constructor(upstream: Store<TIn>, f: (t: TIn) => T, extraStores: Store<any>[] = undefined, initialData : T= undefined) {
constructor(upstream: Store<TIn>, f: (t: TIn) => T, extraStores: Store<any>[] = undefined,
upstreamListenerHandler: ListenerTracker<TIn>) {
super();
this._upstream = upstream;
this._upstreamCallbackHandler = upstreamListenerHandler
this._f = f;
this._data = initialData ?? f(upstream.data)
this._data = f(upstream.data)
this._upstreamPingCount = upstreamListenerHandler.pingCount
this._extraStores = extraStores;
this.registerCallbacksToUpstream()
}
private _data: T;
private _callbacksAreRegistered = false
get data(): T {
/**
* Gets the current data from the store
*
* const src = new UIEventSource(21)
* const mapped = src.map(i => i * 2)
* src.setData(3)
* mapped.data // => 6
*
*/
get data(): T {
if (!this._callbacksAreRegistered) {
// Callbacks are not registered, so we haven't been listening for updates from the upstream which might have changed
if(this._upstreamCallbackHandler.pingCount != this._upstreamPingCount){
// Upstream has pinged - let's update our data first
this._data = this._f(this._upstream.data)
}
return this._data
}
return this._data
}
@ -413,7 +439,7 @@ class MappedStore<TIn, T> extends Store<T> {
this._upstream,
data => f(this._f(data)),
stores,
f(this._data)
this._upstreamCallbackHandler
);
}
@ -423,9 +449,22 @@ class MappedStore<TIn, T> extends Store<T> {
this._unregisterFromUpstream()
this._unregisterFromExtraStores?.forEach(unr => unr())
}
private registerCallbacksToUpstream() {
const self = this
this._unregisterFromUpstream = this._upstream.addCallback(
_ => self.update()
)
this._unregisterFromExtraStores = this._extraStores?.map(store =>
store?.addCallback(_ => self.update())
)
this._callbacksAreRegistered = true;
}
private update(): void {
const newData = this._f(this._upstream.data)
this._upstreamPingCount = this._upstreamCallbackHandler.pingCount
if (this._data == newData) {
return;
}
@ -435,16 +474,9 @@ class MappedStore<TIn, T> extends Store<T> {
addCallback(callback: (data: T) => (any | boolean | void)): (() => void) {
if (!this._callbacksAreRegistered) {
const self = this
// This is the first callback that is added
// We register this 'map' to the upstream object and all the streams
this._unregisterFromUpstream = this._upstream.addCallback(
_ => self.update()
)
this._unregisterFromExtraStores = this._extraStores?.map(store =>
store?.addCallback(_ => self.update())
)
this._callbacksAreRegistered = true;
this.registerCallbacksToUpstream()
}
const unregister = this._callbacks.addCallback(callback)
return () => {
@ -487,7 +519,7 @@ class MappedStore<TIn, T> extends Store<T> {
export class UIEventSource<T> extends Store<T> {
public data: T;
private _callbacks: ListenerTracker<T> = new ListenerTracker<T>()
_callbacks: ListenerTracker<T> = new ListenerTracker<T>()
private static readonly pass: () => {}
@ -637,7 +669,7 @@ export class UIEventSource<T> extends Store<T> {
*/
public map<J>(f: ((t: T) => J),
extraSources: Store<any>[] = []): Store<J> {
return new MappedStore(this, f, extraSources);
return new MappedStore(this, f, extraSources, this._callbacks);
}
/**

View file

@ -58,58 +58,99 @@
"Q22698"
],
"removePrefixes": [
"allée de",
"allée du",
"allée",
"autoroute de",
"autoroute du",
"autoroute",
"avenue de",
"avenue du",
"avenue",
"bibliothèque de",
"bibliothèque du",
"bibliothèque",
"boulevard de",
"boulevard du",
"boulevard",
"centre du",
"centre",
"centre culturel de",
"centre culturel du",
"centre culturel",
"centre de",
"centre du",
"centre",
"chaussée de",
"chaussée du",
"chaussée",
"chemin de",
"chemin du",
"chemin",
"collège de",
"collège du",
"collège",
"complexe sportif de",
"complexe sportif du",
"complexe sportif",
"école du",
"école",
"école élémentaire de",
"école élémentaire du",
"école élémentaire",
"école maternelle de",
"école maternelle du",
"école maternelle",
"école primaire de",
"école primaire du",
"école primaire",
"école de",
"école du",
"école",
"esplanade de",
"esplanade du",
"esplanade",
"groupe scolaire de",
"groupe scolaire du",
"groupe scolaire",
"gymnase de",
"gymnase du",
"gymnase",
"impasse de",
"impasse du",
"impasse",
"lycée de",
"lycée du",
"lycée",
"mail de",
"mail du",
"mail",
"médiathèque de",
"médiathèque du",
"médiathèque",
"musée de",
"musée du",
"musée",
"parc de",
"parc du",
"parc",
"place de",
"place du",
"place",
"résidence de",
"résidence du",
"résidence",
"route de",
"route du",
"route",
"rue de",
"rue du",
"rue",
"square de",
"square du",
"square",
"stade de",
"stade du",
"stade"
"stade",
"villa de",
"villa du",
"villa"
],
"removePostfixes": [
"baan",
@ -278,4 +319,4 @@
}
}
]
}
}