Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

refactor(app): share lexical ordering for stable ties

Hampus91200b6ef89d
diff --git a/fluxer_app/src/features/emoji/state/UsageFrecency.ts b/fluxer_app/src/features/emoji/state/UsageFrecency.ts
index eb89eddf8..7587b6ad7 100644
--- a/fluxer_app/src/features/emoji/state/UsageFrecency.ts
+++ b/fluxer_app/src/features/emoji/state/UsageFrecency.ts
@@ -1,5 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import {compareStrings} from '@app/lib/list/CompareStrings';
+import {MS_PER_DAY} from '@fluxer/date_utils/src/DateConstants';
+
export type UsageEntry = Readonly<{
count: number;
lastUsed: number;
@@ -17,7 +20,7 @@ export type UsageRanking = Readonly<{
scoreByKey: ReadonlyMap<string, number>;
}>;
-const USAGE_FRECENCY_HALF_LIFE_MS = 7 * 24 * 60 * 60 * 1000;
+const USAGE_FRECENCY_HALF_LIFE_MS = 7 * MS_PER_DAY;
export const MAX_TRACKED_USAGE_KEYS = 200;
const MAX_USAGE_COUNT = 100_000;
const MAX_USAGE_KEY_LENGTH = 256;
@@ -73,7 +76,7 @@ function compareRankedUsage(a: RankedUsage, b: RankedUsage): number {
if (a.entry.count !== b.entry.count) {
return b.entry.count - a.entry.count;
}
- return a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
+ return compareStrings(a.key, b.key);
}
function rankEntries(usage: Readonly<Record<string, UsageEntry>>, now: number): Array<RankedUsage> {
diff --git a/fluxer_app/src/features/messaging/utils/DmChannelUtils.ts b/fluxer_app/src/features/messaging/utils/DmChannelUtils.ts
index 268ae6ad4..79abe16d2 100644
--- a/fluxer_app/src/features/messaging/utils/DmChannelUtils.ts
+++ b/fluxer_app/src/features/messaging/utils/DmChannelUtils.ts
@@ -3,6 +3,7 @@
import type {Channel} from '@app/features/channel/models/Channel';
import Relationships from '@app/features/relationship/state/Relationships';
import UserPinnedDM from '@app/features/user/state/UserPinnedDM';
+import {compareStrings} from '@app/lib/list/CompareStrings';
import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
import {RelationshipTypes} from '@fluxer/constants/src/UserConstants';
import {compare, fromTimestamp} from '@fluxer/snowflake/src/SnowflakeUtils';
@@ -30,7 +31,6 @@ const getChannelSortSnowflake = (channel: Channel): string => {
export function getSortedDmChannels(dmChannels: ReadonlyArray<Channel>, currentUserId?: string | null): Array<Channel> {
const pinnedOrder = new Map(UserPinnedDM.pinnedDMs.map((id, index) => [id, index]));
- const compareChannelIds = (a: Channel, b: Channel): number => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
return dmChannels
.filter((channel) => !(channel.type === ChannelTypes.DM_PERSONAL_NOTES || channel.id === currentUserId))
.sort((a, b) => {
@@ -43,7 +43,7 @@ export function getSortedDmChannels(dmChannels: ReadonlyArray<Channel>, currentU
if (diff !== 0) {
return diff;
}
- return compareChannelIds(a, b);
+ return compareStrings(a.id, b.id);
}
if (aIsPinned !== bIsPinned) {
return aIsPinned ? -1 : 1;
@@ -54,6 +54,6 @@ export function getSortedDmChannels(dmChannels: ReadonlyArray<Channel>, currentU
if (sortDiff !== 0) {
return sortDiff;
}
- return compareChannelIds(a, b);
+ return compareStrings(a.id, b.id);
});
}
diff --git a/fluxer_app/src/lib/list/CompareStrings.ts b/fluxer_app/src/lib/list/CompareStrings.ts
new file mode 100644
index 000000000..991e0f294
--- /dev/null
+++ b/fluxer_app/src/lib/list/CompareStrings.ts
@@ -0,0 +1,7 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+export function compareStrings(left: string, right: string): number {
+ if (left < right) return -1;
+ if (left > right) return 1;
+ return 0;
+}