forked from MapComplete/MapComplete
chore: automated housekeeping...
This commit is contained in:
parent
c9ce29f206
commit
40e894df8b
294 changed files with 14209 additions and 4192 deletions
|
@ -23,7 +23,7 @@ export class Stores {
|
|||
}
|
||||
|
||||
public static FromPromiseWithErr<T>(
|
||||
promise: Promise<T>,
|
||||
promise: Promise<T>
|
||||
): Store<{ success: T } | { error: any }> {
|
||||
return UIEventSource.FromPromiseWithErr(promise)
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ export class Stores {
|
|||
*/
|
||||
static holdDefined<T>(store: Store<T | undefined>): Store<T | undefined> {
|
||||
const newStore = new UIEventSource(store.data)
|
||||
store.addCallbackD(t => {
|
||||
store.addCallbackD((t) => {
|
||||
newStore.setData(t)
|
||||
})
|
||||
return newStore
|
||||
|
@ -133,23 +133,27 @@ export abstract class Store<T> implements Readable<T> {
|
|||
abstract map<J>(
|
||||
f: (t: T) => J,
|
||||
extraStoresToWatch: Store<any>[],
|
||||
callbackDestroyFunction: (f: () => void) => void,
|
||||
callbackDestroyFunction: (f: () => void) => void
|
||||
): Store<J>
|
||||
|
||||
public mapD<J>(
|
||||
f: (t: Exclude<T, undefined | null>) => J,
|
||||
extraStoresToWatch?: Store<any>[],
|
||||
callbackDestroyFunction?: (f: () => void) => void,
|
||||
callbackDestroyFunction?: (f: () => void) => void
|
||||
): Store<J> {
|
||||
return this.map((t) => {
|
||||
if (t === undefined) {
|
||||
return undefined
|
||||
}
|
||||
if (t === null) {
|
||||
return null
|
||||
}
|
||||
return f(<Exclude<T, undefined | null>>t)
|
||||
}, extraStoresToWatch, callbackDestroyFunction)
|
||||
return this.map(
|
||||
(t) => {
|
||||
if (t === undefined) {
|
||||
return undefined
|
||||
}
|
||||
if (t === null) {
|
||||
return null
|
||||
}
|
||||
return f(<Exclude<T, undefined | null>>t)
|
||||
},
|
||||
extraStoresToWatch,
|
||||
callbackDestroyFunction
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -176,7 +180,7 @@ export abstract class Store<T> implements Readable<T> {
|
|||
abstract addCallbackAndRun(callback: (data: T) => void): () => void
|
||||
|
||||
public withEqualityStabilized(
|
||||
comparator: (t: T | undefined, t1: T | undefined) => boolean,
|
||||
comparator: (t: T | undefined, t1: T | undefined) => boolean
|
||||
): Store<T> {
|
||||
let oldValue = undefined
|
||||
return this.map((v) => {
|
||||
|
@ -269,7 +273,10 @@ export abstract class Store<T> implements Readable<T> {
|
|||
return sink
|
||||
}
|
||||
|
||||
public bindD<X>(f: (t: Exclude<T, undefined | null>) => Store<X>, extraSources: UIEventSource<object>[] = []): Store<X> {
|
||||
public bindD<X>(
|
||||
f: (t: Exclude<T, undefined | null>) => Store<X>,
|
||||
extraSources: UIEventSource<object>[] = []
|
||||
): Store<X> {
|
||||
return this.bind((t) => {
|
||||
if (t === null) {
|
||||
return null
|
||||
|
@ -343,10 +350,10 @@ export abstract class Store<T> implements Readable<T> {
|
|||
|
||||
public abstract destroy()
|
||||
|
||||
when(callback: () => void, condition?: (v:T) => boolean) {
|
||||
condition ??= v => v === true
|
||||
this.addCallbackAndRunD(v => {
|
||||
if ( condition(v)) {
|
||||
when(callback: () => void, condition?: (v: T) => boolean) {
|
||||
condition ??= (v) => v === true
|
||||
this.addCallbackAndRunD((v) => {
|
||||
if (condition(v)) {
|
||||
callback()
|
||||
return true
|
||||
}
|
||||
|
@ -364,8 +371,7 @@ export class ImmutableStore<T> extends Store<T> {
|
|||
this.data = data
|
||||
}
|
||||
|
||||
private static readonly pass: () => void = () => {
|
||||
}
|
||||
private static readonly pass: () => void = () => {}
|
||||
|
||||
addCallback(_: (data: T) => void): () => void {
|
||||
// pass: data will never change
|
||||
|
@ -394,7 +400,7 @@ export class ImmutableStore<T> extends Store<T> {
|
|||
map<J>(
|
||||
f: (t: T) => J,
|
||||
extraStores: Store<any>[] = undefined,
|
||||
ondestroyCallback?: (f: () => void) => void,
|
||||
ondestroyCallback?: (f: () => void) => void
|
||||
): ImmutableStore<J> {
|
||||
if (extraStores?.length > 0) {
|
||||
return new MappedStore(this, f, extraStores, undefined, f(this.data), ondestroyCallback)
|
||||
|
@ -464,7 +470,7 @@ class ListenerTracker<T> {
|
|||
let endTime = new Date().getTime() / 1000
|
||||
if (endTime - startTime > 500) {
|
||||
console.trace(
|
||||
"Warning: a ping took more then 500ms; this is probably a performance issue",
|
||||
"Warning: a ping took more then 500ms; this is probably a performance issue"
|
||||
)
|
||||
}
|
||||
if (toDelete !== undefined) {
|
||||
|
@ -506,7 +512,7 @@ class MappedStore<TIn, T> extends Store<T> {
|
|||
extraStores: Store<any>[],
|
||||
upstreamListenerHandler: ListenerTracker<TIn> | undefined,
|
||||
initialState: T,
|
||||
onDestroy?: (f: () => void) => void,
|
||||
onDestroy?: (f: () => void) => void
|
||||
) {
|
||||
super()
|
||||
this._upstream = upstream
|
||||
|
@ -546,7 +552,7 @@ class MappedStore<TIn, T> extends Store<T> {
|
|||
map<J>(
|
||||
f: (t: T) => J,
|
||||
extraStores: Store<any>[] = undefined,
|
||||
ondestroyCallback?: (f: () => void) => void,
|
||||
ondestroyCallback?: (f: () => void) => void
|
||||
): Store<J> {
|
||||
let stores: Store<any>[] = undefined
|
||||
if (extraStores?.length > 0 || this._extraStores?.length > 0) {
|
||||
|
@ -568,7 +574,7 @@ class MappedStore<TIn, T> extends Store<T> {
|
|||
stores,
|
||||
this._callbacks,
|
||||
f(this.data),
|
||||
ondestroyCallback,
|
||||
ondestroyCallback
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -624,7 +630,7 @@ class MappedStore<TIn, T> extends Store<T> {
|
|||
|
||||
this._unregisterFromUpstream = this._upstream.addCallback((_) => self.update())
|
||||
this._unregisterFromExtraStores = this._extraStores?.map((store) =>
|
||||
store?.addCallback((_) => self.update()),
|
||||
store?.addCallback((_) => self.update())
|
||||
)
|
||||
this._callbacksAreRegistered = true
|
||||
}
|
||||
|
@ -645,8 +651,7 @@ class MappedStore<TIn, T> extends Store<T> {
|
|||
}
|
||||
|
||||
export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
||||
private static readonly pass: (() => void) = () => {
|
||||
}
|
||||
private static readonly pass: () => void = () => {}
|
||||
public data: T
|
||||
_callbacks: ListenerTracker<T> = new ListenerTracker<T>()
|
||||
|
||||
|
@ -661,7 +666,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
|
||||
public static flatten<X>(
|
||||
source: Store<Store<X>>,
|
||||
possibleSources?: Store<object>[],
|
||||
possibleSources?: Store<object>[]
|
||||
): UIEventSource<X> {
|
||||
const sink = new UIEventSource<X>(source.data?.data)
|
||||
|
||||
|
@ -690,7 +695,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
*/
|
||||
public static FromPromise<T>(
|
||||
promise: Promise<T>,
|
||||
onError: (e) => void = undefined,
|
||||
onError: (e) => void = undefined
|
||||
): UIEventSource<T> {
|
||||
const src = new UIEventSource<T>(undefined)
|
||||
promise?.then((d) => src.setData(d))
|
||||
|
@ -711,7 +716,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
* @constructor
|
||||
*/
|
||||
public static FromPromiseWithErr<T>(
|
||||
promise: Promise<T>,
|
||||
promise: Promise<T>
|
||||
): UIEventSource<{ success: T } | { error: any } | undefined> {
|
||||
const src = new UIEventSource<{ success: T } | { error: any }>(undefined)
|
||||
promise
|
||||
|
@ -743,7 +748,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
return undefined
|
||||
}
|
||||
return "" + fl
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -774,7 +779,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
return undefined
|
||||
}
|
||||
return "" + fl
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -782,11 +787,14 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
return stringUIEventSource.sync(
|
||||
(str) => str === "true",
|
||||
[],
|
||||
(b) => "" + b,
|
||||
(b) => "" + b
|
||||
)
|
||||
}
|
||||
|
||||
static asObject<T extends object>(stringUIEventSource: UIEventSource<string>, defaultV: T): UIEventSource<T> {
|
||||
static asObject<T extends object>(
|
||||
stringUIEventSource: UIEventSource<string>,
|
||||
defaultV: T
|
||||
): UIEventSource<T> {
|
||||
return stringUIEventSource.sync(
|
||||
(str) => {
|
||||
if (str === undefined || str === null || str === "") {
|
||||
|
@ -800,7 +808,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
}
|
||||
},
|
||||
[],
|
||||
(b) => JSON.stringify(b) ?? "",
|
||||
(b) => JSON.stringify(b) ?? ""
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -890,7 +898,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
public map<J>(
|
||||
f: (t: T) => J,
|
||||
extraSources: Store<any>[] = [],
|
||||
onDestroy?: (f: () => void) => void,
|
||||
onDestroy?: (f: () => void) => void
|
||||
): Store<J> {
|
||||
return new MappedStore(this, f, extraSources, this._callbacks, f(this.data), onDestroy)
|
||||
}
|
||||
|
@ -902,7 +910,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
public mapD<J>(
|
||||
f: (t: Exclude<T, undefined | null>) => J,
|
||||
extraSources: Store<any>[] = [],
|
||||
callbackDestroyFunction?: (f: () => void) => void,
|
||||
callbackDestroyFunction?: (f: () => void) => void
|
||||
): Store<J | undefined> {
|
||||
return new MappedStore(
|
||||
this,
|
||||
|
@ -920,7 +928,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
this.data === undefined || this.data === null
|
||||
? <undefined | null>this.data
|
||||
: f(<any>this.data),
|
||||
callbackDestroyFunction,
|
||||
callbackDestroyFunction
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -940,7 +948,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
f: (t: T) => J,
|
||||
extraSources: Store<any>[],
|
||||
g: (j: J, t: T) => T,
|
||||
allowUnregister = false,
|
||||
allowUnregister = false
|
||||
): UIEventSource<J> {
|
||||
const self = this
|
||||
|
||||
|
@ -949,7 +957,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
|
|||
|
||||
const newSource = new UIEventSource<J>(f(this.data), "map(" + this.tag + ")@" + callee)
|
||||
|
||||
const update = function() {
|
||||
const update = function () {
|
||||
newSource.setData(f(self.data))
|
||||
return allowUnregister && newSource._callbacks.length() === 0
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue