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:
2026-05-28 12:53:57 -04:00
parent cabd73287a
commit a37dad33c7
12 changed files with 80 additions and 129 deletions
@@ -0,0 +1,28 @@
import { ref } from 'vue'
// Owns the loading/error lifecycle shared by most Pinia stores. `run`
// flips loading on, clears error, runs `fn`, captures any throw into error
// (swallowed so callers can read `store.error`), and always resets loading.
// Returns fn's resolved value on success, or undefined if it threw.
//
// `errorAs` matches each store's existing convention so migrations don't
// change what `store.error` holds: 'raw' keeps the thrown object (the
// ApiError, with .status/.body), 'message' keeps just the string.
export function useAsyncAction({ errorAs = 'raw' } = {}) {
const loading = ref(false)
const error = ref(null)
async function run(fn) {
loading.value = true
error.value = null
try {
return await fn()
} catch (e) {
error.value = errorAs === 'message' ? (e?.message ?? String(e)) : e
} finally {
loading.value = false
}
}
return { loading, error, run }
}
+4 -10
View File
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
const PAGE = 60
@@ -8,8 +9,7 @@ export const useArtistDirectoryStore = defineStore('artistDirectory', () => {
const api = useApi()
const cards = ref([])
const nextCursor = ref(null)
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
const q = ref('')
const platform = ref(null)
let started = false
@@ -17,9 +17,7 @@ export const useArtistDirectoryStore = defineStore('artistDirectory', () => {
async function loadMore() {
if (loading.value) return
if (started && nextCursor.value === null) return
loading.value = true
error.value = null
try {
await run(async () => {
const params = { limit: PAGE }
if (q.value) params.q = q.value
if (platform.value) params.platform = platform.value
@@ -28,11 +26,7 @@ export const useArtistDirectoryStore = defineStore('artistDirectory', () => {
cards.value.push(...body.cards)
nextCursor.value = body.next_cursor
started = true
} catch (e) {
error.value = e.message
} finally {
loading.value = false
}
})
}
async function reset() {
+4 -10
View File
@@ -1,28 +1,22 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
export const useCredentialsStore = defineStore('credentials', () => {
const api = useApi()
const byPlatform = ref(new Map())
const extensionKey = ref(null)
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction()
async function loadAll() {
loading.value = true
error.value = null
try {
await run(async () => {
const arr = await api.get('/api/credentials')
const m = new Map()
for (const c of arr) m.set(c.platform, c)
byPlatform.value = m
} catch (e) {
error.value = e
} finally {
loading.value = false
}
})
}
async function upload(platform, credential_type, data) {
+6 -17
View File
@@ -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) {
+4 -10
View File
@@ -1,23 +1,17 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
export const useMLStore = defineStore('ml', () => {
const api = useApi()
const settings = ref(null)
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
async function loadSettings() {
loading.value = true
error.value = null
try {
await run(async () => {
settings.value = await api.get('/api/ml/settings')
} catch (e) {
error.value = e.message
} finally {
loading.value = false
}
})
}
async function patchSettings(patch) {
+5 -11
View File
@@ -2,14 +2,14 @@ import { defineStore } from 'pinia'
import { toast } from '../utils/toast.js'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
export const useModalStore = defineStore('modal', () => {
const api = useApi()
const currentImageId = ref(null)
const current = ref(null)
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
// Post-scoped cycle. When set, prev/next cycles within this array
// (used by PostCard's expanded-mosaic PostImageGrid clicks). When
@@ -20,8 +20,7 @@ export const useModalStore = defineStore('modal', () => {
async function open (id, opts = {}) {
currentImageId.value = id
loading.value = true
error.value = null
current.value = null // cleared upfront so it stays null on error
// Update post-scoped state if caller passed it; otherwise clear so
// the next open() from gallery context uses neighbors mode.
if (opts.postImageIds != null) {
@@ -32,14 +31,9 @@ export const useModalStore = defineStore('modal', () => {
postImageIds.value = null
postImageIndex.value = 0
}
try {
await run(async () => {
current.value = await api.get(`/api/gallery/image/${id}`)
} catch (e) {
error.value = e.message
current.value = null
} finally {
loading.value = false
}
})
}
async function close () {
+4 -10
View File
@@ -1,15 +1,15 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
export const usePostsStore = defineStore('posts', () => {
const api = useApi()
const items = ref([])
const cursor = ref(null)
const loading = ref(false)
const { loading, error, run } = useAsyncAction()
const done = ref(false)
const error = ref(null)
const filters = ref({ artist_id: null, platform: null })
function _qs() {
@@ -38,18 +38,12 @@ export const usePostsStore = defineStore('posts', () => {
async function loadMore() {
if (loading.value || done.value) return
loading.value = true
error.value = null
try {
await run(async () => {
const body = await api.get('/api/posts', { params: _qs() })
items.value.push(...body.items)
cursor.value = body.next_cursor
if (body.next_cursor == null) done.value = true
} catch (e) {
error.value = e
} finally {
loading.value = false
}
})
}
async function getPostFull(id) {
+6 -12
View File
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
// IR reader.js "viewport-center-in-third" rule, pure for unit tests.
// metrics: [{page_number, top, height}] in page order.
@@ -33,23 +34,16 @@ export const useSeriesReaderStore = defineStore('seriesReader', () => {
const series = ref(null)
const pages = ref([])
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
async function load(tagId) {
loading.value = true
error.value = null
try {
series.value = null // cleared upfront so they stay reset on error
pages.value = []
await run(async () => {
const body = await api.get(`/api/series/${tagId}/pages`)
series.value = body.series
pages.value = body.pages
} catch (e) {
error.value = e.message
series.value = null
pages.value = []
} finally {
loading.value = false
}
})
}
return { series, pages, loading, error, load }
+4 -10
View File
@@ -1,32 +1,26 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
const PAGE = 60
export const useShowcaseStore = defineStore('showcase', () => {
const api = useApi()
const images = ref([])
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
const exhausted = ref(false)
const seen = new Set()
async function fetchPage() {
if (loading.value) return
loading.value = true
error.value = null
try {
await run(async () => {
const body = await api.get('/api/showcase', { params: { limit: PAGE } })
const fresh = body.images.filter(i => !seen.has(i.id))
if (fresh.length === 0) { exhausted.value = true; return }
for (const i of fresh) seen.add(i.id)
images.value.push(...fresh)
} catch (e) {
error.value = e.message
} finally {
loading.value = false
}
})
}
async function shuffle() {
+6 -18
View File
@@ -1,42 +1,30 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
export const useSourcesStore = defineStore('sources', () => {
const api = useApi()
// keyed cache: null => all sources, number => artist_id filtered
const byArtist = ref(new Map())
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction()
const scheduleStatus = ref(null)
const allSources = computed(() => byArtist.value.get(null) ?? [])
async function loadAll() {
loading.value = true
error.value = null
try {
await run(async () => {
const body = await api.get('/api/sources')
byArtist.value.set(null, body)
} catch (e) {
error.value = e
} finally {
loading.value = false
}
})
}
async function loadForArtist(artistId) {
loading.value = true
error.value = null
try {
await run(async () => {
const body = await api.get('/api/sources', { params: { artist_id: artistId } })
byArtist.value.set(artistId, body)
} catch (e) {
error.value = e
} finally {
loading.value = false
}
})
}
function _invalidate(artistId) {
+5 -11
View File
@@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
import { toast } from '../utils/toast.js'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
// Category display order: people/sources first, general last.
export const CATEGORY_ORDER = ['artist', 'character', 'copyright', 'general']
@@ -15,23 +16,16 @@ export const CATEGORY_LABELS = {
export const useSuggestionsStore = defineStore('suggestions', () => {
const api = useApi()
const byCategory = ref({}) // { category: [suggestion, ...] }
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
let currentImageId = null
async function load(imageId) {
currentImageId = imageId
loading.value = true
error.value = null
try {
byCategory.value = {} // cleared upfront so it stays empty on error
await run(async () => {
const body = await api.get(`/api/images/${imageId}/suggestions`)
byCategory.value = body.by_category || {}
} catch (e) {
error.value = e.message
byCategory.value = {}
} finally {
loading.value = false
}
})
}
function _drop(category, predicate) {
+4 -10
View File
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
const PAGE = 60
@@ -8,8 +9,7 @@ export const useTagDirectoryStore = defineStore('tagDirectory', () => {
const api = useApi()
const cards = ref([])
const nextCursor = ref(null)
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
const kind = ref(null)
const q = ref('')
let started = false
@@ -17,9 +17,7 @@ export const useTagDirectoryStore = defineStore('tagDirectory', () => {
async function loadMore() {
if (loading.value) return
if (started && nextCursor.value === null) return
loading.value = true
error.value = null
try {
await run(async () => {
const params = { limit: PAGE }
if (kind.value) params.kind = kind.value
if (q.value) params.q = q.value
@@ -28,11 +26,7 @@ export const useTagDirectoryStore = defineStore('tagDirectory', () => {
cards.value.push(...body.cards)
nextCursor.value = body.next_cursor
started = true
} catch (e) {
error.value = e.message
} finally {
loading.value = false
}
})
}
async function reset() {