CustomRPC Fix#4228
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements a midnight reset mechanism for the Custom RPC plugin to ensure that the 'current time' timestamp remains accurate across multiple days. It introduces a scheduling function using setTimeout and updates the plugin's lifecycle methods to manage this timer. Review feedback suggests simplifying the start-of-day calculation using the idiomatic Date.setHours method and removing redundant timeout clearing in the stop function, as this logic is already encapsulated within setRpc.
| const getElapsedToday = () => { | ||
| const now = new Date(); | ||
| return now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds(); | ||
| }; | ||
| activity.timestamps = { start: Date.now() - getElapsedToday() * 1000 }; |
There was a problem hiding this comment.
The calculation for the start of the current day can be significantly simplified using the built-in Date.setHours method. This approach is more idiomatic, readable, and avoids manual arithmetic that ignores milliseconds, ensuring the timestamp starts exactly at 00:00:00.000.
activity.timestamps = { start: new Date().setHours(0, 0, 0, 0) };
| stop: () => { | ||
| if (midnightTimeout !== null) { | ||
| clearTimeout(midnightTimeout); | ||
| midnightTimeout = null; | ||
| } | ||
| setRpc(true); | ||
| }, |
Fix
TimestampMode.TIMEnot resetting after 24h in CustomRPC.The timestamp was only calculated once, causing the timer to continue past midnight.
Now it recalculates based on the start of the current day so it correctly resets at 00:00 without needing timers or scheduling.