Skip to content

Commit

Permalink
Move short circuit check to underlying map (#5077)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 22, 2023
1 parent 90883ae commit 7b379c8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 0 additions & 1 deletion frontend/openchat-client/src/stores/failedMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
15 changes: 10 additions & 5 deletions frontend/openchat-client/src/stores/unconfirmed.ts
Original file line number Diff line number Diff line change
@@ -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<Message>[];
Expand Down Expand Up @@ -28,7 +33,7 @@ function createUnconfirmedStore() {
return applyUpdateToState(
result,
key,
removeWhere(messages, (m) => m.timestamp < oneMinuteAgo)
removeWhere(messages, (m) => m.timestamp < oneMinuteAgo),
);
}, new MessageContextMap<UnconfirmedState>());
});
Expand All @@ -37,7 +42,7 @@ function createUnconfirmedStore() {

function removeWhere(
messages: EventWrapper<Message>[],
predicate: (message: EventWrapper<Message>) => boolean
predicate: (message: EventWrapper<Message>) => boolean,
): EventWrapper<Message>[] {
return messages.filter((m) => {
if (predicate(m)) {
Expand All @@ -51,7 +56,7 @@ function createUnconfirmedStore() {
function applyUpdateToState(
state: UnconfirmedMessages,
keyUpdated: MessageContext,
messages: EventWrapper<Message>[]
messages: EventWrapper<Message>[],
): UnconfirmedMessages {
if (messages.length === 0) {
state.delete(keyUpdated);
Expand Down Expand Up @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions frontend/openchat-shared/src/utils/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class SafeMap<K, V> {
}

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 {
Expand Down

0 comments on commit 7b379c8

Please sign in to comment.