Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(payments): skip polling while status refresh is pending

Timothy Yin230b4ad5cf91
diff --git a/apps/cloud/app/composables/useCloudPayments.ts b/apps/cloud/app/composables/useCloudPayments.ts
index 5fe1ee81..20d3753b 100644
--- a/apps/cloud/app/composables/useCloudPayments.ts
+++ b/apps/cloud/app/composables/useCloudPayments.ts
@@ -7,11 +7,16 @@ export function useCloudPayments(
busy = ref(false),
error = ref('')
let generation = 0,
- alive = true
+ alive = true,
+ pending = false
async function refresh() {
const current = ++generation,
id = Number(toValue(workspaceId))
- if (!id) return
+ if (!id) {
+ pending = false
+ return
+ }
+ pending = true
try {
const result = await $fetch('/api/billing/payments', {
query: { workspaceId: id },
@@ -24,6 +29,8 @@ export function useCloudPayments(
} catch {
if (alive && current === generation)
error.value = t('cloud.payments.failed')
+ } finally {
+ if (current === generation) pending = false
}
}
watch(
@@ -40,7 +47,7 @@ export function useCloudPayments(
generation++
})
useIntervalFn(() => {
- if (document.visibilityState === 'visible') void refresh()
+ if (document.visibilityState === 'visible' && !pending) void refresh()
}, 15000)
return { data, busy, error, refresh }
}
diff --git a/tests/portal-review-regressions.test.ts b/tests/portal-review-regressions.test.ts
index 35205fa1..fe3ccf89 100644
--- a/tests/portal-review-regressions.test.ts
+++ b/tests/portal-review-regressions.test.ts
@@ -6,7 +6,16 @@ import { createServer } from 'node:http'
import { createRequire } from 'node:module'
import ts from 'typescript'
import { isAlbumPagePath } from '../packages/gallery/shared/utils/albumPath.ts'
-import { ref, reactive, computed, readonly, watch, effectScope } from 'vue'
+import {
+ ref,
+ reactive,
+ computed,
+ readonly,
+ watch,
+ effectScope,
+ toValue,
+ nextTick,
+} from 'vue'
function load(
file: string,
@@ -487,3 +496,61 @@ test('billing refresh aborts stalled workspace, plan and payment reads', async (
)
}
})
+
+test('payments polling preserves in-flight timeout errors and retries without superseding workspace refreshes', async () => {
+ const requests: {
+ resolve: (value: any) => void
+ reject: (error: Error) => void
+ }[] = []
+ let poll!: () => void
+ const workspaceId = ref(1)
+ const module = load('apps/cloud/app/composables/useCloudPayments.ts', {
+ toValue,
+ document: { visibilityState: 'visible' },
+ $fetch: () =>
+ new Promise((resolve, reject) => requests.push({ resolve, reject })),
+ onMounted: () => {},
+ onBeforeUnmount: () => {},
+ useIntervalFn: (callback: () => void) => {
+ poll = callback
+ },
+ })
+ const flush = async () => {
+ for (let n = 0; n < 8; n++) await nextTick()
+ }
+ const scope = effectScope()
+ try {
+ const payments = scope.run(() => module.useCloudPayments(workspaceId))!
+ const first = payments.refresh()
+ poll() // The timer fires just before the existing request times out.
+ assert.equal(requests.length, 1)
+ requests[0]!.reject(new Error('Request timeout'))
+ await first
+ assert.ok(payments.error.value)
+ poll() // The following tick retries the failed request.
+ assert.equal(requests.length, 2)
+ requests[1]!.resolve({ workspaceId: 1 })
+ await flush()
+ assert.equal(payments.error.value, '')
+ assert.equal(payments.data.value.workspaceId, 1)
+ const stale = payments.refresh()
+ workspaceId.value = 2
+ await flush()
+ assert.equal(requests.length, 4)
+ requests[2]!.resolve({ workspaceId: 1 })
+ await stale
+ // Finishing the superseded request must not clear the new request's pending flag.
+ poll()
+ assert.equal(requests.length, 4)
+ assert.equal(payments.data.value, null)
+ requests[3]!.resolve({ workspaceId: 2 })
+ await flush()
+ assert.equal(payments.data.value.workspaceId, 2)
+ poll()
+ assert.equal(requests.length, 5)
+ requests[4]!.resolve({ workspaceId: 2 })
+ await flush()
+ } finally {
+ scope.stop()
+ }
+})