Skip to content

Commit

Permalink
perf: optimize performance experience
Browse files Browse the repository at this point in the history
  • Loading branch information
Linkontoask committed May 5, 2021
1 parent 8aeea89 commit b2860db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 44 deletions.
46 changes: 2 additions & 44 deletions src/hooks/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useStore } from 'vuex'
import { on, off, isPromise } from '@/utils/index'
import { useRoute } from 'vue-router'
import { watch, toRaw } from 'vue'
import { useIntervalFn } from '@vueuse/core'
import equal from 'lodash/isEqual'
import cloneDeep from 'lodash/cloneDeep'
import store from '@/store/index'
Expand All @@ -21,51 +22,8 @@ interface DragOptions {
vertical?: boolean
}

export const sleep = (ms: number) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(undefined)
}, ms)
})
}

export const animation = (cb: () => unknown, ms: number) => {
let id: number,
isStop = false
const invoke = async () => {
await sleep(ms)
if (!isStop) {
const r = cb()
if (r instanceof Promise) {
r.then(() => {
cancelAnimationFrame(id)
id = requestAnimationFrame(invoke)
})
} else {
cancelAnimationFrame(id)
id = requestAnimationFrame(invoke)
}
}
}

const stop = () => {
isStop = true
cancelAnimationFrame(id)
}

const start = () => {
isStop = false
invoke()
}

return {
stop,
start
}
}

export const useInternal = (ms: number, cb: () => unknown): InternalHook => {
const { start, stop } = animation(cb, ms)
const { start, stop } = useIntervalFn(cb, ms)
return {
startInternal: start,
stopInternal: stop
Expand Down
6 changes: 6 additions & 0 deletions src/pages/news/children/recommend/view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export const Recommend = defineComponent({
}
}

const handleVisibilityChange = () => {
runningSwiper.value = !document.hidden
}

document.addEventListener('visibilitychange', handleVisibilityChange, false)

onActivated(() => {
runningSwiper.value = true
})
Expand Down

0 comments on commit b2860db

Please sign in to comment.