Skip to content

Commit

Permalink
make slider 30 seconds older to fix Date comparison and flicker
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjam committed Dec 14, 2023
1 parent d621222 commit 211c2dc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions source/reddit-comments/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export const databaseName = 'reddit-unread-comments-db';
/** 2 * 10**12 */
export const currentSessionCreatedAt = 2e12 as const;

/** Offset in seconds to fix Date comparison 1hr > 1hr and prevent flicker. */
export const dateCorrectionOffset = 30 as const;

// tested
/** Start deleting at. */
export const dbSizeLimit: number = 1 * 1024 * 1024; // 1 MB limit
Expand Down
11 changes: 9 additions & 2 deletions source/reddit-comments/datetime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sub, isToday, startOfDay, format } from 'date-fns';
import { sub, format } from 'date-fns';
import { dateCorrectionOffset } from './constants';

import { SettingsData, TimeScaleType } from './database/schema';
import { MyUnparsableDateException } from './exceptions';
Expand Down Expand Up @@ -101,5 +102,11 @@ export const radioAndSliderToDate = (

const pastDate = sub(new Date(), { [unit]: timeSlider });

return pastDate;
// make the slider 30 seconds older than it is so the comment passes the filtering condition
// fixes flickering for 1hr > 1hr
// should be done exactly and only here
// sub = older, add = newer
const correctedPastDate = sub(pastDate, { seconds: dateCorrectionOffset });

return correctedPastDate;
};
2 changes: 1 addition & 1 deletion source/reddit-comments/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const getFilteredNewerCommentsByDate = (
const filteredComments = commentElements.filter((commentElement) => {
const commentId = validateCommentElementIdOrThrow(commentElement);
const commentDate = getDateFromCommentId(commentId); // here it throws
return commentDate.getTime() >= newerThan.getTime();
return commentDate.getTime() > newerThan.getTime();
});

return filteredComments;
Expand Down

0 comments on commit 211c2dc

Please sign in to comment.