From 7b379c89b1c1f296a10188ac05fa3f6a2d292369 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Fri, 22 Dec 2023 13:25:51 +0000 Subject: [PATCH] Move short circuit check to underlying map (#5077) --- .../openchat-client/src/stores/failedMessages.ts | 1 - .../openchat-client/src/stores/unconfirmed.ts | 15 ++++++++++----- frontend/openchat-shared/src/utils/map.ts | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/frontend/openchat-client/src/stores/failedMessages.ts b/frontend/openchat-client/src/stores/failedMessages.ts index cc38e75d89..95a39b889b 100644 --- a/frontend/openchat-client/src/stores/failedMessages.ts +++ b/frontend/openchat-client/src/stores/failedMessages.ts @@ -25,7 +25,6 @@ function createFailedMessagesStore() { })); }, contains: (key: MessageContext, messageId: bigint): boolean => { - if (store.size() === 0) return false; const chatState = store.get(key); return chatState ? chatState[messageId.toString()] !== undefined : false; }, diff --git a/frontend/openchat-client/src/stores/unconfirmed.ts b/frontend/openchat-client/src/stores/unconfirmed.ts index 9ffb8c4703..22006b397b 100644 --- a/frontend/openchat-client/src/stores/unconfirmed.ts +++ b/frontend/openchat-client/src/stores/unconfirmed.ts @@ -1,6 +1,11 @@ import { writable } from "svelte/store"; import { createSetStore } from "./setStore"; -import { type EventWrapper, type Message, type MessageContext, MessageContextMap } from "openchat-shared"; +import { + type EventWrapper, + type Message, + type MessageContext, + MessageContextMap, +} from "openchat-shared"; export type UnconfirmedState = { messages: EventWrapper[]; @@ -28,7 +33,7 @@ function createUnconfirmedStore() { return applyUpdateToState( result, key, - removeWhere(messages, (m) => m.timestamp < oneMinuteAgo) + removeWhere(messages, (m) => m.timestamp < oneMinuteAgo), ); }, new MessageContextMap()); }); @@ -37,7 +42,7 @@ function createUnconfirmedStore() { function removeWhere( messages: EventWrapper[], - predicate: (message: EventWrapper) => boolean + predicate: (message: EventWrapper) => boolean, ): EventWrapper[] { return messages.filter((m) => { if (predicate(m)) { @@ -51,7 +56,7 @@ function createUnconfirmedStore() { function applyUpdateToState( state: UnconfirmedMessages, keyUpdated: MessageContext, - messages: EventWrapper[] + messages: EventWrapper[], ): UnconfirmedMessages { if (messages.length === 0) { state.delete(keyUpdated); @@ -86,7 +91,7 @@ function createUnconfirmedStore() { store.update((state) => { const messages = removeWhere( state.get(key)?.messages ?? [], - (m) => m.event.messageId === messageId + (m) => m.event.messageId === messageId, ); return applyUpdateToState(state, key, messages); }); diff --git a/frontend/openchat-shared/src/utils/map.ts b/frontend/openchat-shared/src/utils/map.ts index 76ce581594..42b8b41c9e 100644 --- a/frontend/openchat-shared/src/utils/map.ts +++ b/frontend/openchat-shared/src/utils/map.ts @@ -64,6 +64,7 @@ export class SafeMap { } delete(key: K): boolean { + if (this._map.size === 0) return false; return this._map.delete(this.toString(key)); } forEach(callbackfn: (value: V, key: K) => void): void {