-
Notifications
You must be signed in to change notification settings - Fork 50
Add dedicated service for managing timers (setInterval and setTimeout)
#2298
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
Open
rafaellehmkuhl
wants to merge
3
commits into
bluerobotics:master
Choose a base branch
from
rafaellehmkuhl:create-dedicated-set-interval-timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
09e8503
timers: Add API to control managed setIntervals and setTimeouts
rafaellehmkuhl 4fe86dd
actions: Adapt JavaScript action editor to use the new managed timers
rafaellehmkuhl 0dcfcc3
widget: Adapt DIY widget code execution to use the new managed timers
rafaellehmkuhl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Timer management for cockpit | ||
| /** | ||
| * Information about a timer | ||
| */ | ||
| export interface TimerInfo { | ||
| /** | ||
| * The ID of the timer | ||
| */ | ||
| id: number | ||
| /** | ||
| * The type of the timer | ||
| */ | ||
| type: 'interval' | 'timeout' | ||
| } | ||
|
|
||
| /** | ||
| * Manager for Cockpit timers | ||
| * Handles the creation and clearing of timers for a specific owner, making sure that only one timer is running at a time for a given owner. | ||
| */ | ||
| export class CockpitTimerManager { | ||
| private currentOwnerId: string | null = null | ||
| private ownerTimers: Map<string, TimerInfo[]> = new Map() | ||
|
|
||
| /** | ||
| * Set the current owner ID | ||
| * @param {string} ownerId - The ID of the owner | ||
| */ | ||
| setCurrentOwnerId(ownerId: string): void { | ||
| this.currentOwnerId = ownerId | ||
| } | ||
|
|
||
| /** | ||
| * Clear the current owner ID | ||
| */ | ||
| clearCurrentOwnerId(): void { | ||
| this.currentOwnerId = null | ||
| } | ||
|
|
||
| /** | ||
| * Add a timer to an owner | ||
| * @param {string} ownerId - The ID of the owner | ||
| * @param {number} timerId - The ID of the timer | ||
| * @param {'interval' | 'timeout'} type - The type of the timer | ||
| */ | ||
| addTimer(ownerId: string, timerId: number, type: 'interval' | 'timeout'): void { | ||
| if (!this.ownerTimers.has(ownerId)) { | ||
| this.ownerTimers.set(ownerId, []) | ||
| } | ||
| this.ownerTimers.get(ownerId)!.push({ id: timerId, type }) | ||
| } | ||
|
|
||
| /** | ||
| * Clear all timers for an owner | ||
| * @param {string} ownerId - The ID of the owner | ||
| */ | ||
| clearAllTimersForOwner(ownerId: string): void { | ||
| console.log(`Clearing ${this.ownerTimers.get(ownerId)?.length ?? 0} timers for ${ownerId}.`) | ||
| const timers = this.ownerTimers.get(ownerId) | ||
| if (!timers) return | ||
|
|
||
| timers.forEach((timer) => { | ||
| if (timer.type === 'interval') { | ||
| clearInterval(timer.id) | ||
| } else { | ||
| clearTimeout(timer.id) | ||
| } | ||
| }) | ||
|
|
||
| this.ownerTimers.delete(ownerId) | ||
| } | ||
|
|
||
| /** | ||
| * Create a managed interval | ||
| * @param {Function} callback - The callback to execute | ||
| * @param {number} delay - The delay in milliseconds | ||
| * @param {string} ownerId - The ID of the owner | ||
| * @returns {number} The interval ID | ||
| */ | ||
| createManagedSetInterval(callback: () => void, delay?: number, ownerId?: string): number { | ||
| console.log(`Creating managed interval with ${delay}ms delay.`) | ||
| if (!ownerId && !this.currentOwnerId) { | ||
| throw new Error('No owner ID provided and no current owner ID set.') | ||
| } | ||
| const intervalId = window.setInterval(callback, delay) | ||
| this.addTimer((ownerId || this.currentOwnerId)!, intervalId, 'interval') | ||
| return intervalId | ||
| } | ||
|
|
||
| /** | ||
| * Create a managed timeout | ||
| * @param {Function} callback - The callback to execute | ||
| * @param {number} delay - The delay in milliseconds | ||
| * @param {string} ownerId - The ID of the owner | ||
| * @returns {number} The timeout ID | ||
| */ | ||
| createManagedSetTimeout(callback: () => void, delay?: number, ownerId?: string): number { | ||
| console.log(`Creating managed timeout with ${delay}ms delay.`) | ||
| if (!ownerId && !this.currentOwnerId) { | ||
| throw new Error('No owner ID provided and no current owner ID set.') | ||
| } | ||
| const timeoutId = window.setTimeout(callback, delay) | ||
| this.addTimer((ownerId || this.currentOwnerId)!, timeoutId, 'timeout') | ||
| return timeoutId | ||
| } | ||
| } | ||
|
|
||
| const cockpitTimerManager = new CockpitTimerManager() | ||
|
|
||
| // Export the timer manager for use in action execution | ||
| export { cockpitTimerManager } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good if we can match the interface for the real ones - returning ID values that allow user code to clear them if they want to (since that could be relevant to complex functionality).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree.