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

refactor: allow aux change direction while playing #1241

Merged
merged 1 commit into from
Oct 11, 2024
Merged

Conversation

cpvalente
Copy link
Owner

@cpvalente cpvalente commented Oct 4, 2024

Small improvements to the aux input

  • allow changing direction

Copy link
Contributor

coderabbitai bot commented Oct 4, 2024

Walkthrough

The pull request introduces several modifications across multiple files, primarily focusing on timer functionality and configuration. Key changes include the conversion of duration from seconds to milliseconds in the dispatchFromAdapter function, the introduction of a new auxTimerDefault property in the timer configuration, and enhancements to the SimpleTimer class to better manage its state. Additionally, the AuxTimerService class has been updated to integrate current time into various timer operations, affecting how timers are started, paused, and updated.

Changes

File Change Summary
apps/server/src/api-integration/integration.controller.ts Modified auxtimer action handler to convert duration from seconds to milliseconds for consistency in time representation.
apps/server/src/app.ts Added import for timerConfig, updated auxtimer1.duration and auxtimer1.current properties to use timerConfig.auxTimerDefault instead of hardcoded values.
apps/server/src/classes/simple-timer/SimpleTimer.ts Introduced initialDuration property, updated constructor to accept initialTime, modified setTime, setDirection, and stop methods to manage initial state and playback effectively.
apps/server/src/classes/simple-timer/__tests__/SimpleTimer.test.ts Updated tests for SimpleTimer to reflect changes in setDirection method, renamed test cases, and adjusted expected states accordingly.
apps/server/src/config/config.ts Added auxTimerDefault property to timerConfig, calculated as 5 * MILLIS_PER_MINUTE.
apps/server/src/services/aux-timer-service/AuxTimerService.ts Updated constructor to initialize SimpleTimer with timerConfig.auxTimerDefault, modified methods to include current time in timer operations.

Possibly related PRs

Suggested reviewers

  • alex-Arc

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (9)
apps/server/src/config/config.ts (1)

1-8: Request for additional context

The changes in this file seem to be laying the groundwork for the features mentioned in the PR objectives. However, the full implementation of allowing aux change direction while playing and editing aux input while running is not evident from these changes alone.

Could you provide more context on how these configuration changes fit into the broader implementation of these features? Are there other files that will be using this new auxTimerDefault configuration?

Additionally, since the PR mentions a work-in-progress feature (editing aux input while running), it would be helpful to understand if this PR is intended to be merged as is, or if more changes are expected before it's ready for final review.

apps/server/src/classes/simple-timer/__tests__/SimpleTimer.test.ts (6)

91-91: LGTM! Consider adding a comment for clarity.

The update to setDirection to include an initial time parameter is correct and aligns with the changes mentioned in the summary.

Consider adding a brief comment explaining the purpose of the 0 parameter for better readability:

// Set direction to count up, starting from 0
const initialState = timer.setDirection(SimpleDirection.CountUp, 0);

117-130: LGTM! Consider using a constant for consistency.

The changes to this test case accurately reflect the new behavior of maintaining the current time when changing direction. The additional setup steps provide a clear and consistent starting state.

For consistency with the rest of the test file, consider using the initialTime constant instead of the magic number 1000:

timer.setTime(initialTime);
// ...
expect(initialState.current).toBe(initialTime);
// ...
duration: initialTime,
current: initialTime,

132-146: LGTM! Minor inconsistency in variable usage.

The test correctly verifies the timer's behavior in count-up mode after multiple updates.

For consistency, replace initialTime with 1000 in line 135:

current: 1000 + 100,

This aligns with the use of 1000 in the rest of this test case.


148-162: LGTM! Consider adding a comment for clarity.

The test correctly verifies the timer's behavior when changing direction from count-up to count-down, including maintaining the current time and updating the duration.

Consider adding a brief comment explaining the significance of the 600 parameter in the setDirection call:

// Change direction to count down, passing current time (600ms since start)
newState = timer.setDirection(SimpleDirection.CountDown, 600);

This helps clarify why 600 is used and how it relates to the timer's state.


164-178: LGTM! Consider adding a comment for clarity.

The test correctly verifies the timer's behavior when changing direction back to count-up, including maintaining the current time, updating the duration, and continuing to count up correctly.

Consider adding a brief comment explaining the significance of the 700 parameter in the setDirection call:

