From 473f5007f99160c96c6d78e0fb6b331add521b59 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 24 Sep 2024 10:48:56 -0700 Subject: [PATCH 1/2] Save timezone with first message vs after throttle delay --- src/libs/DateUtils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libs/DateUtils.ts b/src/libs/DateUtils.ts index 6b43a549256d..2de905ff6047 100644 --- a/src/libs/DateUtils.ts +++ b/src/libs/DateUtils.ts @@ -47,6 +47,8 @@ type CustomStatusTypes = ValueOf; type Locale = ValueOf; type WeekDay = 0 | 1 | 2 | 3 | 4 | 5 | 6; +const TIMEZONE_UPDATE_THROTTLE_MINUTES = 5; + let currentUserAccountID: number | undefined; Onyx.connect({ key: ONYXKEYS.SESSION, @@ -352,12 +354,12 @@ function getDaysOfWeek(preferredLocale: Locale): string[] { return daysOfWeek.map((date) => format(date, 'eeee')); } -// Used to throttle updates to the timezone when necessary -let lastUpdatedTimezoneTime = new Date(); +// Used to throttle updates to the timezone when necessary. Initialize outside the throttle window so it's updated the first time. +let lastUpdatedTimezoneTime = subMinutes(new Date(), TIMEZONE_UPDATE_THROTTLE_MINUTES + 1); function canUpdateTimezone(): boolean { const currentTime = new Date(); - const fiveMinutesAgo = subMinutes(currentTime, 5); + const fiveMinutesAgo = subMinutes(currentTime, TIMEZONE_UPDATE_THROTTLE_MINUTES); // Compare the last updated time with five minutes ago return isBefore(lastUpdatedTimezoneTime, fiveMinutesAgo); } From 28c6a591f95ddaca598c98bfe299e70a0eca00a2 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 24 Sep 2024 11:43:13 -0700 Subject: [PATCH 2/2] Fix initial conditions for test --- tests/unit/DateUtilsTest.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/DateUtilsTest.ts b/tests/unit/DateUtilsTest.ts index f3abed2e3372..52ef39ed4da5 100644 --- a/tests/unit/DateUtilsTest.ts +++ b/tests/unit/DateUtilsTest.ts @@ -132,6 +132,7 @@ describe('DateUtils', () => { it('canUpdateTimezone should return false when lastUpdatedTimezoneTime is less than 5 minutes ago', () => { // Use fake timers to control the current time jest.useFakeTimers(); + DateUtils.setTimezoneUpdated(); jest.setSystemTime(addMinutes(new Date(), 4)); const isUpdateTimezoneAllowed = DateUtils.canUpdateTimezone(); expect(isUpdateTimezoneAllowed).toBe(false);