fix: complete scoped lifecycle simplification

This commit is contained in:
Tianyi Cui
2026-07-12 23:15:07 +08:00
parent ed5304fb6d
commit 738054562d
16 changed files with 261 additions and 50 deletions

View File

@@ -451,9 +451,6 @@ export class ApprovalService extends Service {
resolve('cancelled')
}
signal.addEventListener('abort', onAbort, { once: true })
// Abort can win after the initial check but before listener installation.
// Recheck at the settlement boundary so that edge still cancels.
if (signal.aborted) onAbort()
void answer.then((outcome) => {
signal.removeEventListener('abort', onAbort)
// After an abort won the race this resolve is a settled-promise no-op:

View File

@@ -281,26 +281,6 @@ describe('ApprovalService.request', () => {
expect(appended[1]?.data).toMatchObject({ outcome: 'cancelled' })
})
it('does not miss an abort between the initial check and listener installation', async () => {
const ctx = await mounted()
const { agent, appended } = fakeAgent()
const answer = Promise.withResolvers<ApprovalOutcome>()
ctx.on('approval/request', () => answer.promise)
const controller = new AbortController()
const addEventListener = controller.signal.addEventListener.bind(controller.signal)
const add = vi.spyOn(controller.signal, 'addEventListener').mockImplementation((type, listener, options) => {
controller.abort()
addEventListener(type, listener, options)
})
await expect(ctx.approval.request(requestOf(agent, { signal: controller.signal }))).resolves.toBe('cancelled')
answer.resolve('allowed-once')
await Promise.resolve()
expect(add).toHaveBeenCalledOnce()
expect(appended[1]?.data).toMatchObject({ outcome: 'cancelled' })
})
it('resolves cancelled when the signal aborts mid-question and discards the late answer', async () => {
const ctx = await mounted()
const { agent, appended } = fakeAgent()