Created
June 14, 2022 19:09
-
-
Save ilyasakin/3b8b33319dbd9c6d6f1d166cf38e43ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { reactive } from 'vue'; | |
import { TLoadingKey } from '../model/loading.model'; | |
export default class LoadingService { | |
private readonly TIMEOUT: number = 10000; | |
private readonly timeouts: Map<TLoadingKey, NodeJS.Timeout> = reactive( | |
new Map<TLoadingKey, NodeJS.Timeout>(), | |
); | |
public readonly loadings: Set<TLoadingKey> = reactive(new Set<TLoadingKey>()); | |
public startLoading(key: TLoadingKey): void { | |
this.loadings.add(key); | |
const timeout: NodeJS.Timeout = setTimeout(() => { | |
this.loadings.delete(key); | |
}, this.TIMEOUT); | |
this.timeouts.set(key, timeout); | |
} | |
public stopLoading(key: TLoadingKey): void { | |
this.loadings.delete(key); | |
if (this.timeouts.has(key)) { | |
clearTimeout(this.timeouts.get(key)); | |
this.timeouts.delete(key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment