forked from MapComplete/MapComplete
Register callbacks to mapped store immediately too
This commit is contained in:
parent
f97b770203
commit
eac3393495
1 changed files with 15 additions and 8 deletions
|
@ -373,6 +373,7 @@ class MappedStore<TIn, T> extends Store<T> {
|
||||||
private _upstreamCallbackHandler: ListenerTracker<TIn>;
|
private _upstreamCallbackHandler: ListenerTracker<TIn>;
|
||||||
private _upstreamPingCount: number = -1;
|
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
|
||||||
|
@ -391,6 +392,7 @@ class MappedStore<TIn, T> extends Store<T> {
|
||||||
this._data = f(upstream.data)
|
this._data = f(upstream.data)
|
||||||
this._upstreamPingCount = upstreamListenerHandler.pingCount
|
this._upstreamPingCount = upstreamListenerHandler.pingCount
|
||||||
this._extraStores = extraStores;
|
this._extraStores = extraStores;
|
||||||
|
this.registerCallbacksToUpstream()
|
||||||
}
|
}
|
||||||
|
|
||||||
private _data: T;
|
private _data: T;
|
||||||
|
@ -447,6 +449,18 @@ 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)
|
||||||
|
@ -460,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 () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue