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> { class ListenerTracker<T> {
private readonly _callbacks: ((t: T) => (boolean | void | any)) [] = []; private readonly _callbacks: ((t: T) => (boolean | void | any)) [] = [];
public pingCount = 0;
/** /**
* Adds a callback which can be called; a function to unregister is returned * 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 * Returns the number of registered callbacks
*/ */
public ping(data: T): number { public ping(data: T): number {
this.pingCount ++;
let toDelete = undefined let toDelete = undefined
let startTime = new Date().getTime() / 1000; let startTime = new Date().getTime() / 1000;
for (const callback of this._callbacks) { for (const callback of this._callbacks) {
@ -368,7 +370,10 @@ class ListenerTracker<T> {
class MappedStore<TIn, T> extends Store<T> { class MappedStore<TIn, T> extends Store<T> {
private _upstream: Store<TIn>; private _upstream: Store<TIn>;
private _upstreamCallbackHandler: ListenerTracker<TIn>;
private _upstreamPingCount: number = -1;
private _unregisterFromUpstream: (() => void) private _unregisterFromUpstream: (() => void)
private _f: (t: TIn) => T; private _f: (t: TIn) => T;
private readonly _extraStores: Store<any>[] | undefined; private readonly _extraStores: Store<any>[] | undefined;
private _unregisterFromExtraStores: (() => void)[] | undefined private _unregisterFromExtraStores: (() => void)[] | undefined
@ -378,18 +383,39 @@ class MappedStore<TIn, T> extends Store<T> {
private static readonly pass: () => {} 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(); super();
this._upstream = upstream; this._upstream = upstream;
this._upstreamCallbackHandler = upstreamListenerHandler
this._f = f; this._f = f;
this._data = initialData ?? f(upstream.data) this._data = f(upstream.data)
this._upstreamPingCount = upstreamListenerHandler.pingCount
this._extraStores = extraStores; this._extraStores = extraStores;
this.registerCallbacksToUpstream()
} }
private _data: T; private _data: T;
private _callbacksAreRegistered = false 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 return this._data
} }
@ -413,7 +439,7 @@ class MappedStore<TIn, T> extends Store<T> {
this._upstream, this._upstream,
data => f(this._f(data)), data => f(this._f(data)),
stores, stores,
f(this._data) this._upstreamCallbackHandler
); );
} }
@ -423,9 +449,22 @@ class MappedStore<TIn, T> extends Store<T> {
this._unregisterFromUpstream() this._unregisterFromUpstream()
this._unregisterFromExtraStores?.forEach(unr => unr()) 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 { private update(): void {
const newData = this._f(this._upstream.data) const newData = this._f(this._upstream.data)
this._upstreamPingCount = this._upstreamCallbackHandler.pingCount
if (this._data == newData) { if (this._data == newData) {
return; return;
} }
@ -435,16 +474,9 @@ class MappedStore<TIn, T> extends Store<T> {
addCallback(callback: (data: T) => (any | boolean | void)): (() => void) { addCallback(callback: (data: T) => (any | boolean | void)): (() => void) {
if (!this._callbacksAreRegistered) { if (!this._callbacksAreRegistered) {
const self = this
// This is the first callback that is added // This is the first callback that is added
// We register this 'map' to the upstream object and all the streams // We register this 'map' to the upstream object and all the streams
this._unregisterFromUpstream = this._upstream.addCallback( this.registerCallbacksToUpstream()
_ => self.update()
)
this._unregisterFromExtraStores = this._extraStores?.map(store =>
store?.addCallback(_ => self.update())
)
this._callbacksAreRegistered = true;
} }
const unregister = this._callbacks.addCallback(callback) const unregister = this._callbacks.addCallback(callback)
return () => { return () => {
@ -487,7 +519,7 @@ class MappedStore<TIn, T> extends Store<T> {
export class UIEventSource<T> extends Store<T> { export class UIEventSource<T> extends Store<T> {
public data: T; public data: T;
private _callbacks: ListenerTracker<T> = new ListenerTracker<T>() _callbacks: ListenerTracker<T> = new ListenerTracker<T>()
private static readonly pass: () => {} private static readonly pass: () => {}
@ -637,7 +669,7 @@ export class UIEventSource<T> extends Store<T> {
*/ */
public map<J>(f: ((t: T) => J), public map<J>(f: ((t: T) => J),
extraSources: Store<any>[] = []): Store<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" "Q22698"
], ],
"removePrefixes": [ "removePrefixes": [
"allée de",
"allée du", "allée du",
"allée", "allée",
"autoroute de",
"autoroute du", "autoroute du",
"autoroute", "autoroute",
"avenue de",
"avenue du", "avenue du",
"avenue", "avenue",
"bibliothèque de",
"bibliothèque du", "bibliothèque du",
"bibliothèque", "bibliothèque",
"boulevard de",
"boulevard du", "boulevard du",
"boulevard", "boulevard",
"centre du", "centre culturel de",
"centre",
"centre culturel du", "centre culturel du",
"centre culturel", "centre culturel",
"centre de",
"centre du",
"centre",
"chaussée de",
"chaussée du", "chaussée du",
"chaussée", "chaussée",
"chemin de",
"chemin du", "chemin du",
"chemin", "chemin",
"collège de",
"collège du", "collège du",
"collège", "collège",
"complexe sportif de",
"complexe sportif du", "complexe sportif du",
"complexe sportif", "complexe sportif",
"école du", "école élémentaire de",
"école",
"école élémentaire du", "école élémentaire du",
"école élémentaire", "école élémentaire",
"école maternelle de",
"école maternelle du", "école maternelle du",
"école maternelle", "école maternelle",
"école primaire de",
"école primaire du", "école primaire du",
"école primaire", "école primaire",
"école de",
"école du",
"école",
"esplanade de",
"esplanade du", "esplanade du",
"esplanade", "esplanade",
"groupe scolaire de",
"groupe scolaire du", "groupe scolaire du",
"groupe scolaire", "groupe scolaire",
"gymnase de",
"gymnase du", "gymnase du",
"gymnase", "gymnase",
"impasse de",
"impasse du",
"impasse",
"lycée de",
"lycée du", "lycée du",
"lycée", "lycée",
"mail de",
"mail du", "mail du",
"mail", "mail",
"médiathèque de",
"médiathèque du", "médiathèque du",
"médiathèque", "médiathèque",
"musée de",
"musée du", "musée du",
"musée", "musée",
"parc de",
"parc du", "parc du",
"parc", "parc",
"place de",
"place du",
"place",
"résidence de",
"résidence du",
"résidence",
"route de",
"route du",
"route",
"rue de",
"rue du", "rue du",
"rue", "rue",
"square de",
"square du", "square du",
"square", "square",
"stade de",
"stade du", "stade du",
"stade" "stade",
"villa de",
"villa du",
"villa"
], ],
"removePostfixes": [ "removePostfixes": [
"baan", "baan",
@ -278,4 +319,4 @@
} }
} }
] ]
} }