Address code review feedback: fix Date object reuse and add clarifying comments
Co-authored-by: THoguet <61246905+THoguet@users.noreply.github.com>
This commit is contained in:
1 parent
4e7eb18bc5
commit
6061e02835
2 files changed
+8
-8
No files matched your search
@@ -19,6 +19,9 @@ export class CalendarMonth implements OnInit {
|
|||||||
year!: number;
|
year!: number;
|
||||||
|
|
||||||
// Cache "now" as a class property to avoid recreating it on every call
|
// Cache "now" as a class property to avoid recreating it on every call
|
||||||
|
// Note: This value is cached at component initialization and won't update if the day changes
|
||||||
|
// while the component is still mounted. This is acceptable for this use case since the app
|
||||||
|
// is primarily used within a single day session.
|
||||||
private readonly now: Date = (() => {
|
private readonly now: Date = (() => {
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
date.setHours(0, 0, 0, 0);
|
date.setHours(0, 0, 0, 0);
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ export class CalendarService {
|
|||||||
// Cache holidays to avoid recalculation
|
// Cache holidays to avoid recalculation
|
||||||
const currentYear = new Date().getFullYear();
|
const currentYear = new Date().getFullYear();
|
||||||
if (this.holidayCacheYear === currentYear && this.holidayTimestampsCache) {
|
if (this.holidayCacheYear === currentYear && this.holidayTimestampsCache) {
|
||||||
|
// Return timestamps directly for efficient lookup
|
||||||
return Array.from(this.holidayTimestampsCache).map((ts) => new Date(ts));
|
return Array.from(this.holidayTimestampsCache).map((ts) => new Date(ts));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +110,7 @@ export class CalendarService {
|
|||||||
const holidays = hd.getHolidays(currentYear, lang);
|
const holidays = hd.getHolidays(currentYear, lang);
|
||||||
const holidaysPlusOne = hd.getHolidays(currentYear + 1, lang);
|
const holidaysPlusOne = hd.getHolidays(currentYear + 1, lang);
|
||||||
holidays.push(...holidaysPlusOne);
|
holidays.push(...holidaysPlusOne);
|
||||||
let holidaysDate: Date[] = holidays.map((holiday) => new Date(holiday.date));
|
const holidaysDate: Date[] = holidays.map((holiday) => new Date(holiday.date));
|
||||||
|
|
||||||
// Update cache
|
// Update cache
|
||||||
this.holidayTimestampsCache = new Set(holidaysDate.map((d) => d.getTime()));
|
this.holidayTimestampsCache = new Set(holidaysDate.map((d) => d.getTime()));
|
||||||
@@ -736,19 +737,15 @@ export class SelectedDates implements SelectedDateInterface {
|
|||||||
this.datesSelected = this._datesSelected.slice();
|
this.datesSelected = this._datesSelected.slice();
|
||||||
this.grouping();
|
this.grouping();
|
||||||
|
|
||||||
// Mark this day as vacation
|
// Mark this day as vacation (reuse the day object since it's already normalized)
|
||||||
const startDay = new Date(day);
|
this.datesSelected.push(new SelectedDate(vacType, new DateRange<Date>(day, day)));
|
||||||
const endDay = new Date(day);
|
|
||||||
this.datesSelected.push(
|
|
||||||
new SelectedDate(vacType, new DateRange<Date>(startDay, endDay)),
|
|
||||||
);
|
|
||||||
|
|
||||||
const heuristic = this.computeHeuristics();
|
const heuristic = this.computeHeuristics();
|
||||||
|
|
||||||
// Track only if better than current best
|
// Track only if better than current best
|
||||||
if (heuristic > bestHeuristic) {
|
if (heuristic > bestHeuristic) {
|
||||||
bestHeuristic = heuristic;
|
bestHeuristic = heuristic;
|
||||||
const capturedDay = startDay;
|
const capturedDay = day;
|
||||||
bestStrategy = {
|
bestStrategy = {
|
||||||
apply: () => {
|
apply: () => {
|
||||||
this._datesSelected = [...tempDatesBackup];
|
this._datesSelected = [...tempDatesBackup];
|
||||||
|
|||||||
Reference in new issue
Block a user