// Change direction back to count up, passing current time (700ms since last direction change)
newState = timer.setDirection(SimpleDirection.CountUp, 700);

This helps clarify why 700 is used and how it relates to the timer's state.


Line range hint 1-178: Overall, the changes look good and thoroughly test the new functionality.

The updates to this test file accurately reflect and verify the new behavior of the SimpleTimer class, particularly the ability to change direction while maintaining the current time. The tests provide good coverage of various scenarios, including multiple direction changes and updates.

For future improvements, consider:

  1. Using constants for all magic numbers (e.g., 100, 500, 1500) to improve readability and maintainability.
  2. Adding a test case for edge scenarios, such as changing direction at time 0 or at the exact duration time.
apps/server/src/app.ts (1)

191-191: LGTM: Improved flexibility with configurable aux timer duration.

Replacing the hardcoded value with timerConfig.auxTimerDefault enhances maintainability and aligns with the PR's objectives.

Consider adding a comment explaining the purpose of auxTimerDefault for better code documentation.

apps/server/src/api-integration/integration.controller.ts (1)

233-235: Approve changes with a minor suggestion for consistency.

The conversion of duration from seconds to milliseconds is correct and aligns with the PR objective of improving aux input functionality. This change ensures consistency in time representation across the application.

For improved consistency with other parts of the codebase, consider using the MILLIS_PER_SECOND constant:

- const timeInMs = numberOrError(command.duration) * 1000;
+ const timeInMs = numberOrError(command.duration) * MILLIS_PER_SECOND;

This change would make the code more consistent with other time-related operations in the file (e.g., the addtime handler).

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 86e6f8b and a740f9c.

📒 Files selected for processing (6)
  • apps/server/src/api-integration/integration.controller.ts (1 hunks)
  • apps/server/src/app.ts (3 hunks)
  • apps/server/src/classes/simple-timer/SimpleTimer.ts (3 hunks)
  • apps/server/src/classes/simple-timer/tests/SimpleTimer.test.ts (2 hunks)
  • apps/server/src/config/config.ts (1 hunks)
  • apps/server/src/services/aux-timer-service/AuxTimerService.ts (3 hunks)
🔇 Additional comments (17)
apps/server/src/config/config.ts (2)

1-1: LGTM: Good use of utility constant

The import of MILLIS_PER_MINUTE from 'ontime-utils' is a good practice. It promotes code reusability and maintainability by using a centralized constant for time conversion.


8-8: Approved: New auxTimerDefault property

The addition of auxTimerDefault is a good improvement. It provides a clear default duration for the auxiliary timer, which enhances the configuration options.

However, could you please clarify how this change relates to the PR objective of "allowing aux change direction while playing"? The current implementation sets a default duration, but it's not clear how it enables changing direction during playback.

To ensure this change doesn't conflict with existing code, let's check for any other occurrences of auxiliary timer configuration:

✅ Verification successful

Verified: Auxiliary Timer Configurations

The search for other auxiliary timer configurations did not reveal any conflicts with the addition of auxTimerDefault. The property is consistently used across the codebase, and no conflicting configurations were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for other auxiliary timer configurations
rg --type typescript "aux.*timer" --glob '!apps/server/src/config/config.ts'

Length of output: 113


Script:

#!/bin/bash
# Description: Search for other auxiliary timer configurations in TypeScript files
rg --type ts "aux.*timer" --glob '!apps/server/src/config/config.ts'

Length of output: 4868

apps/server/src/app.ts (3)

51-51: LGTM: New import statement added correctly.

The new import for timerConfig is properly placed and follows the project's naming conventions.


51-51: Overall, the changes effectively support the PR objectives.

The modifications to import timerConfig and update the auxtimer1 initialization in eventStore.init are well-focused and align with the goal of improving aux input functionality. These changes enhance the flexibility of the aux timer feature while minimizing the risk of unintended side effects.

To ensure comprehensive testing of the new aux timer behavior, consider adding or updating relevant unit tests for the eventStore.init method and any components that interact with the aux timer.

Also applies to: 191-192


192-192: LGTM: Consistent update for aux timer current value.

Setting current to timerConfig.auxTimerDefault is consistent with the duration update and supports the PR's objectives.

Please confirm that starting the aux timer at its maximum value (counting down) is the intended behavior. If not, consider initializing current to a different value or adding a comment explaining the rationale.

