Skip to content

Commit

Permalink
fix: footer link random pick friend infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyxguo committed Aug 21, 2023
1 parent 36dd8df commit 425ec7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/components/Footer/FooterLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ import { useAppStore } from '@/stores/app'
import { useArticleStore } from '@/stores/article'
import { PropType, computed, defineComponent, onMounted, ref, watch } from 'vue'
import SvgIcon from '@/components/SvgIcon/index.vue'
import { shuffleArray } from '@/utils'
enum FriendLinkRandomMode {
'RANDOM' = 'random',
'SHUFFLE' = 'SHUFFLE'
}
export default defineComponent({
name: 'ARFooterLink',
Expand All @@ -63,21 +69,38 @@ export default defineComponent({
const loadingLinks = ref<boolean>(true)
const linksData = ref<Link[]>([])
const bloggers = ref<Link[][]>([])
const maxShuffleLimit = 100
const refreshLinkData = () => {
loadingLinks.value = true
linksData.value = []
let uniqueBloggers = [
...new Map(bloggers.value.flat().map(m => [m.nick, m])).values()
]
setTimeout(() => {
const recordSet = new Set()
let friendsCount = bloggers.value.length < 5 ? bloggers.value.length : 5
let friendsCount = uniqueBloggers.length < 5 ? uniqueBloggers.length : 5
let mode: FriendLinkRandomMode = FriendLinkRandomMode.SHUFFLE
if (uniqueBloggers.length > maxShuffleLimit) {
mode = FriendLinkRandomMode.RANDOM
}
if (mode === FriendLinkRandomMode.SHUFFLE) {
uniqueBloggers = shuffleArray(uniqueBloggers)
}
while (friendsCount > 0) {
const pair = bloggers.value[Math.floor(Math.random() * 24)]
const blogger = pair[Math.floor(Math.random() * 2)]
if (!recordSet.has(blogger.nick)) {
recordSet.add(blogger.nick)
linksData.value.push(blogger)
if (mode === FriendLinkRandomMode.SHUFFLE) {
linksData.value.push(uniqueBloggers[friendsCount - 1])
friendsCount--
} else {
const blogger =
uniqueBloggers[Math.floor(Math.random() * friendsCount)]
if (!recordSet.has(blogger.nick)) {
recordSet.add(blogger.nick)
linksData.value.push(blogger)
friendsCount--
}
}
}
loadingLinks.value = false
Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,11 @@ export function cleanPath(path: string) {

return path
}

export function shuffleArray<T = any>(array: T[]): T[] {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
return array
}

0 comments on commit 425ec7c

Please sign in to comment.