refactor(dry-F1): shared useAsyncAction lifecycle helper for stores
New composables/useAsyncAction.js owns the loading/error/try-finally lifecycle. Migrated 11 stores: credentials, downloads, sources, posts (error=raw) + ml, artistDirectory, tagDirectory, showcase, suggestions, seriesReader, modal (error=message). The errorAs option preserves each store's existing error shape so store.error keeps the same type for components (~50 consumption sites unchanged). Stores whose catch also reset data (suggestions/seriesReader/modal) clear it upfront instead. Deliberately NOT migrated (special control flow, would change behavior): artist (conditional 404 catch + dual loading states), migration (rethrows), gallery (inflight-id stale-response guard), and the Shape-B no-catch / loaded-guard / keyed-cache stores. Net -77 lines. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { useApi } from '../composables/useApi.js'
|
||||
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||
|
||||
export const useDownloadsStore = defineStore('downloads', () => {
|
||||
const api = useApi()
|
||||
@@ -13,8 +14,7 @@ export const useDownloadsStore = defineStore('downloads', () => {
|
||||
from_date: null, to_date: null,
|
||||
})
|
||||
const selected = ref(null)
|
||||
const loading = ref(false)
|
||||
const error = ref(null)
|
||||
const { loading, error, run } = useAsyncAction()
|
||||
const stats = ref({ pending: 0, running: 0, ok: 0, error: 0, skipped: 0 })
|
||||
const activity = ref({ hours: 24, buckets: [] })
|
||||
const failing = ref([])
|
||||
@@ -28,33 +28,22 @@ export const useDownloadsStore = defineStore('downloads', () => {
|
||||
}
|
||||
|
||||
async function loadFirst() {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
await run(async () => {
|
||||
const body = await api.get('/api/downloads', { params: _params() })
|
||||
events.value = body
|
||||
cursor.value = body.length ? body[body.length - 1].id : null
|
||||
hasMore.value = body.length === 50
|
||||
} catch (e) {
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (!hasMore.value || cursor.value == null) return
|
||||
loading.value = true
|
||||
try {
|
||||
await run(async () => {
|
||||
const body = await api.get('/api/downloads', { params: _params({ before: cursor.value }) })
|
||||
events.value.push(...body)
|
||||
cursor.value = body.length ? body[body.length - 1].id : cursor.value
|
||||
hasMore.value = body.length === 50
|
||||
} catch (e) {
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function loadOne(id) {
|
||||
|
||||
Reference in New Issue
Block a user