Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

Commit

Permalink
fix: set isCancel flag to cancel async task in effect
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Jun 10, 2023
1 parent 601d64c commit fb3e95e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/components/common/Mermaid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { memo, useEffect, useId, useState } from 'react'

import { useAppStore } from '~/atoms/app'
import { ImageLazy } from '~/components/ui/Image'
import { useIsUnMounted } from '~/hooks/common/use-is-unmounted'

export const Mermaid: FC<{
content: string
Expand All @@ -13,7 +12,6 @@ export const Mermaid: FC<{
const [svg, setSvg] = useState('')
const [width, setWidth] = useState<number>()
const [height, setHeight] = useState<number>()
const isUnmounted = useIsUnMounted()

const isDark = useAppStore((state) => state.colorMode === 'dark')

Expand All @@ -35,6 +33,8 @@ export const Mermaid: FC<{
setError('')
setLoading(true)

let isCanceled = false

import('mermaid').then(async (mo) => {
const mermaid = mo.default

Expand All @@ -51,7 +51,7 @@ export const Mermaid: FC<{
setHeight(undefined)
}

if (isUnmounted.current) return
if (isCanceled) return

if (result) {
setSvg(result.svg)
Expand All @@ -64,6 +64,9 @@ export const Mermaid: FC<{
setError('')
}
setLoading(false)
return () => {
isCanceled = true
}
})
}, [id, props.content])

Expand Down
10 changes: 6 additions & 4 deletions src/components/in-page/Note/NoteTimelineList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { MaterialSymbolsArrowCircleRightOutlineRounded } from '~/components/ui/I
import { LeftToRightTransitionView } from '~/components/ui/Transition/LeftToRightTransitionView'
import { TrackerAction } from '~/constants/tracker'
import { useAnalyze } from '~/hooks/app/use-analyze'
import { useIsUnMounted } from '~/hooks/common/use-is-unmounted'
import { useDetectIsNarrowThanLaptop } from '~/hooks/ui/use-viewport'
import { apiClient } from '~/utils/client'
import { springScrollToTop } from '~/utils/spring'
Expand Down Expand Up @@ -71,27 +70,30 @@ const ObserveredNoteTimelineList: FC<
]
})

const isUnmount = useIsUnMounted()
useEffect(() => {
let isCanceled = false
async function fetchList() {
const scrollTop = document.documentElement.scrollTop

if (scrollTop > 0)
// waiting scroll to top
await new Promise((resolve) => setTimeout(resolve, WAITING_SCROLL_TIME))

if (isUnmount.current) return
if (isCanceled) return
const data = await apiClient.note
.getMiddleList(noteId, 10)
.then(({ data }) => {
return data
})
if (isUnmount.current) return
if (isCanceled) return

setList(data)
}
fetchList()
// eslint-disable-next-line react-hooks/exhaustive-deps
return () => {
isCanceled = true
}
}, [noteId])

const triggerComponent = useMemo(
Expand Down

1 comment on commit fb3e95e

@vercel
Copy link

@vercel vercel bot commented on fb3e95e Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kami – ./

dev.innei.ren
kami-psi.vercel.app
kami-git-master-innei-dev.vercel.app
kami-innei-dev.vercel.app

Please sign in to comment.