Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate failedMessages to use shared store logic #5043

Merged
merged 5 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading