Skip to content

Commit

Permalink
Consolidate failedMessages to use shared store logic (#5043)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 19, 2023
1 parent 2e35ca7 commit d000cb1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
5 changes: 3 additions & 2 deletions frontend/app/src/components/home/ChatEventList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
let messageReadTimers: Record<number, number> = {};
$: user = client.user;
$: userId = $user.userId;
$: unconfirmed = client.unconfirmed;
$: failedMessagesStore = client.failedMessagesStore;
$: threadSummary = threadRootEvent?.event.thread;
Expand Down Expand Up @@ -283,7 +284,7 @@
async function afterSendMessage(context: MessageContext, event: EventWrapper<Message>) {
if (context.threadRootMessageIndex !== undefined && threadRootEvent !== undefined) {
const summary = {
participantIds: new Set<string>([$user.userId]),
participantIds: new Set<string>([userId]),
numberOfReplies: event.event.messageIndex + 1,
latestEventIndex: event.index,
latestEventTimestamp: event.timestamp,
Expand Down Expand Up @@ -395,7 +396,7 @@
}
function isReadByMe(_store: unknown, evt: EventWrapper<ChatEventType>): boolean {
if (readonly) return true;
if (readonly || (evt.event.kind === "message" && evt.event.sender === userId)) return true;
if (evt.event.kind === "message" || evt.event.kind === "aggregate_common_events") {
let messageIndex =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function createMessageContextSpecificObjectStore<T>(init: () => T) {
}
return false;
},
clear: (): void => store.set(new MessageContextMap<T>()),
clear: (newValue?: MessageContextMap<T>): void =>
store.set(newValue ?? new MessageContextMap<T>()),
};
}
47 changes: 22 additions & 25 deletions frontend/openchat-client/src/stores/failedMessages.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
import { writable } from "svelte/store";
import { type EventWrapper, type Message, type MessageContext, MessageContextMap } from "openchat-shared";
import {
type EventWrapper,
type Message,
type MessageContext,
MessageContextMap,
} from "openchat-shared";
import { createMessageContextSpecificObjectStore } from "./dataByMessageContextFactory";

type FailedMessageState = Record<number, EventWrapper<Message>>;
type FailedMessageState = Record<string, EventWrapper<Message>>;
export type FailedMessages = MessageContextMap<FailedMessageState>;

function createFailedMessagesStore() {
const store = writable<FailedMessages>(new MessageContextMap<FailedMessageState>());
let storeValue: FailedMessages = new MessageContextMap<FailedMessageState>();
store.subscribe((v) => (storeValue = v));
const store = createMessageContextSpecificObjectStore(() => ({}) as FailedMessageState);

return {
subscribe: store.subscribe,
getMessages: (key: MessageContext): EventWrapper<Message>[] => {
const chatState = storeValue.get(key);
const chatState = store.get(key);
return chatState ? Object.values(chatState) : [];
},
add: (key: MessageContext, message: EventWrapper<Message>): void => {
store.update((state) => {
const chatState = state.get(key) ?? {};
state.set(key, {
...chatState,
[Number(message.event.messageId)]: message,
});
return state;
});
store.update(key, (chatState) => ({
...chatState,
[message.event.messageId.toString()]: message,
}));
},
contains: (key: MessageContext, messageId: bigint): boolean => {
const chatState = storeValue.get(key);
return chatState ? chatState[Number(messageId)] !== undefined : false;
const chatState = store.get(key);
return chatState ? chatState[messageId.toString()] !== undefined : false;
},
delete: (key: MessageContext, messageId: bigint): boolean => {
const chatState = storeValue.get(key);
if (chatState && chatState[Number(messageId)]) {
delete chatState[Number(messageId)];
store.update((state) => {
state.set(key, { ...chatState });
return state;
});
const chatState = store.get(key);
const messageIdString = messageId.toString();
if (chatState && chatState[messageIdString]) {
delete chatState[messageIdString];
store.set(key, chatState);
return true;
}
return false;
},
initialise(data: FailedMessages) {
store.set(data);
store.clear(data);
},
};
}
Expand Down

0 comments on commit d000cb1

Please sign in to comment.