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: dev label #1143

Merged
merged 3 commits into from
Jul 19, 2024
Merged

refactor: dev label #1143

merged 3 commits into from
Jul 19, 2024

Conversation

cpvalente
Copy link
Owner

@cpvalente cpvalente commented Jul 16, 2024

the main objective of this PR is to simplify the code around updating the timer by removing code paths specific to roll

Making the functions agnostic of the play mode means

  • easier maintenance of timer logic
  • integration events "just work"

on the downside, we would need to consider that there is more logic involve for any of the code paths

We would need a separate PR to tidy up the behaviour around roll. I am not aware of any change of behaviour introduced by this PR

Copy link
Contributor

coderabbitai bot commented Jul 16, 2024

Walkthrough

The updates significantly refine the TimerService and related timer logic, introducing callback functions for timer updates, enhancing error handling, and eliminating outdated functionalities. The modifications also reorganize timer-related tests and implement improved logic for managing timer phases, event transitions, and state updates, ensuring a more coherent and robust timing system.

Changes

File(s) Change Summary
.../TimerService.ts Introduced UpdateCallbackFn, modified initialization for endCallback and onUpdateCallback, removed onUpdateCallback from the constructor, added setOnUpdateCallback method, improved error handling in addTime, and updated update method.
.../timerUtils.test.ts, .../timerUtils.ts Removed updateRoll function, updated getRuntimeOffset and getTimerPhase by eliminating secondaryTarget, set offset to 0, and exported isPlaybackActive function.
.../RuntimeService.ts Updated constructor to accept timerService, enhanced checkTimerUpdate logic, set onUpdateCallback in init, exported runtimeService, and added broadcastResult decorator function.
.../runtimeState.test.ts, .../runtimeState.ts Modified initial state definitions for Runtime and TimerState, refined comments, and enhanced logic in functions like clear, updateRundownData, start, update, and roll.
.../development.ts Introduced shouldCrashDev function to handle critical checks in the development environment, providing a mechanism for controlled crashes based on specified conditions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RuntimeService
    participant TimerService
    participant TimerState

    User->>RuntimeService: start timer
    RuntimeService->>TimerService: set and start timer
    TimerService->>TimerState: update initial state
    TimerService->>RuntimeService: onUpdateCallback invoked periodically
    
    loop Every update interval
        TimerService->>RuntimeService: checkTimerUpdate
        RuntimeService->>TimerState: update timer state
    end
    
    User->>RuntimeService: stop timer
    RuntimeService->>TimerService: stop timer
    TimerService->>TimerState: finalize state
Loading

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c5e821d and fd3fe6f.

Files ignored due to path filters (1)
  • apps/server/package.json is excluded by none and included by none
Files selected for processing (4)
  • apps/server/src/app.ts (1 hunks)
  • apps/server/src/classes/data-provider/DataProvider.ts (1 hunks)
  • apps/server/src/services/app-state-service/AppStateService.ts (2 hunks)
  • apps/server/src/utils/development.ts (1 hunks)
Additional context used
Biome
apps/server/src/services/app-state-service/AppStateService.ts

[error] 31-31: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

apps/server/src/classes/data-provider/DataProvider.ts

[error] 28-28: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

Additional comments not posted (2)
apps/server/src/utils/development.ts (1)

9-20: Review of the shouldCrashDev function

  1. Correctness & Logic: The function correctly checks if it's in production and exits early if so, which is a good practice for development-only features. The condition to crash (check) is straightforward and the error handling (logging and exiting) is appropriate for a development environment.

  2. Performance: Since this function is only called under specific conditions (not in production and when check is true), its performance impact is minimal.

  3. Best Practices: The function is well-documented with a clear description of its purpose and behavior. However, the use of process.exit(2) is generally discouraged in Node.js applications because it does not allow the Node.js event loop to clear existing tasks before terminating. It's better to handle the exit more gracefully if possible.

  4. Error Handling: The function handles errors by logging them and exiting, which is suitable for a crash function in a development environment.

  5. Potential Improvement: Consider adding a mechanism to handle graceful shutdowns where resources are cleaned up properly before exiting.

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

266-266: Clear Documentation of Exit Codes

The added comment clearly explains the meaning of exit code 2 in the context of a development crash. This is a good practice as it helps maintain clarity and understanding of the code's behavior, especially during debugging and maintenance phases.

}
}
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential Issue with Label Usage

The use of a label (DEV:) here is unconventional and flagged by static analysis as potentially confusing. Labels are typically used with loops in JavaScript, not with function calls.

Suggestion: If the intent was to mark this section of code as development-specific, consider using a more conventional approach such as wrapping the code in an if-statement that checks if the environment is development.

- DEV: shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
+ if (!isProduction) {
+   shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
DEV: shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
if (!isProduction) {
shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
}
Tools
Biome

[error] 31-31: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

}
}
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential Issue with Label Usage

Similar to the previous file, the use of a label (DEV:) here is unconventional and flagged by static analysis as potentially confusing. Labels are typically used with loops in JavaScript, not with function calls.

Suggestion: If the intent was to mark this section of code as development-specific, consider using a more conventional approach such as wrapping the code in an if-statement that checks if the environment is development.

