Refactoring(uieventsource): introduce 'once'

This commit is contained in:
Pieter Vander Vennet 2025-08-27 00:18:49 +02:00
parent cac87a5467
commit 3d225655df
3 changed files with 9 additions and 5 deletions

View file

@ -34,9 +34,8 @@ export class OsmPreferences {
this.auth = auth
this._fakeUser = fakeUser
this.osmConnection = osmConnection
osmConnection.userDetails.addCallbackAndRunD(() => {
osmConnection.userDetails.once(() => {
this.loadBulkPreferences()
return true
})
}

View file

@ -389,7 +389,7 @@ export default class UserRelatedState {
*/
public addUnofficialTheme(themeInfo: MinimalThemeInformation) {
const pref = this.osmConnection.getPreference("unofficial-theme-" + themeInfo.id)
this.osmConnection.isLoggedIn.when(() => pref.set(JSON.stringify(themeInfo)))
this.osmConnection.isLoggedIn.once(() => pref.set(JSON.stringify(themeInfo)))
}
public getUnofficialTheme(id: string): MinimalThemeInformation | undefined {

View file

@ -340,8 +340,13 @@ export abstract class Store<T> implements Readable<T> {
public abstract destroy()
when(callback: () => void, condition?: (v: T) => boolean) {
condition ??= (v) => v === true
/**
* Execute `f` once, but only if the value within is true-ish (or the explicit 'condition' is met)
* @param callback
* @param condition
*/
public once(callback: () => void, condition?: (v: T) => boolean) {
condition ??= (v) => !!v
this.addCallbackAndRunD((v) => {
if (condition(v)) {
callback()