Browse Source

refactor: reuse eviction logic for message and node stores

pull/814/head
philon- 11 months ago
parent
commit
2910c96622
  1. 18
      packages/web/src/core/stores/messageStore/index.ts
  2. 10
      packages/web/src/core/stores/nodeDBStore/index.ts
  3. 5
      packages/web/src/core/stores/nodeDBStore/nodeValidation.ts
  4. 14
      packages/web/src/core/stores/utils/evictOldestEntries.ts

18
packages/web/src/core/stores/messageStore/index.ts

@ -15,6 +15,7 @@ import type { Types } from "@meshtastic/core";
import { produce } from "immer"; import { produce } from "immer";
import { create as createStore, type StateCreator } from "zustand"; import { create as createStore, type StateCreator } from "zustand";
import { type PersistOptions, persist } from "zustand/middleware"; import { type PersistOptions, persist } from "zustand/middleware";
import { evictOldestEntries } from "../utils/evictOldestEntries";
const CURRENT_STORE_VERSION = 0; const CURRENT_STORE_VERSION = 0;
const MESSAGESTORE_RETENTION_NUM = 10; const MESSAGESTORE_RETENTION_NUM = 10;
@ -182,11 +183,9 @@ function messageStoreFactory(
log?.set(message.messageId, message); log?.set(message.messageId, message);
} }
while (log && log.size > MESSAGELOG_RETENTION_NUM) { if (log) {
const firstKey = log.keys().next().value; // maps keep insertion order, so this is oldest // Enforce retention limit
if (firstKey !== undefined) { evictOldestEntries(log, MESSAGELOG_RETENTION_NUM);
log.delete(firstKey);
}
} }
}), }),
); );
@ -372,13 +371,8 @@ export const messageStoreInitializer: StateCreator<PrivateMessageStoreState> = (
produce<PrivateMessageStoreState>((draft) => { produce<PrivateMessageStoreState>((draft) => {
draft.messageStores.set(id, nodeStore); draft.messageStores.set(id, nodeStore);
// If over limit, remove oldest inserted. FIFO // Enforce retention limit
if (draft.messageStores.size > MESSAGESTORE_RETENTION_NUM) { evictOldestEntries(draft.messageStores, MESSAGESTORE_RETENTION_NUM);
const firstKey = draft.messageStores.keys().next().value;
if (firstKey !== undefined) {
draft.messageStores.delete(firstKey);
}
}
}), }),
); );

10
packages/web/src/core/stores/nodeDBStore/index.ts

@ -6,6 +6,7 @@ import { Protobuf, type Types } from "@meshtastic/core";
import { produce } from "immer"; import { produce } from "immer";
import { create as createStore, type StateCreator } from "zustand"; import { create as createStore, type StateCreator } from "zustand";
import { type PersistOptions, persist } from "zustand/middleware"; import { type PersistOptions, persist } from "zustand/middleware";
import { evictOldestEntries } from "../utils/evictOldestEntries";
import type { NodeError, NodeErrorType, ProcessPacketParams } from "./types"; import type { NodeError, NodeErrorType, ProcessPacketParams } from "./types";
const CURRENT_STORE_VERSION = 0; const CURRENT_STORE_VERSION = 0;
@ -393,13 +394,8 @@ export const nodeDBInitializer: StateCreator<PrivateNodeDBState> = (
produce<PrivateNodeDBState>((draft) => { produce<PrivateNodeDBState>((draft) => {
draft.nodeDBs.set(id, nodeDB); draft.nodeDBs.set(id, nodeDB);
// If over limit, remove oldest inserted. FIFO // Enforce retention limit
while (draft.nodeDBs.size > NODEDB_RETENTION_NUM) { evictOldestEntries(draft.nodeDBs, 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);
}
}
}), }),
); );

5
packages/web/src/core/stores/nodeDBStore/nodeValidation.ts

@ -17,7 +17,10 @@ export function validateIncomingNode(
// No existing node with this node number. // No existing node with this node number.
// Check if the new node's public key (if present and not empty) // Check if the new node's public key (if present and not empty)
// is already claimed by another existing node. // 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( const nodesWithSameKey = getNodes(
(node) => node.user?.publicKey === newNode.user?.publicKey, (node) => node.user?.publicKey === newNode.user?.publicKey,
); );

14
packages/web/src/core/stores/utils/evictOldestEntries.ts

@ -0,0 +1,14 @@
export function evictOldestEntries<K, V>(
map: Map<K, V>,
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
}
}
}
Loading…
Cancel
Save