Skip to content

Commit

Permalink
refactor: fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
shaokeyibb committed Sep 29, 2024
1 parent 411c9ef commit 35837be
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions frontend/lib/dom/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ export const closeCommentsPanel = () => {
export const submitComment = async ({
offsets,
content,
onPendingFulfilled,
}: {
offsets: [number, number];
content: string;
onPendingFulfilled?: () => Promise<void>;
}) => {
const commitHash = sessionStorage.getItem("commitHash");
if (!commitHash) {
Expand Down Expand Up @@ -243,7 +245,7 @@ export const submitComment = async ({
pending: true,
});
}
await openCommentsPanel();
await onPendingFulfilled?.();
const resp = await res;

if (!resp.ok) {
Expand All @@ -266,9 +268,11 @@ export const submitComment = async ({
export const modifyComment = async ({
id,
comment,
onPendingFulfilled,
}: {
id: number;
comment: string;
onPendingFulfilled?: () => Promise<void>;
}) => {
const res = fetch(
`${apiEndpoint}comment/${encodeURIComponent(new URL(window.location.href).pathname)}/id/${id}`,
Expand All @@ -290,7 +294,7 @@ export const modifyComment = async ({
);
}

await openCommentsPanel();
onPendingFulfilled?.();
const resp = await res;

if (!resp.ok) {
Expand Down Expand Up @@ -730,6 +734,9 @@ export const renderComments = async (comments: Comment[]) => {
parseInt(selectedOffset!.dataset.originalDocumentEnd!),
],
content: textarea.value,
onPendingFulfilled: async () => {
await openCommentsPanel();
}
});

textarea.value = "";
Expand Down Expand Up @@ -810,6 +817,9 @@ export const renderComments = async (comments: Comment[]) => {
await modifyComment({
id: parseInt(id),
comment: textarea.value,
onPendingFulfilled: async () => {
await openCommentsPanel();
}
});

textarea.value = "";
Expand Down

0 comments on commit 35837be

Please sign in to comment.