fix(phase3): add $autoCancel:false to prevent SDK auto-cancellation

PocketBase JS SDK auto-cancels pending requests when a new request
targets the same collection. This causes errors when loadLedgers and
getLedgerSummary run in parallel via Promise.all. Added $autoCancel:false
to all API calls in ledgers.ts and assets.ts, matching project convention.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
congsh
2026-04-18 20:05:27 +08:00
parent e4b730c8db
commit dc11ef90fd
2 changed files with 24 additions and 23 deletions
+7 -6
View File
@@ -25,14 +25,15 @@ export async function createAsset(data: CreateAssetData): Promise<Asset> {
if (data.description) formData.append('description', data.description)
if (data.image) formData.append('image', data.image)
return pb.collection('assets').create(formData) as Promise<Asset>
return pb.collection('assets').create(formData, { $autoCancel: false }) as Promise<Asset>
}
export async function listAssets(groupId: string): Promise<Asset[]> {
const result = await pb.collection('assets').getFullList({
filter: `group="${groupId}"`,
sort: 'created',
expand: 'creator,currentHolder'
expand: 'creator,currentHolder',
$autoCancel: false
})
return result as unknown as Asset[]
}
@@ -49,20 +50,20 @@ export async function updateAsset(
if (data.description !== undefined) formData.append('description', data.description)
formData.append('image', image)
return pb.collection('assets').update(assetId, formData) as Promise<Asset>
return pb.collection('assets').update(assetId, formData, { $autoCancel: false }) as Promise<Asset>
}
return pb.collection('assets').update(assetId, data) as Promise<Asset>
return pb.collection('assets').update(assetId, data, { $autoCancel: false }) as Promise<Asset>
}
export async function transferAsset(assetId: string, userId: string): Promise<Asset> {
return pb.collection('assets').update(assetId, {
currentHolder: userId
}) as Promise<Asset>
}, { $autoCancel: false }) as Promise<Asset>
}
export async function deleteAsset(assetId: string): Promise<void> {
await pb.collection('assets').delete(assetId)
await pb.collection('assets').delete(assetId, { $autoCancel: false })
}
export function subscribeAssets(
+17 -17
View File
@@ -32,7 +32,7 @@ export async function createLedger(data: CreateLedgerData): Promise<Ledger> {
...data,
creator: user?.id || '',
relatedMembers: data.relatedMembers || []
})
}, { $autoCancel: false })
return record as unknown as Ledger
}
@@ -45,11 +45,9 @@ export async function listLedgers(
if (type) filter += ` && type="${type}"`
if (category) filter += ` && category="${category}"`
if (month) {
// month format: "2026-04"
const start = `${month}-01 00:00:00`
const year = parseInt(month.slice(0, 4))
const mon = parseInt(month.slice(5, 7))
// 下个月的第一天
const nextMonth = mon === 12 ? `${year + 1}-01` : `${year}-${String(mon + 1).padStart(2, '0')}`
const end = `${nextMonth}-01 00:00:00`
filter += ` && occurredAt>="${start}" && occurredAt<"${end}"`
@@ -58,7 +56,8 @@ export async function listLedgers(
const result = await pb.collection('ledgers').getList(page, limit, {
filter,
sort: '-occurredAt',
expand: 'creator,relatedMembers'
expand: 'creator,relatedMembers',
$autoCancel: false
})
return { items: result.items as unknown as Ledger[], total: result.totalItems }
}
@@ -67,12 +66,12 @@ export async function updateLedger(
ledgerId: string,
data: Partial<CreateLedgerData>
): Promise<Ledger> {
const record = await pb.collection('ledgers').update(ledgerId, data)
const record = await pb.collection('ledgers').update(ledgerId, data, { $autoCancel: false })
return record as unknown as Ledger
}
export async function deleteLedger(ledgerId: string): Promise<void> {
await pb.collection('ledgers').delete(ledgerId)
await pb.collection('ledgers').delete(ledgerId, { $autoCancel: false })
}
export async function getLedgerSummary(
@@ -85,20 +84,21 @@ export async function getLedgerSummary(
const batchSize = 500
let hasMore = true
while (hasMore) {
let filter = `group="${groupId}"`
if (month) {
const start = `${month}-01 00:00:00`
const year = parseInt(month.slice(0, 4))
const mon = parseInt(month.slice(5, 7))
const nextMonth = mon === 12 ? `${year + 1}-01` : `${year}-${String(mon + 1).padStart(2, '0')}`
const end = `${nextMonth}-01 00:00:00`
filter += ` && occurredAt>="${start}" && occurredAt<"${end}"`
}
let filter = `group="${groupId}"`
if (month) {
const start = `${month}-01 00:00:00`
const year = parseInt(month.slice(0, 4))
const mon = parseInt(month.slice(5, 7))
const nextMonth = mon === 12 ? `${year + 1}-01` : `${year}-${String(mon + 1).padStart(2, '0')}`
const end = `${nextMonth}-01 00:00:00`
filter += ` && occurredAt>="${start}" && occurredAt<"${end}"`
}
while (hasMore) {
const result = await pb.collection('ledgers').getList(page, batchSize, {
filter,
fields: 'type,amount'
fields: 'type,amount',
$autoCancel: false
})
for (const item of result.items as any[]) {