Skip to content

Commit 0622689

Browse files
committed
cleanup: reduce code complexity checking the nested comment update result instead of preventively reading the fragment
1 parent 6279a1b commit 0622689

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

components/use-live-comments.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ export function itemUpdateQuery (client, id, sort, fn) {
6060

6161
// update the newComments field of a nested comment fragment
6262
export function commentUpdateFragment (client, id, fn) {
63-
client.cache.updateFragment({
63+
const result = client.cache.updateFragment({
6464
id: `Item:${id}`,
6565
fragment: COMMENT_WITH_NEW,
6666
fragmentName: 'CommentWithNew'
6767
}, (data) => {
6868
if (!data) return data
6969
return fn(data)
7070
})
71+
72+
return result
7173
}
7274

7375
function cacheNewComments (client, rootId, newComments, sort) {
@@ -83,17 +85,10 @@ function cacheNewComments (client, rootId, newComments, sort) {
8385
itemUpdateQuery(client, rootId, sort, (data) => mergeNewComment(data, newComment))
8486
} else {
8587
// if the comment is a reply, update the parent comment
86-
// but first check if parent exists in cache before attempting update
87-
const parentExists = client.cache.readFragment({
88-
id: `Item:${parentId}`,
89-
fragment: COMMENT_WITH_NEW,
90-
fragmentName: 'CommentWithNew'
91-
})
88+
// merge the new comment into the parent comment's newComments field, checking for duplicates
89+
const result = commentUpdateFragment(client, parentId, (data) => mergeNewComment(data, newComment))
9290

93-
if (parentExists) {
94-
// merge the new comment into the parent comment's newComments field, checking for duplicates
95-
commentUpdateFragment(client, parentId, (data) => mergeNewComment(data, newComment))
96-
} else {
91+
if (!result) {
9792
// parent not in cache, queue for retry
9893
queuedComments.push(newComment)
9994
}

0 commit comments

Comments
 (0)