- DEV: shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
+ if (!isProduction) {
+   shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
DEV: shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
if (!isProduction) {
shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
}
Tools
Biome

[error] 28-28: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fd3fe6f and 9ef85ac.

Files ignored due to path filters (1)
  • packages/types/src/definitions/runtime/TimerState.type.ts is excluded by none and included by none
Files selected for processing (6)
  • apps/server/src/services/TimerService.ts (4 hunks)
  • apps/server/src/services/tests/timerUtils.test.ts (14 hunks)
  • apps/server/src/services/runtime-service/RuntimeService.ts (6 hunks)
  • apps/server/src/services/timerUtils.ts (5 hunks)
  • apps/server/src/stores/tests/runtimeState.test.ts (1 hunks)
  • apps/server/src/stores/runtimeState.ts (9 hunks)
Files skipped from review due to trivial changes (1)
  • apps/server/src/stores/tests/runtimeState.test.ts
Additional context used
Biome
apps/server/src/services/TimerService.ts

[error] 88-88: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 88-88: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Additional comments not posted (16)
apps/server/src/services/TimerService.ts (4)

8-8: Approved: New type alias for update callbacks.

The addition of UpdateCallbackFn type alias improves type safety and clarity in callback handling.


19-19: Approved: Initialization of callback properties to undefined.

Initializing endCallback and onUpdateCallback as undefined is a good practice, making the state management more predictable.

Also applies to: 21-21


40-42: Approved: Method for setting update callbacks.

The setOnUpdateCallback method enhances modularity by allowing dynamic assignment of the update callback function.


104-104: Approved: Use of optional chaining for callback invocation.

Using optional chaining (?.) for invoking onUpdateCallback ensures that the code does not throw if the callback is not set.

apps/server/src/services/__tests__/timerUtils.test.ts (5)

Line range hint 1366-1407: Refactored getRuntimeOffset function.

The changes to getRuntimeOffset remove unnecessary properties and simplify the state management. This aligns with the PR objectives.


Line range hint 1699-1738: Updated getTimerPhase function.

The removal of the secondaryTarget property simplifies the function and reduces complexity, which is beneficial for maintenance and future modifications.


Line range hint 1-1738: Removal of updateRoll function.

The function has been completely removed as intended. This should help in simplifying the timer update logic as per the PR objectives. Make sure to verify that no residual calls or dependencies are left in the codebase.

Verification successful

Verification successful: The updateRoll function has been completely removed from the codebase, and there are no residual calls or dependencies left.

  • No occurrences of updateRoll were found in the codebase.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `updateRoll` is not called anywhere in the codebase.

# Test: Search for the function usage. Expect: No occurrences.
rg --type typescript -A 5 $'updateRoll'

Length of output: 73


Script:

#!/bin/bash
# Description: Verify that `updateRoll` is not called anywhere in the codebase.

# Test: Search for the function usage. Expect: No occurrences.
rg --type ts -A 5 'updateRoll'

Length of output: 30


Line range hint 1-1738: Review of getExpectedFinish function and its tests.

The function and its tests appear to be correctly implemented. Ensure that the changes in other parts of the code do not indirectly affect this function's behavior.

Verification successful

Verification of getExpectedFinish function and its tests

The getExpectedFinish function and its tests are correctly implemented and interact as expected with other parts of the codebase.

  • Instances of getExpectedFinish were found in runtimeState.ts and timerUtils.test.ts.
  • The interactions with getRuntimeOffset and getTimerPhase are consistent with the expected functionality.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that changes in `getRuntimeOffset` and `getTimerPhase` do not affect `getExpectedFinish`.

# Test: Search for mentions of `getExpectedFinish` in the context of the modified functions. Expect: No unintended interactions.
rg --type typescript -A 5 $'getExpectedFinish'

Length of output: 80


Script:

#!/bin/bash
# Description: Verify that changes in `getRuntimeOffset` and `getTimerPhase` do not affect `getExpectedFinish`.

# Test: Search for mentions of `getExpectedFinish` in the context of the modified functions. Expect: No unintended interactions.
rg --type ts -A 5 $'getExpectedFinish'

Length of output: 10927


Line range hint 1-1738: Review of getCurrent function and its tests.

The function and its tests are correctly implemented. Ensure that the changes in other parts of the code do not indirectly affect this function's behavior.

Verification successful

Ensure getRuntimeOffset and getTimerPhase do not affect getCurrent.

The getCurrent function is used in conjunction with getRuntimeOffset in several places. After reviewing the code, there are no unintended interactions between these functions. The tests for getCurrent cover various scenarios, ensuring its correctness.

  • apps/server/src/stores/runtimeState.ts
  • apps/server/src/services/timerUtils.ts
  • apps/server/src/services/__tests__/timerUtils.test.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that changes in `getRuntimeOffset` and `getTimerPhase` do not affect `getCurrent`.

# Test: Search for mentions of `getCurrent` in the context of the modified functions. Expect: No unintended interactions.
rg --type typescript -A 5 $'getCurrent'

Length of output: 73


Script:

#!/bin/bash
# Description: Verify that changes in `getRuntimeOffset` and `getTimerPhase` do not affect `getCurrent`.

# Test: Search for mentions of `getCurrent` in the context of the modified functions. Expect: No unintended interactions.
rg --type ts -A 5 $'getCurrent'

Length of output: 10750

apps/server/src/services/timerUtils.ts (3)

327-327: Export of isPlaybackActive function approved.

Ensure that the newly exported isPlaybackActive function is being used appropriately in other modules.

Verification successful

Export of isPlaybackActive function approved.

The isPlaybackActive function is being used appropriately in other modules, confirming the export change is correct.

  • apps/server/src/stores/runtimeState.ts: Usage of isPlaybackActive.
  • apps/server/src/services/timerUtils.ts: Original function definition and internal usage.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for usage of `isPlaybackActive` across the codebase.

rg --type ts "isPlaybackActive" "apps/server/src"

Length of output: 363


346-346: Update in getTimerPhase function approved.

The addition of a check for state.timer.secondaryTimer not being null enhances the function's robustness. Verify its impact on the timer's behavior across different scenarios.

Verification successful

Update in getTimerPhase function approved.

The addition of a check for state.timer.secondaryTimer not being null enhances the function's robustness. The existing tests in apps/server/src/services/__tests__/timerUtils.test.ts cover various scenarios, ensuring the function's behavior is validated.

  • apps/server/src/services/__tests__/timerUtils.test.ts: Contains tests for getTimerPhase function.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Test the `getTimerPhase` function across different scenarios to ensure robust behavior.

# Placeholder for a more detailed test script.
echo "Testing `getTimerPhase` function..."

Length of output: 148


Script:

#!/bin/bash
# Locate the definition of the `getTimerPhase` function in the codebase.
ast-grep --lang typescript --pattern 'function getTimerPhase($_) { $$$ }'

# Find all usages of the `getTimerPhase` function to understand its impact.
rg 'getTimerPhase' -A 5

Length of output: 6120


Line range hint 13-13: Verify removal of timerConfig import.

Ensure that timerConfig is no longer used in this file. If it is still referenced, this could lead to runtime errors.

Verification successful

Line range hint 13-13: Verified removal of timerConfig import.

The timerConfig import has been correctly removed from timerUtils.ts as there are no references to it in this file. However, timerConfig is still in use in other parts of the codebase.

  • apps/server/src/stores/runtimeState.ts
  • apps/server/src/services/TimerService.ts
  • apps/server/src/services/runtime-service/rundownService.utils.ts
  • apps/server/src/services/runtime-service/RuntimeService.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any remaining usage of `timerConfig` in `timerUtils.ts`.

rg --type ts "timerConfig" "apps/server/src/services/timerUtils.ts"

Length of output: 65


Script:

#!/bin/bash
# Search for `timerConfig` in the entire codebase to ensure it is not being used elsewhere.

# Search in the config.js file
rg --type js "timerConfig" "apps/server/src/config/config.js"

# Search in the broader codebase
rg --type ts "timerConfig"

Length of output: 2161

apps/server/src/stores/runtimeState.ts (2)

18-24: Clarify the purpose of property changes and validate logic.

The added comments help clarify the conditions under which properties change, which is beneficial for maintainability. However, ensure that the logic changes, especially around expectedFinish and forceFinish, are thoroughly tested to prevent bugs.

Also applies to: 29-38, 51-51

Verification successful

Verification successful for property changes in runtimeState.ts.

The logic changes around expectedFinish and forceFinish are handled correctly across the codebase. The added comments help clarify the conditions under which these properties change, which is beneficial for maintainability.

  • expectedFinish and forceFinish are used in various files, including runtimeState.ts, TimerService.ts, timerUtils.ts, and several test files.
  • The logic for these properties appears to be implemented correctly, with no issues found in the search results.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the logic changes to properties in runtimeState.ts.

# Test: Search for usages of `expectedFinish` and `forceFinish` to ensure they are handled correctly.
rg --type typescript -A 5 $'expectedFinish|forceFinish'

Length of output: 91


Script:

#!/bin/bash
# Description: Verify the logic changes to properties in runtimeState.ts.

# Test: Search for usages of `expectedFinish` and `forceFinish` to ensure they are handled correctly.
rg --type ts -A 5 'expectedFinish|forceFinish'

Length of output: 29190


373-426: Review updates to the update function for potential issues.

The update function has undergone significant changes. Ensure that the new logic, especially the handling of different playback states and the new updateIfWaitingToRoll function, integrates well with the rest of the system and does not introduce regressions.

-    runtimeState.timer.secondaryTimer = targetTime - runtimeState.clock;
+    // Ensure that secondaryTimer is not set to a negative value
+    runtimeState.timer.secondaryTimer = Math.max(0, targetTime - runtimeState.clock);
Verification successful

Ensure secondaryTimer is not set to a negative value.

The update function is used in various parts of the codebase, including tests and service files, indicating that it is well-integrated. The proposed change to ensure secondaryTimer is not set to a negative value is a valid improvement and should be implemented.

  • File: apps/server/src/stores/runtimeState.ts
  • Lines: 373-426
  • Change:
    -    runtimeState.timer.secondaryTimer = targetTime - runtimeState.clock;
    +    // Ensure that secondaryTimer is not set to a negative value
    +    runtimeState.timer.secondaryTimer = Math.max(0, targetTime - runtimeState.clock);
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the new logic in the `update` function.

# Test: Search for usages of the `update` function to ensure it is called correctly.
rg --type typescript -A 5 $'update'

Length of output: 69


Script:

#!/bin/bash
# Description: Verify the new logic in the `update` function.

# Test: Search for usages of the `update` function to ensure it is called correctly.
rg --type-add 'ts:*.{ts,tsx}' -A 5 update

Length of output: 129220

apps/server/src/services/runtime-service/RuntimeService.ts (2)

42-50: Validate new properties and logic in RuntimeService.

The new properties and the added comments improve clarity, which is good. However, ensure that the new logic for handling timer updates is correct and does not introduce any unintended side effects.

Verification successful

The new properties and logic in RuntimeService are correctly implemented.

The previousTimerValue property is properly initialized, used in conditional checks, and updated with the current timer value. The added comments help in understanding its purpose.

  • RuntimeService.ts:
    • Initialization: RuntimeService.previousTimerValue = -1;
    • Conditional check: getShouldTimerUpdate(RuntimeService.previousTimerValue, state.timer.current);
    • Update: RuntimeService.previousTimerValue = state.timer.current;
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify new properties and logic in RuntimeService.ts.

# Test: Search for usages of `previousTimerValue` to ensure it is handled correctly.
rg --type typescript -A 5 $'previousTimerValue'

Length of output: 81


Script:

#!/bin/bash
# Description: Verify new properties and logic in RuntimeService.ts.

# Test: Search for usages of `previousTimerValue` to ensure it is handled correctly.
rg --type ts -A 5 'previousTimerValue'

Length of output: 2478


Line range hint 54-143: Thoroughly review new functions and modifications in RuntimeService.

The modifications and new functions in RuntimeService are crucial for the service's functionality. Ensure that the checkTimerUpdate function and the new broadcastResult decorator are implemented correctly and efficiently to avoid performance issues.

-    const shouldUpdateTimer = getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current);
+    // Ensure that the timer is only updated when necessary to avoid performance issues
+    const shouldUpdateTimer = this.lastIntegrationTimerValue !== newState.timer.current && getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current);
Verification successful

