Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: frontend: Add delay in one-more-time #2438

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 30 additions & 8 deletions core/frontend/src/one-more-time.ts
@@ -1,18 +1,42 @@
/**
* Used to configure the OneMoreTime instance
*/
export interface OneMoreTimeOptions {
/**
* Represents the value in ms that will be used to delay the next call
* Remember that this value is applied after the action is executed, it does
* not count the time the action takes to execute
* @type {number}
*/
delay?: number

/**
* When some error happens, this value in ms will be delayed to the next call
* @type {number}
*/
errorDelay?: number

/**
* If true, the action will be executed immediately after the object is created
* @type {boolean}
*/
autostart?: boolean
}

/**
* After multiple years and attempts
* This is a RAII class that takes advantage of the new disposable feature
* to create a promise that repeats itself until it's disposed
*/
class OneMoreTime {
export default class OneMoreTime {
private isDisposed = false

constructor(
private readonly action: () => Promise<void>,
private readonly timeout = 5000,
autostart = true,
private readonly options: OneMoreTimeOptions = {}
) {
// One more time
if (autostart) {
if (options.autostart ?? true) {
// eslint-disable-next-line no-return-await
(async () => await this.start())()
}
Expand All @@ -29,12 +53,10 @@ class OneMoreTime {
console.error('Error in self-calling promise:', error)
// Oh yeah, alright, don't stop the dancing
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, this.timeout))
await new Promise((resolve) => setTimeout(resolve, this.options.errorDelay))
}

// One more time, a celebration
// eslint-disable-next-line no-return-await
(async () => await this.start())()
setTimeout(() => this.start(), this.options.delay)
}

// Celebrate and dance so free
Expand Down