Skip to content

Add validation for time invariants in setTime method

Summary

Consider adding validation to ensure time values are consistent and within valid bounds when setting time in the player state.

Background

During review of PR #1, it was identified that the setTime method in src/store.ts should validate time invariants to prevent:

  • Negative or invalid time ranges
  • Out-of-bounds elapsed time values
  • Inconsistent start/end relationships

Proposed Solution

Add validation logic that ensures:

  • start and end are finite numbers with start >= 0 and end >= start
  • elapsed is clamped to the range [0, max_duration]
  • Handle NaN/Infinity values appropriately

Implementation Options

Two potential locations for this validation:

  1. Store level (src/store.ts - setTime method): Validate at the state mutation point
  2. API level (src/index.ts methods): Validate before calling setTime

Original Code Suggestion

setTime: (time) => {
    const start = Math.max(0, time.start);
    const end = Math.max(start, time.end);
    const duration = Math.max(0, time.duration);
    const maxElapsed = duration || Math.max(0, end - start);
    const elapsed = Math.min(Math.max(0, time.elapsed), maxElapsed);
    set({ time: { start, end, duration, elapsed } });
},

References

Requested by: @kenzenfuyuba