From 3d225655df43b64518e28a127111ee650d380b4e Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Wed, 27 Aug 2025 00:18:49 +0200 Subject: [PATCH] Refactoring(uieventsource): introduce 'once' --- src/Logic/Osm/OsmPreferences.ts | 3 +-- src/Logic/State/UserRelatedState.ts | 2 +- src/Logic/UIEventSource.ts | 9 +++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Logic/Osm/OsmPreferences.ts b/src/Logic/Osm/OsmPreferences.ts index db8dcfa27..e1dda7820 100644 --- a/src/Logic/Osm/OsmPreferences.ts +++ b/src/Logic/Osm/OsmPreferences.ts @@ -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 }) } diff --git a/src/Logic/State/UserRelatedState.ts b/src/Logic/State/UserRelatedState.ts index fe78cab3d..b7d93bc58 100644 --- a/src/Logic/State/UserRelatedState.ts +++ b/src/Logic/State/UserRelatedState.ts @@ -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 { diff --git a/src/Logic/UIEventSource.ts b/src/Logic/UIEventSource.ts index c4af67e6e..015715952 100644 --- a/src/Logic/UIEventSource.ts +++ b/src/Logic/UIEventSource.ts @@ -340,8 +340,13 @@ export abstract class Store implements Readable { 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()