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:
@@ -25,14 +25,15 @@ export async function createAsset(data: CreateAssetData): Promise<Asset> {
|
|||||||
if (data.description) formData.append('description', data.description)
|
if (data.description) formData.append('description', data.description)
|
||||||
if (data.image) formData.append('image', data.image)
|
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[]> {
|
export async function listAssets(groupId: string): Promise<Asset[]> {
|
||||||
const result = await pb.collection('assets').getFullList({
|
const result = await pb.collection('assets').getFullList({
|
||||||
filter: `group="${groupId}"`,
|
filter: `group="${groupId}"`,
|
||||||
sort: 'created',
|
sort: 'created',
|
||||||
expand: 'creator,currentHolder'
|
expand: 'creator,currentHolder',
|
||||||
|
$autoCancel: false
|
||||||
})
|
})
|
||||||
return result as unknown as Asset[]
|
return result as unknown as Asset[]
|
||||||
}
|
}
|
||||||
@@ -49,20 +50,20 @@ export async function updateAsset(
|
|||||||
if (data.description !== undefined) formData.append('description', data.description)
|
if (data.description !== undefined) formData.append('description', data.description)
|
||||||
formData.append('image', image)
|
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> {
|
export async function transferAsset(assetId: string, userId: string): Promise<Asset> {
|
||||||
return pb.collection('assets').update(assetId, {
|
return pb.collection('assets').update(assetId, {
|
||||||
currentHolder: userId
|
currentHolder: userId
|
||||||
}) as Promise<Asset>
|
}, { $autoCancel: false }) as Promise<Asset>
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteAsset(assetId: string): Promise<void> {
|
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(
|
export function subscribeAssets(
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export async function createLedger(data: CreateLedgerData): Promise<Ledger> {
|
|||||||
...data,
|
...data,
|
||||||
creator: user?.id || '',
|
creator: user?.id || '',
|
||||||
relatedMembers: data.relatedMembers || []
|
relatedMembers: data.relatedMembers || []
|
||||||
})
|
}, { $autoCancel: false })
|
||||||
return record as unknown as Ledger
|
return record as unknown as Ledger
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,11 +45,9 @@ export async function listLedgers(
|
|||||||
if (type) filter += ` && type="${type}"`
|
if (type) filter += ` && type="${type}"`
|
||||||
if (category) filter += ` && category="${category}"`
|
if (category) filter += ` && category="${category}"`
|
||||||
if (month) {
|
if (month) {
|
||||||
// month format: "2026-04"
|
|
||||||
const start = `${month}-01 00:00:00`
|
const start = `${month}-01 00:00:00`
|
||||||
const year = parseInt(month.slice(0, 4))
|
const year = parseInt(month.slice(0, 4))
|
||||||
const mon = parseInt(month.slice(5, 7))
|
const mon = parseInt(month.slice(5, 7))
|
||||||
// 下个月的第一天
|
|
||||||
const nextMonth = mon === 12 ? `${year + 1}-01` : `${year}-${String(mon + 1).padStart(2, '0')}`
|
const nextMonth = mon === 12 ? `${year + 1}-01` : `${year}-${String(mon + 1).padStart(2, '0')}`
|
||||||
const end = `${nextMonth}-01 00:00:00`
|
const end = `${nextMonth}-01 00:00:00`
|
||||||
filter += ` && occurredAt>="${start}" && occurredAt<"${end}"`
|
filter += ` && occurredAt>="${start}" && occurredAt<"${end}"`
|
||||||
@@ -58,7 +56,8 @@ export async function listLedgers(
|
|||||||
const result = await pb.collection('ledgers').getList(page, limit, {
|
const result = await pb.collection('ledgers').getList(page, limit, {
|
||||||
filter,
|
filter,
|
||||||
sort: '-occurredAt',
|
sort: '-occurredAt',
|
||||||
expand: 'creator,relatedMembers'
|
expand: 'creator,relatedMembers',
|
||||||
|
$autoCancel: false
|
||||||
})
|
})
|
||||||
return { items: result.items as unknown as Ledger[], total: result.totalItems }
|
return { items: result.items as unknown as Ledger[], total: result.totalItems }
|
||||||
}
|
}
|
||||||
@@ -67,12 +66,12 @@ export async function updateLedger(
|
|||||||
ledgerId: string,
|
ledgerId: string,
|
||||||
data: Partial<CreateLedgerData>
|
data: Partial<CreateLedgerData>
|
||||||
): Promise<Ledger> {
|
): 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
|
return record as unknown as Ledger
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteLedger(ledgerId: string): Promise<void> {
|
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(
|
export async function getLedgerSummary(
|
||||||
@@ -85,7 +84,6 @@ export async function getLedgerSummary(
|
|||||||
const batchSize = 500
|
const batchSize = 500
|
||||||
let hasMore = true
|
let hasMore = true
|
||||||
|
|
||||||
while (hasMore) {
|
|
||||||
let filter = `group="${groupId}"`
|
let filter = `group="${groupId}"`
|
||||||
if (month) {
|
if (month) {
|
||||||
const start = `${month}-01 00:00:00`
|
const start = `${month}-01 00:00:00`
|
||||||
@@ -96,9 +94,11 @@ export async function getLedgerSummary(
|
|||||||
filter += ` && occurredAt>="${start}" && occurredAt<"${end}"`
|
filter += ` && occurredAt>="${start}" && occurredAt<"${end}"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (hasMore) {
|
||||||
const result = await pb.collection('ledgers').getList(page, batchSize, {
|
const result = await pb.collection('ledgers').getList(page, batchSize, {
|
||||||
filter,
|
filter,
|
||||||
fields: 'type,amount'
|
fields: 'type,amount',
|
||||||
|
$autoCancel: false
|
||||||
})
|
})
|
||||||
|
|
||||||
for (const item of result.items as any[]) {
|
for (const item of result.items as any[]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user