(feat): add cache package
(feat): improve local storage security (feat): move settings to dedicated settings package
This commit is contained in:
parent
fb095db7a6
commit
790a1db788
54 changed files with 1984 additions and 947 deletions
30
packages/cache/src/helpers.ts
vendored
Normal file
30
packages/cache/src/helpers.ts
vendored
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import type { CachedMessage, ConversationWindow } from "./schemas";
|
||||
|
||||
export function trimMessages(
|
||||
messages: readonly CachedMessage[],
|
||||
limit: number,
|
||||
): CachedMessage[] {
|
||||
return [...messages]
|
||||
.sort((a, b) => b.SendTime - a.SendTime)
|
||||
.slice(0, limit)
|
||||
.sort((a, b) => a.SendTime - b.SendTime);
|
||||
}
|
||||
|
||||
export function replaceConversation(
|
||||
windows: readonly ConversationWindow[],
|
||||
replacement: ConversationWindow,
|
||||
): ConversationWindow[] {
|
||||
return [
|
||||
replacement,
|
||||
...windows.filter((item) => item.UserId !== replacement.UserId),
|
||||
];
|
||||
}
|
||||
|
||||
export function selectConversationWindows(
|
||||
windows: readonly ConversationWindow[],
|
||||
limit: number,
|
||||
): ConversationWindow[] {
|
||||
return [...windows]
|
||||
.sort((a, b) => b.LastMessageAt - a.LastMessageAt || a.UserId - b.UserId)
|
||||
.slice(0, limit);
|
||||
}
|
||||
Loading…
Reference in a new issue