core:frontend:OneMoreTime: Improve robustness#3227
Conversation
* Avoid double starts * Avoid unecessary resume and stops * Avoid empty action running * Make sure to kill timeouts when stop/dispose to avoid double invoking
Reviewer's Guide by SourceryThis pull request enhances the robustness of the Sequence diagram for OneMoreTime.start()sequenceDiagram
participant OMT as OneMoreTime
OMT->OMT: Check isDisposed, isPaused, isRunning
alt Not disposed, not paused, not running
alt action is not defined
OMT->OMT: console.warn('OneMoreTime: Started without an action, stopping execution')
else action is defined
OMT->OMT: this.isRunning = true
OMT->OMT: await this.action()
alt Error during action
OMT->OMT: await new Promise((resolve) => setTimeout(resolve, this.options.errorDelay))
end
OMT->OMT: this.isRunning = false
alt Not paused and not disposed
OMT->OMT: this.timeoutId = setTimeout(() => this.start(), this.options.delay)
end
end
end
Sequence diagram for OneMoreTime.resume()sequenceDiagram
participant OMT as OneMoreTime
OMT->OMT: Check isDisposed or not isPaused
alt Not disposed and isPaused
OMT->OMT: this.isPaused = false
OMT->OMT: this.start()
end
Updated class diagram for OneMoreTimeclassDiagram
class OneMoreTime {
-options: OneMoreTimeOptions
-action: Function
-isDisposed: boolean
-isPaused: boolean
-isRunning: boolean
-timeoutId: ReturnType<typeof setTimeout>
+constructor(options: OneMoreTimeOptions)
-killTask(): void
-watchDisposeWith(): void
-softStart(): void
+start(): Promise<void>
+stop(): void
+resume(): void
+Symbol.dispose(): void
}
note for OneMoreTime "Added isRunning and timeoutId attributes for state management and timeout handling."
note for OneMoreTime "Added killTask method to clear the timeout."
note for OneMoreTime "Added checks for isRunning, isDisposed and isPaused in start, stop and resume methods."
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @joaomariolago - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a state diagram to clarify the component's lifecycle and state transitions.
- It might be beneficial to use a more descriptive name than
killTaskto better reflect its purpose.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| /** | ||
| * Starts the action if `autostart` is enabled and an action is defined. | ||
| * @returns {void} | ||
| */ |
There was a problem hiding this comment.
issue (complexity): Consider consolidating duplicate state guard checks into a helper function and inlining the simple killTask logic where it’s used to reduce code duplication and improve readability .
Consider consolidating duplicate state guard checks into a helper function and inlining the simple killTask logic where it’s used. For example:
-
Create a helper to check if execution should proceed:
private canExecute(): boolean { return !this.isDisposed && !this.isPaused; }
-
Use it in your methods:
async start(): Promise<void> { if (!this.canExecute() || this.isRunning) return; if (!this.action) { console.warn('OneMoreTime: Started without an action, stopping execution'); return; } this.isRunning = true; try { await this.action(); } catch (error) { console.error('Error in self-calling promise:', error); this.options.onError?.(error); await new Promise(resolve => setTimeout(resolve, this.options.errorDelay)); } finally { this.isRunning = false; } if (this.canExecute()) { this.timeoutId = setTimeout(() => this.start(), this.options.delay); } }
-
Inline the simple
killTasklogic instopand disposal:stop(): void { if (!this.canExecute()) return; this.isPaused = true; if (this.timeoutId) { clearTimeout(this.timeoutId); this.timeoutId = undefined; } } [Symbol.dispose](): void { this.isDisposed = true; if (this.timeoutId) { clearTimeout(this.timeoutId); this.timeoutId = undefined; } }
These changes reduce the repetition of guard conditions and simplify the overall control flow while preserving all functionality.
Summary by Sourcery
Improve robustness of OneMoreTime class by adding state management, preventing double starts, and handling edge cases more gracefully
Bug Fixes:
Enhancements: