From f97b770203b25c37bda6bd31c5d6618d7a63076d Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Wed, 8 Jun 2022 01:20:37 +0200 Subject: [PATCH] More debugging of mappedStore --- Logic/UIEventSource.ts | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/Logic/UIEventSource.ts b/Logic/UIEventSource.ts index eadd38d520..9a0044b984 100644 --- a/Logic/UIEventSource.ts +++ b/Logic/UIEventSource.ts @@ -305,7 +305,8 @@ export class ImmutableStore extends Store { */ class ListenerTracker { 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 { * 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,6 +370,8 @@ class ListenerTracker { class MappedStore extends Store { private _upstream: Store; + private _upstreamCallbackHandler: ListenerTracker; + private _upstreamPingCount: number = -1; private _unregisterFromUpstream: (() => void) private _f: (t: TIn) => T; private readonly _extraStores: Store[] | undefined; @@ -378,18 +382,38 @@ class MappedStore extends Store { private static readonly pass: () => {} - constructor(upstream: Store, f: (t: TIn) => T, extraStores: Store[] = undefined, initialData : T= undefined) { + constructor(upstream: Store, f: (t: TIn) => T, extraStores: Store[] = undefined, + upstreamListenerHandler: ListenerTracker) { 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; } 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 +437,7 @@ class MappedStore extends Store { this._upstream, data => f(this._f(data)), stores, - f(this._data) + this._upstreamCallbackHandler ); } @@ -426,6 +450,7 @@ class MappedStore extends Store { private update(): void { const newData = this._f(this._upstream.data) + this._upstreamPingCount = this._upstreamCallbackHandler.pingCount if (this._data == newData) { return; } @@ -487,7 +512,7 @@ class MappedStore extends Store { export class UIEventSource extends Store { public data: T; - private _callbacks: ListenerTracker = new ListenerTracker() + _callbacks: ListenerTracker = new ListenerTracker() private static readonly pass: () => {} @@ -637,7 +662,7 @@ export class UIEventSource extends Store { */ public map(f: ((t: T) => J), extraSources: Store[] = []): Store { - return new MappedStore(this, f, extraSources); + return new MappedStore(this, f, extraSources, this._callbacks); } /**