To verify the aux timer initialization, you can run the following script:

apps/server/src/api-integration/integration.controller.ts (1)

Line range hint 236-243: Verify implementation of direction change feature.

The PR objectives mention allowing aux change direction while playing. The existing code handles direction changes, but it's not clear if any additional modifications are needed to allow changing direction during playback.

Please confirm if the current implementation fully satisfies the requirement of changing aux direction while playing. If not, consider what additional changes might be necessary to meet this objective.

apps/server/src/services/aux-timer-service/AuxTimerService.ts (7)

5-5: Import timerConfig

Good job importing timerConfig; this allows the service to use configurable timer defaults.


34-34: Pass current time to setDirection method

Including this.getTime() when calling this.timer.setDirection() ensures the timer's direction is updated accurately with the current time context.


Line range hint 41-41: Pass current time to start method

Providing this.getTime() to this.timer.start() ensures the timer starts accurately from the current time, maintaining synchronization.


Line range hint 49-49: Pass current time to pause method

Supplying this.getTime() to this.timer.pause() allows the timer to pause precisely at the current time.


Line range hint 62-62: Pass current time to update method

By passing this.getTime() to this.timer.update(), you ensure the timer updates based on the current time, enhancing time accuracy.


34-34: Ensure SimpleTimer methods accept currentTime parameter

You've updated the methods to pass this.getTime() to this.timer. Please verify that the corresponding methods in SimpleTimer are designed to accept the currentTime parameter and handle it appropriately.

Run the following script to confirm that SimpleTimer methods accept currentTime:

#!/bin/bash
# Description: Verify that `SimpleTimer` methods accept `currentTime` as a parameter.

# Test: Search for method definitions in `SimpleTimer` class.
# Expect: Methods have signatures accepting `currentTime` as a parameter.

ast-grep --lang typescript --pattern $'class SimpleTimer {
  $$$

  setDirection($_, $_): $_ {
    $$$
  }

  start($_): $_ {
    $$$
  }

  pause($_): $_ {
    $$$
  }

  update($_): $_ {
    $$$
  }

  $$$
}'

Also applies to: 41-41, 49-49, 62-62


17-17: Initialize SimpleTimer with default configuration

Initializing this.timer with timerConfig.auxTimerDefault ensures that the timer starts with the appropriate default settings. Please verify that auxTimerDefault is properly defined in the configuration and contains the expected initial values.

Run the following script to confirm that auxTimerDefault is defined:

✅ Verification successful

To further verify the definition of auxTimerDefault, please run the following script:


auxTimerDefault is properly defined in the configuration and contains the expected initial values.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `auxTimerDefault` is defined in `timerConfig`.

# Test: Search for the definition of `auxTimerDefault` in configuration files.
# Expect: At least one match defining `auxTimerDefault`.

rg 'auxTimerDefault' $(fd 'config.js')

Length of output: 211


Script:

#!/bin/bash
# Description: Verify that `auxTimerDefault` is defined in any configuration files.

# Test: Search for the definition of `auxTimerDefault` in various configuration files.
# Expect: At least one match defining `auxTimerDefault`.

rg 'auxTimerDefault' $(fd 'config*' -e js -e ts -e json)

Length of output: 661

apps/server/src/classes/simple-timer/SimpleTimer.ts (4)

12-17: Proper initialization of initialDuration and state properties

The introduction of the initialDuration property and its initialization along with state.duration and state.current in the constructor ensures that the timer maintains its initial settings correctly.


35-35: Update initialDuration in setTime method for consistency

Updating initialDuration when setTime is called keeps the initial state consistent with the updated duration.


40-47: Correctly resetting targets when changing direction during playback

Modifying setDirection to reset startedAt and update state.duration when the timer is playing ensures that the timer adjusts correctly to direction changes on the fly. Calling update(timeNow) reflects the changes immediately.


72-73: Resetting timer to initial duration in stop method

By resetting state.duration and state.current to initialDuration upon stopping, the timer correctly returns to its original state.

@cpvalente cpvalente requested a review from alex-Arc October 6, 2024 18:21
@cpvalente cpvalente merged commit 16fd44a into master Oct 11, 2024
4 checks passed
@cpvalente cpvalente deleted the improve-aux branch October 11, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants