-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/iter-fest/src/observableSubscribeAsReadable.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Observable, type SubscriberFunction, type SubscriptionObserver } from './Observable'; | ||
import { observableSubscribeAsReadable } from './observableSubscribeAsReadable'; | ||
import type { JestMockOf } from './private/JestMockOf'; | ||
import hasResolved from './private/hasResolved'; | ||
|
||
describe('comprehensive', () => { | ||
let observable: Observable<number>; | ||
let readable: ReadableStream<number>; | ||
let subscriberFunction: JestMockOf<SubscriberFunction<number>>; | ||
let unsubscribeFunction: JestMockOf<() => void>; | ||
|
||
beforeEach(() => { | ||
unsubscribeFunction = jest.fn(); | ||
subscriberFunction = jest.fn().mockImplementation(() => unsubscribeFunction); | ||
observable = new Observable(subscriberFunction); | ||
readable = observableSubscribeAsReadable(observable); | ||
}); | ||
|
||
test('should subscribe', () => expect(subscriberFunction).toHaveBeenCalledTimes(1)); | ||
test('should not call unsubscribe', () => expect(unsubscribeFunction).not.toHaveBeenCalled()); | ||
|
||
describe('when read()', () => { | ||
let observer: SubscriptionObserver<number>; | ||
let reader: ReadableStreamDefaultReader<number>; | ||
let readPromise: Promise<ReadableStreamReadResult<number>>; | ||
|
||
beforeEach(() => { | ||
reader = readable.getReader(); | ||
readPromise = reader.read(); | ||
observer = subscriberFunction.mock.calls[0]![0]; | ||
}); | ||
|
||
test('should not resolve', () => expect(hasResolved(readPromise)).resolves.toBe(false)); | ||
|
||
describe('when complete()', () => { | ||
beforeEach(() => observer.complete()); | ||
|
||
test('should receive done', () => expect(readPromise).resolves.toEqual({ done: true })); | ||
}); | ||
|
||
describe('when error()', () => { | ||
beforeEach(() => observer.error(new Error('artificial'))); | ||
|
||
test('should reject', () => expect(readPromise).rejects.toThrow('artificial')); | ||
}); | ||
|
||
describe('when next(1)', () => { | ||
beforeEach(() => observer.next(1)); | ||
|
||
test('should receive a value', () => expect(readPromise).resolves.toEqual({ done: false, value: 1 })); | ||
}); | ||
}); | ||
|
||
describe('when cancel()', () => { | ||
beforeEach(() => readable.cancel()); | ||
|
||
test('should unsubscribe', () => expect(unsubscribeFunction).toHaveBeenCalledTimes(1)); | ||
}); | ||
}); | ||
|
||
test('all at once as AsyncIterableIterator', async () => { | ||
const observable = Observable.from([1, 2, 3]); | ||
const readable = observableSubscribeAsReadable(observable); | ||
const reader = readable.getReader(); | ||
|
||
await expect(reader.read()).resolves.toEqual({ done: false, value: 1 }); | ||
await expect(reader.read()).resolves.toEqual({ done: false, value: 2 }); | ||
await expect(reader.read()).resolves.toEqual({ done: false, value: 3 }); | ||
await expect(reader.read()).resolves.toEqual({ done: true }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Observable, Subscription } from './Observable'; | ||
|
||
export function observableSubscribeAsReadable<T>(observable: Observable<T>): ReadableStream<T> { | ||
let subscription: Subscription; | ||
|
||
return new ReadableStream<T>({ | ||
cancel() { | ||
subscription.unsubscribe(); | ||
}, | ||
start(controller) { | ||
subscription = observable.subscribe({ | ||
complete() { | ||
controller.close(); | ||
}, | ||
error(err: unknown) { | ||
controller.error(err); | ||
}, | ||
next(value) { | ||
controller.enqueue(value); | ||
} | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters