Skip to content

Commit 2147e3f

Browse files
authored
fix: EventPublisher should throw immediately if signal is aborted (#629)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved handling of aborted operations to ensure immediate feedback if an operation is started with an already-aborted signal. - **Tests** - Updated tests to verify that operations throw immediately when initiated with an aborted signal. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 343cede commit 2147e3f

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

packages/shared/src/event-publisher.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,11 @@ describe('eventPublisher', () => {
239239
expect(pub.size).toEqual(2) // iterator1 was unsubscribed
240240
})
241241

242-
it('throw right away if signal aborted', async () => {
242+
it('throw immediately if signal aborted', () => {
243243
controller3.abort()
244244
const payloads: any[] = []
245245

246-
await expect(async () => {
247-
for await (const payload of iterator3) {
248-
payloads.push(payload)
249-
}
250-
},
251-
).rejects.toThrow(controller3.signal.reason)
246+
expect(() => pub.subscribe('event3', { signal: controller3.signal })).toThrow(controller3.signal.reason)
252247
})
253248

254249
it('throw if signal aborted while awaiting next', async () => {

packages/shared/src/event-publisher.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ export class EventPublisher<T extends Record<PropertyKey, any>> {
103103
const signal = listenerOrOptions?.signal
104104
const maxBufferedEvents = listenerOrOptions?.maxBufferedEvents ?? this.#maxBufferedEvents
105105

106+
signal?.throwIfAborted()
107+
106108
const bufferedEvents: T[K][] = []
107109
const pullResolvers: [(result: IteratorResult<T[K]>) => void, (error: Error) => void][] = []
108110

0 commit comments

Comments
 (0)