Verify the optimization in shouldUpdateTimer condition

The checkTimerUpdate function and the broadcastResult decorator are correctly implemented and used within the RuntimeService. The suggested change to the shouldUpdateTimer condition is a valid optimization to ensure the timer is only updated when necessary.

  • File: apps/server/src/services/runtime-service/RuntimeService.ts
  • Line: 98
-    const shouldUpdateTimer = getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current);
+    const shouldUpdateTimer = this.lastIntegrationTimerValue !== newState.timer.current && getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current);
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of new functions in RuntimeService.ts.

# Test: Search for usages of `checkTimerUpdate` and `broadcastResult` to ensure they are implemented correctly.
rg --type typescript -A 5 $'checkTimerUpdate|broadcastResult'

Length of output: 97


Script:

#!/bin/bash
# Description: Verify the implementation of new functions in RuntimeService.ts.

# Test: Search for usages of `checkTimerUpdate` and `broadcastResult` to ensure they are implemented correctly.
rg -t ts -A 5 'checkTimerUpdate|broadcastResult'

Length of output: 5763

Comment on lines 87 to 91
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.timer.expectedFinish === null) {
consoleError('TimerService.addTime: expectedFinish is negative');
throw new Error('TimerService.addTime: expectedFinish is negative');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Issue: Misuse of labels and potential error handling flaw.

The label DEV: is not used correctly here, which could lead to confusion and maintenance issues. Also, the error message and the condition do not align since the condition checks for null but the error message states "negative."

- DEV: {
-   if (state.timer.expectedFinish === null) {
+   if (state.timer.expectedFinish < 0) {
    consoleError('TimerService.addTime: expectedFinish is negative');
    throw new Error('TimerService.addTime: expectedFinish is negative');
- }
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.timer.expectedFinish === null) {
consoleError('TimerService.addTime: expectedFinish is negative');
throw new Error('TimerService.addTime: expectedFinish is negative');
}
}
if (state.timer.expectedFinish < 0) {
consoleError('TimerService.addTime: expectedFinish is negative');
throw new Error('TimerService.addTime: expectedFinish is negative');
}
Tools
Biome

[error] 88-88: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 88-88: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Comment on lines 100 to 105
// console.log(
// state,
// previousTime,
// skipLimit,
// hasSkipped && (adjustedClock > adjustedExpectedFinish || adjustedClock < startedAt),
// );
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider removing or controlling the commented-out debug log.

Leaving commented-out code can clutter the codebase. It's better to either remove these lines or implement a logging level control to enable/disable logs dynamically.

-  // console.log(
-  //   state,
-  //   previousTime,
-  //   skipLimit,
-  //   hasSkipped && (adjustedClock > adjustedExpectedFinish || adjustedClock < startedAt),
-  // );
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// console.log(
// state,
// previousTime,
// skipLimit,
// hasSkipped && (adjustedClock > adjustedExpectedFinish || adjustedClock < startedAt),
// );

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: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9ef85ac and d8781cb.

Files ignored due to path filters (1)
  • packages/types/src/definitions/runtime/TimerState.type.ts is excluded by none and included by none
Files selected for processing (6)
  • apps/server/src/services/TimerService.ts (4 hunks)
  • apps/server/src/services/tests/timerUtils.test.ts (14 hunks)
  • apps/server/src/services/runtime-service/RuntimeService.ts (6 hunks)
  • apps/server/src/services/timerUtils.ts (8 hunks)
  • apps/server/src/stores/tests/runtimeState.test.ts (1 hunks)
  • apps/server/src/stores/runtimeState.ts (9 hunks)
Files skipped from review as they are similar to previous changes (4)
  • apps/server/src/services/tests/timerUtils.test.ts
  • apps/server/src/services/runtime-service/RuntimeService.ts
  • apps/server/src/stores/tests/runtimeState.test.ts
  • apps/server/src/stores/runtimeState.ts
Additional context used
GitHub Check: unit-test
apps/server/src/services/TimerService.ts

[failure] 6-6:
'consoleError' is defined but never used. Allowed unused vars must match /^_/u

Biome
apps/server/src/services/TimerService.ts

[error] 88-88: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 88-88: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

apps/server/src/services/timerUtils.ts

[error] 59-59: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 59-59: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 97-97: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 97-97: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 272-272: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 272-272: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Additional comments not posted (5)
apps/server/src/services/TimerService.ts (5)

8-8: Approved: New type alias for update callbacks.

This new type alias UpdateCallbackFn simplifies the type definitions across the service, making the code cleaner and easier to maintain.


19-19: Approved: Initialization of endCallback to undefined.

Initializing endCallback to undefined is a good practice as it explicitly states that the timer is not set initially. This change improves readability and state management.


21-21: Approved: Change in onUpdateCallback to use the new type alias and be optionally undefined.

This change not only uses the newly defined type alias but also makes the callback optional, which enhances flexibility in how the timer updates are handled.


40-42: Approved: Addition of setOnUpdateCallback method.

Adding a method to set the update callback is a clean way to manage this functionality, allowing for dynamic changes to the callback handling during runtime.


103-103: Approved: Use of optional chaining for callback invocation.

The use of optional chaining (?.) here is appropriate for calling onUpdateCallback as it safely handles cases where the callback might not be set.

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: 4

Outside diff range, codebase verification and nitpick comments (1)
apps/server/src/services/timerUtils.ts (1)

Line range hint 10-32: Add a null check for duration to prevent potential runtime errors.

The function uses duration! which assumes duration is not null. It's safer to add a check before using it.

- const expectedFinish = startedAt + duration! + addedTime + pausedTime;
+ if (duration === null) throw new Error('Duration must be set');
+ const expectedFinish = startedAt + duration + addedTime + pausedTime;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d8781cb and 7ac9d69.

Files ignored due to path filters (1)
  • packages/types/src/definitions/runtime/TimerState.type.ts is excluded by none and included by none
Files selected for processing (6)
  • apps/server/src/services/TimerService.ts (4 hunks)
  • apps/server/src/services/tests/timerUtils.test.ts (14 hunks)
  • apps/server/src/services/runtime-service/RuntimeService.ts (6 hunks)
  • apps/server/src/services/timerUtils.ts (8 hunks)
  • apps/server/src/stores/tests/runtimeState.test.ts (1 hunks)
  • apps/server/src/stores/runtimeState.ts (9 hunks)
Files skipped from review as they are similar to previous changes (4)
  • apps/server/src/services/tests/timerUtils.test.ts
  • apps/server/src/services/runtime-service/RuntimeService.ts
  • apps/server/src/stores/tests/runtimeState.test.ts
  • apps/server/src/stores/runtimeState.ts
Additional context used
Biome
apps/server/src/services/TimerService.ts

[error] 87-87: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 87-87: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

apps/server/src/services/timerUtils.ts

[error] 59-59: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 59-59: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 97-97: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 97-97: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 272-272: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 272-272: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Additional comments not posted (11)
apps/server/src/services/TimerService.ts (5)

7-7: Type alias UpdateCallbackFn correctly defined.

The new type alias for update callbacks is correctly defined and aligns with the PR objectives to simplify callback handling.


18-18: Proper initialization of endCallback property.

Changing the endCallback property to allow undefined improves safety and initialization clarity.


20-20: Initialization of onUpdateCallback with undefined is appropriate.

This change aligns with the new UpdateCallbackFn type and allows the property to be safely uninitialized, enhancing code safety and clarity.


102-102: Proper use of optional chaining in update.

Using optional chaining for onUpdateCallback ensures that the method call is safe even if the callback is not set, aligning with the PR's goal to enhance robustness.


39-41: Method setOnUpdateCallback correctly implemented.

The addition of setOnUpdateCallback simplifies the management of update callbacks and uses the new UpdateCallbackFn type correctly.

Please ensure this method is utilized properly wherever the timer updates are handled.

Verification successful

Method setOnUpdateCallback correctly implemented and utilized.

The addition of setOnUpdateCallback simplifies the management of update callbacks and uses the new UpdateCallbackFn type correctly. The method is properly utilized in RuntimeService.ts.

  • apps/server/src/services/runtime-service/RuntimeService.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of `setOnUpdateCallback` in the codebase.

# Test: Search for the method usage. Expect: Proper usage in relevant parts of the code.
rg --type typescript -A 5 $'setOnUpdateCallback'

Length of output: 82


Script:

#!/bin/bash
# Description: Verify usage of `setOnUpdateCallback` in the codebase.

# Test: Search for the method usage. Expect: Proper usage in relevant parts of the code.
rg --type ts -A 5 $'setOnUpdateCallback'

Length of output: 960

apps/server/src/services/timerUtils.ts (6)

6-6: Function normaliseEndTime is implemented correctly.

This function handles the adjustment of end times for events that span over midnight effectively.


Line range hint 103-210: Function getRollTimers handles complex conditions effectively.

This function manages to determine the current and next events based on the time and event properties, handling midnight spans and public visibility efficiently.

Tools
Biome

[error] 97-97: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 97-97: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


Line range hint 280-290: Function getTotalDuration is implemented correctly.

This function accurately calculates the total duration of a time span, considering events that span across midnight.

Tools
Biome

[error] 272-272: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 272-272: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


330-330: Function getExpectedEnd is implemented correctly.

This function properly calculates the expected end of the rundown, ensuring all necessary conditions are met before performing the calculation.


341-341: Function isPlaybackActive is implemented correctly.

This function accurately determines if the playback is considered active based on the timer state.


360-360: Function getTimerPhase handles complex conditions effectively.

This function manages to determine the current phase of the timer based on various conditions and states, using the isPlaybackActive function effectively.

Comment on lines +88 to +90
if (state.timer.expectedFinish === null) {
throw new Error('TimerService.addTime: expectedFinish is negative');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Misalignment between condition and error message in addTime.

The error handling in addTime checks for null but logs an error for a negative value, which is misleading.

- if (state.timer.expectedFinish === null) {
+ if (state.timer.expectedFinish < 0) {
  consoleError('TimerService.addTime: expectedFinish is negative');
  throw new Error('TimerService.addTime: expectedFinish is negative');
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (state.timer.expectedFinish === null) {
throw new Error('TimerService.addTime: expectedFinish is negative');
}
if (state.timer.expectedFinish < 0) {
consoleError('TimerService.addTime: expectedFinish is negative');
throw new Error('TimerService.addTime: expectedFinish is negative');
}

@cpvalente cpvalente requested a review from alex-Arc July 17, 2024 12:15
Copy link
Collaborator

@alex-Arc alex-Arc left a comment

Choose a reason for hiding this comment

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

Should roll trigger a load and start dispatch when it rolls an event?
Because that seams to not happen right now

@@ -88,6 +93,12 @@ export function getCurrent(state: RuntimeState): number {
* @returns {boolean}
*/
export function skippedOutOfEvent(state: RuntimeState, previousTime: number, skipLimit: number): boolean {
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.timer.expectedFinish === null || state.timer.startedAt === null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm also hitting this
just by creating an event 1 minnut from now and then clicking roll

Copy link
Collaborator

Choose a reason for hiding this comment

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

@cpvalente
how about this is it intended?

Copy link
Owner Author

Choose a reason for hiding this comment

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

This should no longer be called before an event is loaded. Is that not the case?

Copy link
Collaborator

Choose a reason for hiding this comment

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

looks to work fine after the recent force push
d721d55

@cpvalente
Copy link
Owner Author

Should roll trigger a load and start dispatch when it rolls an event?

Because that seams to not happen right now

I havent added any new behaviour. Just removed the paths specific to roll.

We would need a follow up ticket to improve roll

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: 4

Outside diff range, codebase verification and nitpick comments (1)
apps/server/src/services/timerUtils.ts (1)

98-98: Consider removing or controlling the commented-out debug log.

Leaving commented-out code can clutter the codebase. It's better to either remove these lines or implement a logging level control to enable/disable logs dynamically.

-  // console.log(
-  //   state,
-  //   previousTime,
-  //   skipLimit,
-  //   hasSkipped && (adjustedClock > adjustedExpectedFinish || adjustedClock < startedAt),
-  // );
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7ac9d69 and d721d55.

Files ignored due to path filters (1)
  • packages/types/src/definitions/runtime/TimerState.type.ts is excluded by none and included by none
Files selected for processing (6)
  • apps/server/src/services/TimerService.ts (4 hunks)
  • apps/server/src/services/tests/timerUtils.test.ts (14 hunks)
  • apps/server/src/services/runtime-service/RuntimeService.ts (6 hunks)
  • apps/server/src/services/timerUtils.ts (8 hunks)
  • apps/server/src/stores/tests/runtimeState.test.ts (1 hunks)
  • apps/server/src/stores/runtimeState.ts (9 hunks)
Files skipped from review as they are similar to previous changes (4)
  • apps/server/src/services/tests/timerUtils.test.ts
  • apps/server/src/services/runtime-service/RuntimeService.ts
  • apps/server/src/stores/tests/runtimeState.test.ts
  • apps/server/src/stores/runtimeState.ts
Additional context used
Biome
apps/server/src/services/TimerService.ts

[error] 87-87: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 87-87: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

apps/server/src/services/timerUtils.ts

[error] 59-59: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 59-59: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 97-97: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 97-97: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 272-272: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 272-272: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Additional comments not posted (7)
apps/server/src/services/TimerService.ts (5)

7-7: LGTM: Addition of UpdateCallbackFn type alias.

The addition of the UpdateCallbackFn type alias improves code readability and maintainability.


18-18: LGTM: Initialization of endCallback property.

Initializing the endCallback property as undefined helps prevent potential runtime errors.


20-20: LGTM: Initialization of onUpdateCallback property.

Initializing the onUpdateCallback property as undefined helps prevent potential runtime errors.


35-41: LGTM: Addition of setOnUpdateCallback method.

The setOnUpdateCallback method provides a clear and flexible way to set the onUpdateCallback property, enhancing the functionality and maintainability of the TimerService class.


102-102: LGTM: Use of optional chaining for onUpdateCallback.

Using optional chaining ensures that the onUpdateCallback is only called if it is defined, preventing potential runtime errors.

apps/server/src/services/timerUtils.ts (2)

341-341: LGTM: Exporting isPlaybackActive function.

Exporting the isPlaybackActive function improves code reusability.


360-360: LGTM: Modification to getTimerPhase function.

Including a check for state.timer.secondaryTimer not being null ensures that the function correctly identifies the timer phase when a secondary timer is present.

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: 6

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d721d55 and bed091b.

Files ignored due to path filters (2)
  • apps/server/package.json is excluded by none and included by none
  • packages/types/src/definitions/runtime/TimerState.type.ts is excluded by none and included by none
Files selected for processing (10)
  • apps/server/src/app.ts (1 hunks)
  • apps/server/src/classes/data-provider/DataProvider.ts (1 hunks)
  • apps/server/src/services/TimerService.ts (4 hunks)
  • apps/server/src/services/tests/timerUtils.test.ts (14 hunks)
  • apps/server/src/services/app-state-service/AppStateService.ts (2 hunks)
  • apps/server/src/services/runtime-service/RuntimeService.ts (6 hunks)
  • apps/server/src/services/timerUtils.ts (8 hunks)
  • apps/server/src/stores/tests/runtimeState.test.ts (1 hunks)
  • apps/server/src/stores/runtimeState.ts (9 hunks)
  • apps/server/src/utils/development.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • apps/server/src/app.ts
  • apps/server/src/stores/tests/runtimeState.test.ts
  • apps/server/src/utils/development.ts
Additional context used
Biome
apps/server/src/services/app-state-service/AppStateService.ts

[error] 31-31: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

apps/server/src/services/TimerService.ts

[error] 87-87: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 87-87: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

apps/server/src/classes/data-provider/DataProvider.ts

[error] 28-28: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

apps/server/src/services/timerUtils.ts

[error] 59-59: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 59-59: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 97-97: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 97-97: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)


[error] 272-272: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 272-272: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Additional comments not posted (24)
apps/server/src/services/app-state-service/AppStateService.ts (1)

4-6: LGTM!

The import changes are appropriate and align with the new error handling strategy.

apps/server/src/services/TimerService.ts (5)

7-8: LGTM!

The addition of the UpdateCallbackFn type alias improves code readability and maintainability.


18-18: LGTM!

Initializing endCallback as undefined ensures that it has a clear initial state.


20-20: LGTM!

Initializing onUpdateCallback as undefined ensures that it has a clear initial state.


28-28: LGTM!

The removal of the onUpdateCallback parameter simplifies the constructor and aligns with the new method for setting the callback.


35-41: LGTM!

The addition of the setOnUpdateCallback method improves the flexibility and modularity of the TimerService class.

apps/server/src/classes/data-provider/DataProvider.ts (1)

16-20: LGTM!

The import changes are appropriate and align with the new error handling strategy.

apps/server/src/services/timerUtils.ts (3)

330-332: LGTM!

The modification ensures that the function returns null if either state.runtime.actualStart or state.runtime.plannedEnd is null.


Line range hint 341-345:
LGTM!

The modification makes the function available for use in other modules.


360-362: LGTM!

The modification ensures that the function returns TimerPhase.Pending if state.timer.secondaryTimer is not null.

apps/server/src/stores/runtimeState.ts (6)

18-24: LGTM!

The modifications improve the clarity of the code by providing explanations for the changes in state properties.

Also applies to: 29-38, 51-53


Line range hint 75-83:
LGTM!

The modifications ensure that the function resets all relevant state properties when clearing the state.


124-125: LGTM!

The modifications ensure that the function correctly updates the state properties based on the rundown data.


Line range hint 134-153:
LGTM!

The modifications ensure that the function correctly loads an event and updates the state based on the provided initial data.


373-426: LGTM!

The modifications ensure that the function correctly updates the timer and handles playback states, including edge cases related to roll and timer finishing.


Line range hint 455-460:
LGTM!

The modifications ensure that the function correctly handles rolling events and updates the state accordingly.

apps/server/src/services/runtime-service/RuntimeService.ts (6)

54-56: LGTM!

The modification ensures that the RuntimeService class is initialized with a timerService instance, improving modularity and reusability.


Line range hint 67-133:
LGTM!

The modifications ensure that the function correctly handles timer phase transitions, roll events, and updates to the timer and clock, improving the overall robustness of the timer management logic.


147-149: LGTM!

The modifications ensure that the function correctly initializes the eventTimer and handles resumable states, improving the initialization logic.


Line range hint 455-460:
LGTM!

The modifications ensure that the function correctly handles rolling events and updates the state accordingly.


575-578: LGTM!

The modifications ensure that the function correctly manages side effects during runtime updates, improving the overall robustness of the runtime management logic.


573-573: LGTM!

The export statement ensures that the runtimeService is available for use in other modules, improving modularity and reusability.

apps/server/src/services/__tests__/timerUtils.test.ts (2)

1366-1366: LGTM! Simplification of getRuntimeOffset.

The removal of the secondaryTarget property and setting the offset property to 0 instead of null are logical improvements that simplify the function.

Also applies to: 1407-1407, 1442-1442, 1459-1459, 1494-1494, 1511-1511, 1534-1534, 1551-1551


1699-1699: LGTM! Simplification of getTimerPhase.

The removal of the secondaryTarget property is a logical improvement that simplifies the function.

Also applies to: 1738-1738

Comment on lines +30 to +31
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the DEV: label and use a more conventional approach.

The use of the DEV: label is unconventional and flagged by static analysis tools. Consider wrapping the code in an if-statement that checks if the environment is development.

-  // eslint-disable-next-line no-unused-labels -- dev code path
-  DEV: shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
+  if (!isProduction) {
+    shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
if (!isProduction) {
shouldCrashDev(isPath(filename), 'setLastLoadedProject should not be called with a path');
}
Tools
Biome

[error] 31-31: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

Comment on lines +86 to +91
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.timer.expectedFinish === null) {
throw new Error('TimerService.addTime: expectedFinish is negative');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the DEV: label and use a more conventional approach.

The use of the DEV: label is unconventional and flagged by static analysis tools. Consider wrapping the code in an if-statement that checks if the environment is development.

-    // eslint-disable-next-line no-unused-labels -- dev code path
-    DEV: {
-      if (state.timer.expectedFinish === null) {
-        throw new Error('TimerService.addTime: expectedFinish is negative');
-      }
-    }
+    if (!isProduction) {
+      if (state.timer.expectedFinish === null) {
+        throw new Error('TimerService.addTime: expectedFinish is negative');
+      }
+    }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.timer.expectedFinish === null) {
throw new Error('TimerService.addTime: expectedFinish is negative');
}
}
if (!isProduction) {
if (state.timer.expectedFinish === null) {
throw new Error('TimerService.addTime: expectedFinish is negative');
}
}
Tools
Biome

[error] 87-87: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 87-87: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Comment on lines +27 to +28
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the DEV: label and use a more conventional approach.

The use of the DEV: label is unconventional and flagged by static analysis tools. Consider wrapping the code in an if-statement that checks if the environment is development.

-  // eslint-disable-next-line no-unused-labels -- dev code path
-  DEV: shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
+  if (!isProduction) {
+    shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
if (!isProduction) {
shouldCrashDev(!isPath(filePath), 'initPersistence should be called with a path');
}
Tools
Biome

[error] 28-28: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)

Comment on lines +271 to +277
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
// we know current exists as long as eventNow exists
if (state.timer.current === null) {
throw new Error('timerUtils.calculate: current must be set');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused label and refine error handling logic.

The label DEV is not used correctly and should be removed. The error handling can be improved by using more descriptive error messages and ensuring they align with the conditions checked.

-  // eslint-disable-next-line no-unused-labels -- dev code path
-  DEV: {
-    // we know current exists as long as eventNow exists
-    if (state.timer.current === null) {
-      throw new Error('timerUtils.calculate: current must be set');
-    }
-  }
+  if (state.timer.current === null) {
+    console.error('timerUtils.calculate: current must be set');
+    throw new Error('timerUtils.calculate: current must be set');
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
// we know current exists as long as eventNow exists
if (state.timer.current === null) {
throw new Error('timerUtils.calculate: current must be set');
}
}
if (state.timer.current === null) {
console.error('timerUtils.calculate: current must be set');
throw new Error('timerUtils.calculate: current must be set');
}
Tools
Biome

[error] 272-272: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 272-272: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Comment on lines +58 to +63
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.eventNow === null || state.timer.duration === null) {
throw new Error('timerUtils.getCurrent: invalid state received');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused label and refine error handling logic.

The label DEV is not used correctly and should be removed. The error handling can be improved by using more descriptive error messages and ensuring they align with the conditions checked.

-  // eslint-disable-next-line no-unused-labels -- dev code path
-  DEV: {
-    if (state.eventNow === null || state.timer.duration === null) {
-      throw new Error('timerUtils.getCurrent: invalid state received');
-    }
-  }
+  if (state.eventNow === null || state.timer.duration === null) {
+    console.error('timerUtils.getCurrent: invalid state received');
+    throw new Error('timerUtils.getCurrent: invalid state received');
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.eventNow === null || state.timer.duration === null) {
throw new Error('timerUtils.getCurrent: invalid state received');
}
}
if (state.eventNow === null || state.timer.duration === null) {
console.error('timerUtils.getCurrent: invalid state received');
throw new Error('timerUtils.getCurrent: invalid state received');
}
Tools
Biome

[error] 59-59: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 59-59: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Comment on lines +96 to +101
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.timer.expectedFinish === null || state.timer.startedAt === null) {
throw new Error('timerUtils.skippedOutOfEvent: invalid state received');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused label and refine error handling logic.

The label DEV is not used correctly and should be removed. The error handling can be improved by using more descriptive error messages and ensuring they align with the conditions checked.

-  // eslint-disable-next-line no-unused-labels -- dev code path
-  DEV: {
-    if (state.timer.expectedFinish === null || state.timer.startedAt === null) {
-      throw new Error('timerUtils.skippedOutOfEvent: invalid state received');
-    }
-  }
+  if (state.timer.expectedFinish === null || state.timer.startedAt === null) {
+    console.error('timerUtils.skippedOutOfEvent: invalid state received');
+    throw new Error('timerUtils.skippedOutOfEvent: invalid state received');
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (state.timer.expectedFinish === null || state.timer.startedAt === null) {
throw new Error('timerUtils.skippedOutOfEvent: invalid state received');
}
}
if (state.timer.expectedFinish === null || state.timer.startedAt === null) {
console.error('timerUtils.skippedOutOfEvent: invalid state received');
throw new Error('timerUtils.skippedOutOfEvent: invalid state received');
}
Tools
Biome

[error] 97-97: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 97-97: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

@cpvalente cpvalente merged commit edc7f20 into master Jul 19, 2024
3 checks passed
@cpvalente cpvalente deleted the roll-events branch July 19, 2024 12:11
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