Skip to content

Commit

Permalink
fix(monkey): fetch options
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Feb 26, 2024
1 parent 1901b2e commit 5ff6b09
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
12 changes: 9 additions & 3 deletions apps/monkey/src/Config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const dateRange = computed({
return configStore.state.dateRange
},
set(val: [number, number]) {
configStore.state.dateRange = val ?? []
configStore.state.dateRange = val ?? [Date.now(), Date.now()]
configStore.state.isFetchAll = false
},
})
Expand All @@ -31,8 +32,7 @@ configStore.setConfig({ now: Date.now() })
<button
class="bg-#18a058 py-1 btn hover:bg-green-7"
@click="() => {
const { now } = configStore.state
dateRange = [now, now]
configStore.state.isFetchAll = true
}"
>
重置为所有微博
Expand Down Expand Up @@ -70,3 +70,9 @@ configStore.setConfig({ now: Date.now() })
</div>
</div>
</template>

<style>
.n-base-clear {
display: none !important;
}
</style>
24 changes: 14 additions & 10 deletions apps/monkey/src/Ctrl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const isFinish = ref(false)
const percentage = computed(() => postStore.posts.length / postStore.total * 100)
const progressText = computed(() => () => `${postStore.posts.length}/${postStore.total} 条`)
let pauseFetch = () => {}
let resumeFetch = async () => {}
const pauseFn = ref<() => void>()
const resumeFn = ref<() => void>()
async function start() {
message.info('开始爬取中,请稍等~', {
Expand All @@ -30,7 +30,7 @@ async function start() {
isStop.value = false
const { pause, resume } = await fetchPosts({
startPage: postStore.fetchedPage,
startPage: () => postStore.fetchedPage + 1,
isFetchAll: configStore.state.isFetchAll,
setTotal: total => postStore.total = total,
addPosts: postStore.add,
Expand All @@ -45,15 +45,19 @@ async function start() {
return finished
},
})
pauseFetch = pause
resumeFetch = resume
pauseFn.value = pause
resumeFn.value = resume
}
watchEffect(() => {
if (isStop.value)
pauseFetch()
watch(isStop, (val, oldVal) => {
if (val === oldVal)
return
if (val)
pauseFn.value?.()
else
resumeFetch()
resumeFn.value?.()
})
// @ts-expect-error TODO: fix this
Expand Down Expand Up @@ -100,7 +104,7 @@ onMounted(async () => {
@click="start"
>
{{ isStart ? '重新开始' : '开始' }}
(获取{{ configStore.state.isFetchAll ? '全部' : '部分' }}微博)
(获取 <strong>{{ configStore.state.isFetchAll ? '全部' : '部分' }}</strong> 微博)
</button>

<div
Expand Down
5 changes: 0 additions & 5 deletions apps/monkey/src/stores/configStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const useConfigStore = defineStore('config', () => {
const state = reactive<FetchOptions>(localData
? JSON.parse(localData)
: {
now,
uid: '',
name: '',
cookie: '',
Expand All @@ -26,10 +25,6 @@ export const useConfigStore = defineStore('config', () => {
Object.assign(state, config)
}

watchImmediate(() => state.dateRange, (newVal) => {
state.isFetchAll = newVal.every(v => v === state.now)
})

watchEffect(() => {
localStorage.setItem('fetchOptions', JSON.stringify(state))
})
Expand Down
4 changes: 2 additions & 2 deletions apps/monkey/src/stores/postStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const usePostStore = defineStore('post', () => {
const posts = ref([] as Post[])

// 已获取的页数
const fetchedPage = ref(1)
const fetchedPage = ref(0)
// 每页显示的帖子数量 ppp
const postsPerPage = ref(20)

Expand All @@ -21,7 +21,7 @@ export const usePostStore = defineStore('post', () => {
function reset() {
posts.value = []
postsPerPage.value = 20
fetchedPage.value = 1
fetchedPage.value = 0
}

/**
Expand Down
25 changes: 12 additions & 13 deletions packages/core/src/services/postService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function fetchComments(post: Post): Promise<Comment[]> {
}

interface FetchPosts {
startPage: number
startPage: () => number
isFetchAll: boolean
setTotal: (total: number) => void
addPosts: (posts: Post[]) => void
Expand All @@ -105,24 +105,23 @@ interface FetchPosts {
export async function fetchPosts(
{ startPage, isFetchAll, setTotal, addPosts, stopCondition }: FetchPosts,
) {
let page = startPage
const res = isFetchAll
? await fetchAllPosts(page)
: await fetchRangePosts(page)
async function fetching() {
const page = startPage()
const res = isFetchAll
? await fetchAllPosts(page)
: await fetchRangePosts(page)
addPosts(res?.list || [])
console.log(`已获取第 ${page} 页`)
return res
}

const res = await fetching()
// 先获取总页数
setTotal(res?.total || 0)
addPosts(res?.list || [])

const { startLoop, resume, pause } = usePausableLoop(
async () => {
page++
const data = isFetchAll
? await fetchAllPosts(page)
: await fetchRangePosts(page)

addPosts(data?.list || [])
console.log(`已获取第 ${page} 页`)
await fetching()

// 如果已经获取到所有帖子
if (stopCondition())
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ export function usePausableLoop(
}

function pause() {
console.log('已暂停')
_isPaused = true
}

async function resume() {
console.log('已恢复')
_isPaused = false
await startLoop()
}
Expand Down

0 comments on commit 5ff6b09

Please sign in to comment.