Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/peer/src/hibernation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('hibernationAsyncIteratorClass', () => {
await expect(iterator.next()).rejects.toThrow('Cannot use hibernating iterator directly')
})

it('return() throws', async () => {
it('return() does not throw', async () => {
const iterator = new HibernationAsyncIteratorClass(vi.fn())
await expect(iterator.return()).rejects.toThrow('Cannot use hibernating iterator directly')
await expect(iterator.return()).resolves.toEqual({ done: true, value: undefined })
})

it('invokes callback with correct id', () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/peer/src/hibernation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ export class HibernationAsyncIteratorClass<T, TReturn = unknown, TNext = unknown
) {
super(async () => {
throw new Error('Cannot use hibernating iterator directly')
}, async ({ kind }) => {
if (kind === 'cancelled') {
throw new Error('Cannot use hibernating iterator directly')
}
}, async () => {
// nothing to clean up
})

this['~callback'] = callback
Expand Down
2 changes: 2 additions & 0 deletions packages/peer/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ describe('serverPeer', () => {
expect(callback).toHaveBeenCalledWith('1')
expect(send).toHaveBeenCalledTimes(1)
expect(send).toHaveBeenNthCalledWith(1, expect.objectContaining({ kind: 'response' }))
await expect(hibernationIter.next()).resolves.toEqual({ done: true, value: undefined }) // already cleaned up
})

it('reject if HibernationAsyncIteratorClassCallback reject', async () => {
Expand All @@ -340,6 +341,7 @@ describe('serverPeer', () => {
expect(send).toHaveBeenCalledTimes(2)
expect(send).toHaveBeenNthCalledWith(1, expect.objectContaining({ kind: 'response' }))
expect(send).toHaveBeenNthCalledWith(2, expect.objectContaining({ kind: 'cancel' }))
await expect(hibernationIter.next()).resolves.toEqual({ done: true, value: undefined }) // already cleaned up
})

it('cancels active transmitter on close', async () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/peer/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ export class ServerPeer {

if (isAsyncIteratorObject(response.body)) {
if (response.body instanceof HibernationAsyncIteratorClass) {
await response.body['~callback']?.(id)
try {
await response.body['~callback']?.(id)
}
finally {
await response.body.return()
}
}
else {
const transmitter = new EventStreamTransmitter(response.body, id, this.send)
Expand Down
Loading