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:
-
startandendare finite numbers withstart >= 0andend >= start -
elapsedis clamped to the range[0, max_duration] - Handle NaN/Infinity values appropriately
Implementation Options
Two potential locations for this validation:
-
Store level (
src/store.ts-setTimemethod): Validate at the state mutation point -
API level (
src/index.tsmethods): Validate before callingsetTime
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
- PR: !1 (merged)
- Original comment: !1 (comment 1434)
Requested by: @kenzenfuyuba