Chore: housekeeping, fix tests

This commit is contained in:
Pieter Vander Vennet 2023-10-06 03:34:26 +02:00
parent 1f67ab2aca
commit 36a73f3a17
50 changed files with 1124 additions and 1022 deletions

View file

@ -1,17 +1,16 @@
import Constants from "../Models/Constants";
import Constants from "../Models/Constants"
export default class Maproulette {
public static readonly defaultEndpoint = "https://maproulette.org/api/v2"
public static readonly defaultEndpoint = "https://maproulette.org/api/v2";
public static readonly STATUS_OPEN = 0;
public static readonly STATUS_FIXED = 1;
public static readonly STATUS_FALSE_POSITIVE = 2;
public static readonly STATUS_SKIPPED = 3;
public static readonly STATUS_DELETED = 4;
public static readonly STATUS_ALREADY_FIXED = 5;
public static readonly STATUS_TOO_HARD = 6;
public static readonly STATUS_DISABLED = 9;
public static readonly STATUS_OPEN = 0
public static readonly STATUS_FIXED = 1
public static readonly STATUS_FALSE_POSITIVE = 2
public static readonly STATUS_SKIPPED = 3
public static readonly STATUS_DELETED = 4
public static readonly STATUS_ALREADY_FIXED = 5
public static readonly STATUS_TOO_HARD = 6
public static readonly STATUS_DISABLED = 9
public static readonly STATUS_MEANING = {
0: "Open",
@ -21,28 +20,28 @@ export default class Maproulette {
4: "Deleted",
5: "Already fixed",
6: "Too hard",
9: "Disabled"
};
public static singleton = new Maproulette();
9: "Disabled",
}
public static singleton = new Maproulette()
/*
* The API endpoint to use
*/
endpoint: string;
endpoint: string
/**
* The API key to use for all requests
*/
private readonly apiKey: string;
private readonly apiKey: string
/**
* Creates a new Maproulette instance
* @param endpoint The API endpoint to use
*/
constructor(endpoint?: string) {
this.endpoint = endpoint ?? Maproulette.defaultEndpoint;
if(!this.endpoint ){
this.endpoint = endpoint ?? Maproulette.defaultEndpoint
if (!this.endpoint) {
throw "MapRoulette endpoint is undefined. Make sure that `Maproulette.defaultEndpoint` is defined on top of the class"
}
this.apiKey = Constants.MaprouletteApiKey;
this.apiKey = Constants.MaprouletteApiKey
}
/**
@ -54,14 +53,14 @@ export default class Maproulette {
*/
public static codeToIndex(code: string): number | undefined {
if (code === "Created") {
return Maproulette.STATUS_OPEN;
return Maproulette.STATUS_OPEN
}
for (let i = 0; i < 9; i++) {
if (Maproulette.STATUS_MEANING["" + i] === code) {
return i;
return i
}
}
return undefined;
return undefined
}
/**
@ -82,18 +81,18 @@ export default class Maproulette {
completionResponses?: Record<string, string>
}
): Promise<void> {
console.log("Maproulette: setting", `${this.endpoint}/task/${taskId}/${status}`, options);
console.log("Maproulette: setting", `${this.endpoint}/task/${taskId}/${status}`, options)
const response = await fetch(`${this.endpoint}/task/${taskId}/${status}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
apiKey: this.apiKey
apiKey: this.apiKey,
},
body: options !== undefined ? JSON.stringify(options) : undefined
});
body: options !== undefined ? JSON.stringify(options) : undefined,
})
if (response.status !== 204) {
console.log(`Failed to close task: ${response.status}`);
throw `Failed to close task: ${response.status}`;
console.log(`Failed to close task: ${response.status}`)
throw `Failed to close task: ${response.status}`
}
}
}

View file

@ -532,7 +532,6 @@ export class Changes {
deletedObjects: OsmObject[]
} = self.CreateChangesetObjects(pending, objects)
return Changes.createChangesetFor("" + csId, changes)
},
metatags,
@ -561,8 +560,11 @@ export class Changes {
const successes = await Promise.all(
Array.from(pendingPerTheme, async ([theme, pendingChanges]) => {
try {
const openChangeset = UIEventSource.asInt(this.state.osmConnection
.GetPreference("current-open-changeset-" + theme))
const openChangeset = UIEventSource.asInt(
this.state.osmConnection.GetPreference(
"current-open-changeset-" + theme
)
)
console.log(
"Using current-open-changeset-" +
theme +

View file

@ -1,14 +1,17 @@
import { UIEventSource } from "../UIEventSource"
import UserDetails, { OsmConnection } from "./OsmConnection"
import { Utils } from "../../Utils"
import { LocalStorageSource } from "../Web/LocalStorageSource";
import { LocalStorageSource } from "../Web/LocalStorageSource"
export class OsmPreferences {
/**
* A dictionary containing all the preferences. The 'preferenceSources' will be initialized from this
* We keep a local copy of them, to init mapcomplete with the previous choices and to be able to get the open changesets right away
*/
public preferences = LocalStorageSource.GetParsed<Record<string, string>>( "all-osm-preferences", {})
public preferences = LocalStorageSource.GetParsed<Record<string, string>>(
"all-osm-preferences",
{}
)
private readonly preferenceSources = new Map<string, UIEventSource<string>>()
private auth: any
private userDetails: UIEventSource<UserDetails>

View file

@ -30,7 +30,6 @@ export class OsmConnectionFeatureSwitches {
public readonly featureSwitchFakeUser: UIEventSource<boolean>
constructor() {
this.featureSwitchFakeUser = QueryParameters.GetBooleanQueryParameter(
"fake-user",
false,

View file

@ -1,14 +1,42 @@
import { Utils } from "../../Utils"
/** This code is autogenerated - do not edit. Edit ./assets/layers/usersettings/usersettings.json instead */
export class ThemeMetaTagging {
public static readonly themeName = "usersettings"
public static readonly themeName = "usersettings"
public metaTaggging_for_usersettings(feat: {properties: Record<string, string>}) {
Utils.AddLazyProperty(feat.properties, '_mastodon_candidate_md', () => feat.properties._description.match(/\[[^\]]*\]\((.*(mastodon|en.osm.town).*)\).*/)?.at(1) )
Utils.AddLazyProperty(feat.properties, '_d', () => feat.properties._description?.replace(/&lt;/g,'<')?.replace(/&gt;/g,'>') ?? '' )
Utils.AddLazyProperty(feat.properties, '_mastodon_candidate_a', () => (feat => {const e = document.createElement('div');e.innerHTML = feat.properties._d;return Array.from(e.getElementsByTagName("a")).filter(a => a.href.match(/mastodon|en.osm.town/) !== null)[0]?.href }) (feat) )
Utils.AddLazyProperty(feat.properties, '_mastodon_link', () => (feat => {const e = document.createElement('div');e.innerHTML = feat.properties._d;return Array.from(e.getElementsByTagName("a")).filter(a => a.getAttribute("rel")?.indexOf('me') >= 0)[0]?.href})(feat) )
Utils.AddLazyProperty(feat.properties, '_mastodon_candidate', () => feat.properties._mastodon_candidate_md ?? feat.properties._mastodon_candidate_a )
feat.properties['__current_backgroun'] = 'initial_value'
}
}
public metaTaggging_for_usersettings(feat: { properties: Record<string, string> }) {
Utils.AddLazyProperty(feat.properties, "_mastodon_candidate_md", () =>
feat.properties._description
.match(/\[[^\]]*\]\((.*(mastodon|en.osm.town).*)\).*/)
?.at(1)
)
Utils.AddLazyProperty(
feat.properties,
"_d",
() => feat.properties._description?.replace(/&lt;/g, "<")?.replace(/&gt;/g, ">") ?? ""
)
Utils.AddLazyProperty(feat.properties, "_mastodon_candidate_a", () =>
((feat) => {
const e = document.createElement("div")
e.innerHTML = feat.properties._d
return Array.from(e.getElementsByTagName("a")).filter(
(a) => a.href.match(/mastodon|en.osm.town/) !== null
)[0]?.href
})(feat)
)
Utils.AddLazyProperty(feat.properties, "_mastodon_link", () =>
((feat) => {
const e = document.createElement("div")
e.innerHTML = feat.properties._d
return Array.from(e.getElementsByTagName("a")).filter(
(a) => a.getAttribute("rel")?.indexOf("me") >= 0
)[0]?.href
})(feat)
)
Utils.AddLazyProperty(
feat.properties,
"_mastodon_candidate",
() => feat.properties._mastodon_candidate_md ?? feat.properties._mastodon_candidate_a
)
feat.properties["__current_backgroun"] = "initial_value"
}
}

View file

@ -1,28 +1,28 @@
import { Utils } from "../Utils";
import { Readable, Subscriber, Unsubscriber, Updater, Writable } from "svelte/store";
import { Utils } from "../Utils"
import { Readable, Subscriber, Unsubscriber, Updater, Writable } from "svelte/store"
/**
* Various static utils
*/
export class Stores {
public static Chronic(millis: number, asLong: () => boolean = undefined): Store<Date> {
const source = new UIEventSource<Date>(undefined);
const source = new UIEventSource<Date>(undefined)
function run() {
source.setData(new Date());
source.setData(new Date())
if (asLong === undefined || asLong()) {
window.setTimeout(run, millis);
window.setTimeout(run, millis)
}
}
run();
return source;
run()
return source
}
public static FromPromiseWithErr<T>(
promise: Promise<T>
): Store<{ success: T } | { error: any }> {
return UIEventSource.FromPromiseWithErr(promise);
return UIEventSource.FromPromiseWithErr(promise)
}
/**
@ -32,14 +32,14 @@ export class Stores {
* @constructor
*/
public static FromPromise<T>(promise: Promise<T>): Store<T> {
const src = new UIEventSource<T>(undefined);
promise?.then((d) => src.setData(d));
promise?.catch((err) => console.warn("Promise failed:", err));
return src;
const src = new UIEventSource<T>(undefined)
promise?.then((d) => src.setData(d))
promise?.catch((err) => console.warn("Promise failed:", err))
return src
}
public static flatten<X>(source: Store<Store<X>>, possibleSources?: Store<any>[]): Store<X> {
return UIEventSource.flatten(source, possibleSources);
return UIEventSource.flatten(source, possibleSources)
}
/**
@ -57,39 +57,39 @@ export class Stores {
* @constructor
*/
public static ListStabilized<T>(src: Store<T[]>): Store<T[]> {
const stable = new UIEventSource<T[]>(undefined);
const stable = new UIEventSource<T[]>(undefined)
src.addCallbackAndRun((list) => {
if (list === undefined) {
stable.setData(undefined);
return;
stable.setData(undefined)
return
}
if (Utils.sameList(stable.data, list)) {
return;
return
}
stable.setData(list);
});
return stable;
stable.setData(list)
})
return stable
}
}
export abstract class Store<T> implements Readable<T> {
abstract readonly data: T;
abstract readonly data: T
/**
* Optional value giving a title to the UIEventSource, mainly used for debugging
*/
public readonly tag: string | undefined;
public readonly tag: string | undefined
constructor(tag: string = undefined) {
this.tag = tag;
this.tag = tag
if (tag === undefined || tag === "") {
let createStack = Utils.runningFromConsole;
let createStack = Utils.runningFromConsole
if (!Utils.runningFromConsole) {
createStack = window.location.hostname === "127.0.0.1";
createStack = window.location.hostname === "127.0.0.1"
}
if (createStack) {
const callstack = new Error().stack.split("\n");
this.tag = callstack[1];
const callstack = new Error().stack.split("\n")
this.tag = callstack[1]
}
}
}
@ -100,13 +100,13 @@ export abstract class Store<T> implements Readable<T> {
public mapD<J>(f: (t: T) => J, extraStoresToWatch?: Store<any>[]): Store<J> {
return this.map((t) => {
if (t === undefined) {
return undefined;
return undefined
}
if (t === null) {
return null;
return null
}
return f(t);
}, extraStoresToWatch);
return f(t)
}, extraStoresToWatch)
}
/**
@ -135,17 +135,17 @@ export abstract class Store<T> implements Readable<T> {
public withEqualityStabilized(
comparator: (t: T | undefined, t1: T | undefined) => boolean
): Store<T> {
let oldValue = undefined;
let oldValue = undefined
return this.map((v) => {
if (v == oldValue) {
return oldValue;
return oldValue
}
if (comparator(oldValue, v)) {
return oldValue;
return oldValue
}
oldValue = v;
return v;
});
oldValue = v
return v
})
}
/**
@ -195,49 +195,49 @@ export abstract class Store<T> implements Readable<T> {
* lastValue // => "def"
*/
public bind<X>(f: (t: T) => Store<X>): Store<X> {
const mapped = this.map(f);
const sink = new UIEventSource<X>(undefined);
const seenEventSources = new Set<Store<X>>();
const mapped = this.map(f)
const sink = new UIEventSource<X>(undefined)
const seenEventSources = new Set<Store<X>>()
mapped.addCallbackAndRun((newEventSource) => {
if (newEventSource === null) {
sink.setData(null);
sink.setData(null)
} else if (newEventSource === undefined) {
sink.setData(undefined);
sink.setData(undefined)
} else if (!seenEventSources.has(newEventSource)) {
seenEventSources.add(newEventSource);
seenEventSources.add(newEventSource)
newEventSource.addCallbackAndRun((resultData) => {
if (mapped.data === newEventSource) {
sink.setData(resultData);
sink.setData(resultData)
}
});
})
} else {
// Already seen, so we don't have to add a callback, just update the value
sink.setData(newEventSource.data);
sink.setData(newEventSource.data)
}
});
})
return sink;
return sink
}
public stabilized(millisToStabilize): Store<T> {
if (Utils.runningFromConsole) {
return this;
return this
}
const newSource = new UIEventSource<T>(this.data);
const newSource = new UIEventSource<T>(this.data)
const self = this;
const self = this
this.addCallback((latestData) => {
window.setTimeout(() => {
if (self.data == latestData) {
// compare by reference.
// Note that 'latestData' and 'self.data' are both from the same UIEVentSource, but both are dereferenced at a different time
newSource.setData(latestData);
newSource.setData(latestData)
}
}, millisToStabilize);
});
}, millisToStabilize)
})
return newSource;
return newSource
}
/**
@ -247,23 +247,23 @@ export abstract class Store<T> implements Readable<T> {
* @constructor
*/
public AsPromise(condition?: (t: T) => boolean): Promise<T> {
const self = this;
condition = condition ?? ((t) => t !== undefined);
const self = this
condition = condition ?? ((t) => t !== undefined)
return new Promise((resolve) => {
const data = self.data;
const data = self.data
if (condition(data)) {
resolve(data);
resolve(data)
} else {
self.addCallbackD((data) => {
if (condition(data)) {
resolve(data);
return true; // return true to unregister as we only need to be called once
resolve(data)
return true // return true to unregister as we only need to be called once
} else {
return false; // We didn't resolve yet, wait for the next ping
return false // We didn't resolve yet, wait for the next ping
}
});
})
}
});
})
}
/**
@ -275,51 +275,50 @@ export abstract class Store<T> implements Readable<T> {
// Note: run is wrapped in an anonymous function. 'Run' returns the value. If this value happens to be true, it would unsubscribe
return this.addCallbackAndRun((v) => {
run(v);
});
run(v)
})
}
}
export class ImmutableStore<T> extends Store<T> {
public readonly data: T;
public readonly data: T
constructor(data: T) {
super();
this.data = data;
super()
this.data = data
}
private static readonly pass: () => void = () => {
};
private static readonly pass: () => void = () => {}
addCallback(_: (data: T) => void): () => void {
// pass: data will never change
return ImmutableStore.pass;
return ImmutableStore.pass
}
addCallbackAndRun(callback: (data: T) => void): () => void {
callback(this.data);
callback(this.data)
// no callback registry: data will never change
return ImmutableStore.pass;
return ImmutableStore.pass
}
addCallbackAndRunD(callback: (data: T) => void): () => void {
if (this.data !== undefined) {
callback(this.data);
callback(this.data)
}
// no callback registry: data will never change
return ImmutableStore.pass;
return ImmutableStore.pass
}
addCallbackD(_: (data: T) => void): () => void {
// pass: data will never change
return ImmutableStore.pass;
return ImmutableStore.pass
}
map<J>(f: (t: T) => J, extraStores: Store<any>[] = undefined): ImmutableStore<J> {
if (extraStores?.length > 0) {
return new MappedStore(this, f, extraStores, undefined, f(this.data));
return new MappedStore(this, f, extraStores, undefined, f(this.data))
}
return new ImmutableStore<J>(f(this.data));
return new ImmutableStore<J>(f(this.data))
}
}
@ -327,8 +326,8 @@ export class ImmutableStore<T> extends Store<T> {
* Keeps track of the callback functions
*/
class ListenerTracker<T> {
public pingCount = 0;
private readonly _callbacks: ((t: T) => boolean | void | any)[] = [];
public pingCount = 0
private readonly _callbacks: ((t: T) => boolean | void | any)[] = []
/**
* Adds a callback which can be called; a function to unregister is returned
@ -336,17 +335,17 @@ class ListenerTracker<T> {
public addCallback(callback: (t: T) => boolean | void | any): () => void {
if (callback === console.log) {
// This ^^^ actually works!
throw "Don't add console.log directly as a callback - you'll won't be able to find it afterwards. Wrap it in a lambda instead.";
throw "Don't add console.log directly as a callback - you'll won't be able to find it afterwards. Wrap it in a lambda instead."
}
this._callbacks.push(callback);
this._callbacks.push(callback)
// Give back an unregister-function!
return () => {
const index = this._callbacks.indexOf(callback);
const index = this._callbacks.indexOf(callback)
if (index >= 0) {
this._callbacks.splice(index, 1);
this._callbacks.splice(index, 1)
}
};
}
}
/**
@ -354,40 +353,40 @@ class ListenerTracker<T> {
* Returns the number of registered callbacks
*/
public ping(data: T): number {
this.pingCount++;
let toDelete = undefined;
let startTime = new Date().getTime() / 1000;
this.pingCount++
let toDelete = undefined
let startTime = new Date().getTime() / 1000
for (const callback of this._callbacks) {
try {
if (callback(data) === true) {
// This callback wants to be deleted
// Note: it has to return precisely true in order to avoid accidental deletions
if (toDelete === undefined) {
toDelete = [callback];
toDelete = [callback]
} else {
toDelete.push(callback);
toDelete.push(callback)
}
}
} catch (e) {
console.error("Got an error while running a callback:", e);
console.error("Got an error while running a callback:", e)
}
}
let endTime = new Date().getTime() / 1000;
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"
);
)
}
if (toDelete !== undefined) {
for (const toDeleteElement of toDelete) {
this._callbacks.splice(this._callbacks.indexOf(toDeleteElement), 1);
this._callbacks.splice(this._callbacks.indexOf(toDeleteElement), 1)
}
}
return this._callbacks.length;
return this._callbacks.length
}
length() {
return this._callbacks.length;
return this._callbacks.length
}
}
@ -395,16 +394,16 @@ class ListenerTracker<T> {
* The mapped store is a helper type which does the mapping of a function.
*/
class MappedStore<TIn, T> extends Store<T> {
private static readonly pass: () => {};
private readonly _upstream: Store<TIn>;
private readonly _upstreamCallbackHandler: ListenerTracker<TIn> | undefined;
private _upstreamPingCount: number = -1;
private _unregisterFromUpstream: () => void;
private readonly _f: (t: TIn) => T;
private readonly _extraStores: Store<any>[] | undefined;
private _unregisterFromExtraStores: (() => void)[] | undefined;
private _callbacks: ListenerTracker<T> = new ListenerTracker<T>();
private _callbacksAreRegistered = false;
private static readonly pass: () => {}
private readonly _upstream: Store<TIn>
private readonly _upstreamCallbackHandler: ListenerTracker<TIn> | undefined
private _upstreamPingCount: number = -1
private _unregisterFromUpstream: () => void
private readonly _f: (t: TIn) => T
private readonly _extraStores: Store<any>[] | undefined
private _unregisterFromExtraStores: (() => void)[] | undefined
private _callbacks: ListenerTracker<T> = new ListenerTracker<T>()
private _callbacksAreRegistered = false
constructor(
upstream: Store<TIn>,
@ -414,20 +413,20 @@ class MappedStore<TIn, T> extends Store<T> {
initialState: T,
onDestroy?: (f: () => void) => void
) {
super();
this._upstream = upstream;
this._upstreamCallbackHandler = upstreamListenerHandler;
this._f = f;
this._data = initialState;
this._upstreamPingCount = upstreamListenerHandler?.pingCount;
this._extraStores = extraStores;
this.registerCallbacksToUpstream();
super()
this._upstream = upstream
this._upstreamCallbackHandler = upstreamListenerHandler
this._f = f
this._data = initialState
this._upstreamPingCount = upstreamListenerHandler?.pingCount
this._extraStores = extraStores
this.registerCallbacksToUpstream()
if (onDestroy !== undefined) {
onDestroy(() => this.unregisterFromUpstream());
onDestroy(() => this.unregisterFromUpstream())
}
}
private _data: T;
private _data: T
/**
* Gets the current data from the store
@ -443,27 +442,27 @@ class MappedStore<TIn, T> extends Store<T> {
// 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);
this._data = this._f(this._upstream.data)
}
return this._data;
return this._data
}
return this._data;
return this._data
}
map<J>(f: (t: T) => J, extraStores: Store<any>[] = undefined): Store<J> {
let stores: Store<any>[] = undefined;
let stores: Store<any>[] = undefined
if (extraStores?.length > 0 || this._extraStores?.length > 0) {
stores = [];
stores = []
}
if (extraStores?.length > 0) {
stores.push(...extraStores);
stores.push(...extraStores)
}
if (this._extraStores?.length > 0) {
this._extraStores?.forEach((store) => {
if (stores.indexOf(store) < 0) {
stores.push(store);
stores.push(store)
}
});
})
}
return new MappedStore(
this,
@ -471,111 +470,111 @@ class MappedStore<TIn, T> extends Store<T> {
stores,
this._callbacks,
f(this.data)
);
)
}
addCallback(callback: (data: T) => any | boolean | void): () => void {
if (!this._callbacksAreRegistered) {
// This is the first callback that is added
// We register this 'map' to the upstream object and all the streams
this.registerCallbacksToUpstream();
this.registerCallbacksToUpstream()
}
const unregister = this._callbacks.addCallback(callback);
const unregister = this._callbacks.addCallback(callback)
return () => {
unregister();
unregister()
if (this._callbacks.length() == 0) {
this.unregisterFromUpstream();
this.unregisterFromUpstream()
}
};
}
}
addCallbackAndRun(callback: (data: T) => any | boolean | void): () => void {
const unregister = this.addCallback(callback);
const doRemove = callback(this.data);
const unregister = this.addCallback(callback)
const doRemove = callback(this.data)
if (doRemove === true) {
unregister();
return MappedStore.pass;
unregister()
return MappedStore.pass
}
return unregister;
return unregister
}
addCallbackAndRunD(callback: (data: T) => any | boolean | void): () => void {
return this.addCallbackAndRun((data) => {
if (data !== undefined) {
return callback(data);
return callback(data)
}
});
})
}
addCallbackD(callback: (data: T) => any | boolean | void): () => void {
return this.addCallback((data) => {
if (data !== undefined) {
return callback(data);
return callback(data)
}
});
})
}
private unregisterFromUpstream() {
console.debug("Unregistering callbacks for", this.tag);
this._callbacksAreRegistered = false;
this._unregisterFromUpstream();
this._unregisterFromExtraStores?.forEach((unr) => unr());
console.debug("Unregistering callbacks for", this.tag)
this._callbacksAreRegistered = false
this._unregisterFromUpstream()
this._unregisterFromExtraStores?.forEach((unr) => unr())
}
private registerCallbacksToUpstream() {
const self = this;
const self = this
this._unregisterFromUpstream = this._upstream.addCallback((_) => self.update());
this._unregisterFromUpstream = this._upstream.addCallback((_) => self.update())
this._unregisterFromExtraStores = this._extraStores?.map((store) =>
store?.addCallback((_) => self.update())
);
this._callbacksAreRegistered = true;
)
this._callbacksAreRegistered = true
}
private update(): void {
const newData = this._f(this._upstream.data);
this._upstreamPingCount = this._upstreamCallbackHandler?.pingCount;
const newData = this._f(this._upstream.data)
this._upstreamPingCount = this._upstreamCallbackHandler?.pingCount
if (this._data == newData) {
return;
return
}
this._data = newData;
this._callbacks.ping(this._data);
this._data = newData
this._callbacks.ping(this._data)
}
}
export class UIEventSource<T> extends Store<T> implements Writable<T> {
private static readonly pass: () => {};
public data: T;
_callbacks: ListenerTracker<T> = new ListenerTracker<T>();
private static readonly pass: () => {}
public data: T
_callbacks: ListenerTracker<T> = new ListenerTracker<T>()
constructor(data: T, tag: string = "") {
super(tag);
this.data = data;
super(tag)
this.data = data
}
public static flatten<X>(
source: Store<Store<X>>,
possibleSources?: Store<any>[]
): UIEventSource<X> {
const sink = new UIEventSource<X>(source.data?.data);
const sink = new UIEventSource<X>(source.data?.data)
source.addCallback((latestData) => {
sink.setData(latestData?.data);
sink.setData(latestData?.data)
latestData.addCallback((data) => {
if (source.data !== latestData) {
return true;
return true
}
sink.setData(data);
});
});
sink.setData(data)
})
})
for (const possibleSource of possibleSources ?? []) {
possibleSource?.addCallback(() => {
sink.setData(source.data?.data);
});
sink.setData(source.data?.data)
})
}
return sink;
return sink
}
/**
@ -586,16 +585,16 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
promise: Promise<T>,
onError: (e: any) => void = undefined
): UIEventSource<T> {
const src = new UIEventSource<T>(undefined);
promise?.then((d) => src.setData(d));
const src = new UIEventSource<T>(undefined)
promise?.then((d) => src.setData(d))
promise?.catch((err) => {
if (onError !== undefined) {
onError(err);
onError(err)
} else {
console.warn("Promise failed:", err);
console.warn("Promise failed:", err)
}
});
return src;
})
return src
}
/**
@ -607,10 +606,10 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
public static FromPromiseWithErr<T>(
promise: Promise<T>
): UIEventSource<{ success: T } | { error: any }> {
const src = new UIEventSource<{ success: T } | { error: any }>(undefined);
promise?.then((d) => src.setData({ success: d }));
promise?.catch((err) => src.setData({ error: err }));
return src;
const src = new UIEventSource<{ success: T } | { error: any }>(undefined)
promise?.then((d) => src.setData({ success: d }))
promise?.catch((err) => src.setData({ error: err }))
return src
}
/**
@ -627,17 +626,17 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
public static asInt(source: UIEventSource<string>): UIEventSource<number> {
return source.sync(
(str) => {
let parsed = parseInt(str);
return isNaN(parsed) ? undefined : parsed;
let parsed = parseInt(str)
return isNaN(parsed) ? undefined : parsed
},
[],
(fl) => {
if (fl === undefined || isNaN(fl)) {
return undefined;
return undefined
}
return ("" + fl);
return "" + fl
}
);
)
}
/**
@ -658,17 +657,17 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
public static asFloat(source: UIEventSource<string>): UIEventSource<number> {
return source.sync(
(str) => {
let parsed = parseFloat(str);
return isNaN(parsed) ? undefined : parsed;
let parsed = parseFloat(str)
return isNaN(parsed) ? undefined : parsed
},
[],
(fl) => {
if (fl === undefined || isNaN(fl)) {
return undefined;
return undefined
}
return ("" + fl);
return "" + fl
}
);
)
}
static asBoolean(stringUIEventSource: UIEventSource<string>) {
@ -676,7 +675,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
(str) => str === "true",
[],
(b) => "" + b
);
)
}
/**
@ -684,9 +683,9 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
* However, this value can be overriden without affecting source
*/
static feedFrom<T>(store: Store<T>): UIEventSource<T> {
const src = new UIEventSource(store.data);
store.addCallback((t) => src.setData(t));
return src;
const src = new UIEventSource(store.data)
store.addCallback((t) => src.setData(t))
return src
}
/**
@ -696,46 +695,46 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
* @param callback
*/
public addCallback(callback: (latestData: T) => boolean | void | any): () => void {
return this._callbacks.addCallback(callback);
return this._callbacks.addCallback(callback)
}
public addCallbackAndRun(callback: (latestData: T) => boolean | void | any): () => void {
const doDeleteCallback = callback(this.data);
const doDeleteCallback = callback(this.data)
if (doDeleteCallback !== true) {
return this.addCallback(callback);
return this.addCallback(callback)
} else {
return UIEventSource.pass;
return UIEventSource.pass
}
}
public addCallbackAndRunD(callback: (data: T) => void): () => void {
return this.addCallbackAndRun((data) => {
if (data !== undefined && data !== null) {
return callback(data);
return callback(data)
}
});
})
}
public addCallbackD(callback: (data: T) => void): () => void {
return this.addCallback((data) => {
if (data !== undefined && data !== null) {
return callback(data);
return callback(data)
}
});
})
}
public setData(t: T): UIEventSource<T> {
if (this.data == t) {
// MUST COMPARE BY REFERENCE!
return;
return
}
this.data = t;
this._callbacks.ping(t);
return this;
this.data = t
this._callbacks.ping(t)
return this
}
public ping(): void {
this._callbacks.ping(this.data);
this._callbacks.ping(this.data)
}
/**
@ -767,7 +766,7 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
extraSources: Store<any>[] = [],
onDestroy?: (f: () => void) => void
): Store<J> {
return new MappedStore(this, f, extraSources, this._callbacks, f(this.data), onDestroy);
return new MappedStore(this, f, extraSources, this._callbacks, f(this.data), onDestroy)
}
/**
@ -779,14 +778,14 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
this,
(t) => {
if (t === undefined) {
return undefined;
return undefined
}
return f(t);
return f(t)
},
extraSources,
this._callbacks,
this.data === undefined ? undefined : f(this.data)
);
)
}
/**
@ -803,53 +802,53 @@ export class UIEventSource<T> extends Store<T> implements Writable<T> {
g: (j: J, t: T) => T,
allowUnregister = false
): UIEventSource<J> {
const self = this;
const self = this
const stack = new Error().stack.split("\n");
const callee = stack[1];
const stack = new Error().stack.split("\n")
const callee = stack[1]
const newSource = new UIEventSource<J>(f(this.data), "map(" + this.tag + ")@" + callee);
const newSource = new UIEventSource<J>(f(this.data), "map(" + this.tag + ")@" + callee)
const update = function() {
newSource.setData(f(self.data));
return allowUnregister && newSource._callbacks.length() === 0;
};
const update = function () {
newSource.setData(f(self.data))
return allowUnregister && newSource._callbacks.length() === 0
}
this.addCallback(update);
this.addCallback(update)
for (const extraSource of extraSources) {
extraSource?.addCallback(update);
extraSource?.addCallback(update)
}
if (g !== undefined) {
newSource.addCallback((latest) => {
self.setData(g(latest, self.data));
});
self.setData(g(latest, self.data))
})
}
return newSource;
return newSource
}
public syncWith(otherSource: UIEventSource<T>, reverseOverride = false): UIEventSource<T> {
this.addCallback((latest) => otherSource.setData(latest));
const self = this;
otherSource.addCallback((latest) => self.setData(latest));
this.addCallback((latest) => otherSource.setData(latest))
const self = this
otherSource.addCallback((latest) => self.setData(latest))
if (reverseOverride) {
if (otherSource.data !== undefined) {
this.setData(otherSource.data);
this.setData(otherSource.data)
}
} else if (this.data === undefined) {
this.setData(otherSource.data);
this.setData(otherSource.data)
} else {
otherSource.setData(this.data);
otherSource.setData(this.data)
}
return this;
return this
}
set(value: T): void {
this.setData(value);
this.setData(value)
}
update(f: Updater<T> & ((value: T) => T)): void {
this.setData(f(this.data));
this.setData(f(this.data))
}
}