forked from MapComplete/MapComplete
Refactoring(uieventsource): introduce 'once'
This commit is contained in:
parent
cac87a5467
commit
3d225655df
3 changed files with 9 additions and 5 deletions
|
|
@ -34,9 +34,8 @@ export class OsmPreferences {
|
||||||
this.auth = auth
|
this.auth = auth
|
||||||
this._fakeUser = fakeUser
|
this._fakeUser = fakeUser
|
||||||
this.osmConnection = osmConnection
|
this.osmConnection = osmConnection
|
||||||
osmConnection.userDetails.addCallbackAndRunD(() => {
|
osmConnection.userDetails.once(() => {
|
||||||
this.loadBulkPreferences()
|
this.loadBulkPreferences()
|
||||||
return true
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -389,7 +389,7 @@ export default class UserRelatedState {
|
||||||
*/
|
*/
|
||||||
public addUnofficialTheme(themeInfo: MinimalThemeInformation) {
|
public addUnofficialTheme(themeInfo: MinimalThemeInformation) {
|
||||||
const pref = this.osmConnection.getPreference("unofficial-theme-" + themeInfo.id)
|
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 {
|
public getUnofficialTheme(id: string): MinimalThemeInformation | undefined {
|
||||||
|
|
|
||||||
|
|
@ -340,8 +340,13 @@ export abstract class Store<T> implements Readable<T> {
|
||||||
|
|
||||||
public abstract destroy()
|
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) => {
|
this.addCallbackAndRunD((v) => {
|
||||||
if (condition(v)) {
|
if (condition(v)) {
|
||||||
callback()
|
callback()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue