Chore: improve typing

This commit is contained in:
Pieter Vander Vennet 2025-04-09 17:10:33 +02:00
parent d01f830632
commit 01f1fbf89a
5 changed files with 38 additions and 14 deletions

View file

@ -37,8 +37,10 @@ export class Stores {
*/
public static FromPromise<T>(promise: Promise<T>): Store<T | undefined> {
const src = new UIEventSource<T>(undefined)
promise?.then((d) => src.setData(d))
promise?.catch((err) => console.warn("Promise failed:", err))
promise?.catch((err): undefined => {
console.warn("Promise failed:", err)
return undefined
})?.then((d) => src.setData(d))
return src
}
@ -108,7 +110,6 @@ export class Stores {
public static fromArray<T>(sources: ReadonlyArray<UIEventSource<T>>): UIEventSource<T[]> {
const src = new UIEventSource<T[]>(sources.map(s => s.data))
for (let i = 0; i < sources.length; i++) {
sources[i].addCallback(content => {
src.data[i] = content
@ -120,7 +121,17 @@ export class Stores {
sources[i].setData(contents[i])
}
})
return src
}
public static fromStoresArray<T>(sources: ReadonlyArray<Store<T>>): Store<T[]> {
const src = new UIEventSource<T[]>(sources.map(s => s.data))
for (let i = 0; i < sources.length; i++) {
sources[i].addCallback(content => {
src.data[i] = content
src.ping()
})
}
return src
}
}
@ -388,7 +399,8 @@ 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
@ -666,7 +678,8 @@ 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>()