diff --git a/packages/web/src/core/stores/messageStore/index.ts b/packages/web/src/core/stores/messageStore/index.ts index accd6696..1c1edf14 100644 --- a/packages/web/src/core/stores/messageStore/index.ts +++ b/packages/web/src/core/stores/messageStore/index.ts @@ -15,6 +15,7 @@ import type { Types } from "@meshtastic/core"; import { produce } from "immer"; import { create as createStore, type StateCreator } from "zustand"; import { type PersistOptions, persist } from "zustand/middleware"; +import { evictOldestEntries } from "../utils/evictOldestEntries"; const CURRENT_STORE_VERSION = 0; const MESSAGESTORE_RETENTION_NUM = 10; @@ -182,11 +183,9 @@ function messageStoreFactory( log?.set(message.messageId, message); } - while (log && log.size > MESSAGELOG_RETENTION_NUM) { - const firstKey = log.keys().next().value; // maps keep insertion order, so this is oldest - if (firstKey !== undefined) { - log.delete(firstKey); - } + if (log) { + // Enforce retention limit + evictOldestEntries(log, MESSAGELOG_RETENTION_NUM); } }), ); @@ -372,13 +371,8 @@ export const messageStoreInitializer: StateCreator = ( produce((draft) => { draft.messageStores.set(id, nodeStore); - // If over limit, remove oldest inserted. FIFO - if (draft.messageStores.size > MESSAGESTORE_RETENTION_NUM) { - const firstKey = draft.messageStores.keys().next().value; - if (firstKey !== undefined) { - draft.messageStores.delete(firstKey); - } - } + // Enforce retention limit + evictOldestEntries(draft.messageStores, MESSAGESTORE_RETENTION_NUM); }), ); diff --git a/packages/web/src/core/stores/nodeDBStore/index.ts b/packages/web/src/core/stores/nodeDBStore/index.ts index a5d633fe..f1f2362b 100644 --- a/packages/web/src/core/stores/nodeDBStore/index.ts +++ b/packages/web/src/core/stores/nodeDBStore/index.ts @@ -6,6 +6,7 @@ import { Protobuf, type Types } from "@meshtastic/core"; import { produce } from "immer"; import { create as createStore, type StateCreator } from "zustand"; import { type PersistOptions, persist } from "zustand/middleware"; +import { evictOldestEntries } from "../utils/evictOldestEntries"; import type { NodeError, NodeErrorType, ProcessPacketParams } from "./types"; const CURRENT_STORE_VERSION = 0; @@ -393,13 +394,8 @@ export const nodeDBInitializer: StateCreator = ( produce((draft) => { draft.nodeDBs.set(id, nodeDB); - // If over limit, remove oldest inserted. FIFO - while (draft.nodeDBs.size > NODEDB_RETENTION_NUM) { - const firstKey = draft.nodeDBs.keys().next().value; // maps keep insertion order, so this is oldest - if (firstKey !== undefined) { - draft.nodeDBs.delete(firstKey); - } - } + // Enforce retention limit + evictOldestEntries(draft.nodeDBs, NODEDB_RETENTION_NUM); }), ); diff --git a/packages/web/src/core/stores/nodeDBStore/nodeValidation.ts b/packages/web/src/core/stores/nodeDBStore/nodeValidation.ts index 1e6d1f66..52975660 100644 --- a/packages/web/src/core/stores/nodeDBStore/nodeValidation.ts +++ b/packages/web/src/core/stores/nodeDBStore/nodeValidation.ts @@ -17,7 +17,10 @@ export function validateIncomingNode( // No existing node with this node number. // Check if the new node's public key (if present and not empty) // is already claimed by another existing node. - if (newNode.user?.publicKey !== undefined) { + if ( + newNode.user?.publicKey !== undefined && + newNode.user?.publicKey.length > 0 + ) { const nodesWithSameKey = getNodes( (node) => node.user?.publicKey === newNode.user?.publicKey, ); diff --git a/packages/web/src/core/stores/utils/evictOldestEntries.ts b/packages/web/src/core/stores/utils/evictOldestEntries.ts new file mode 100644 index 00000000..500726de --- /dev/null +++ b/packages/web/src/core/stores/utils/evictOldestEntries.ts @@ -0,0 +1,14 @@ +export function evictOldestEntries( + map: Map, + maxSize: number, +): void { + // while loop in case maxSize is ever changed to be lower, to trim all the way down + while (map.size > maxSize) { + const firstKey = map.keys().next().value; // maps keep insertion order, so this is oldest + if (firstKey !== undefined) { + map.delete(firstKey); + } else { + break; // should not happen, but just in case + } + } +}