-
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
14 changed files
with
275 additions
and
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { PushAsyncIterableIterator } from './PushAsyncIterableIterator'; | ||
import type { JestMockOf } from './private/JestMockOf'; | ||
|
||
describe('comprehensive', () => { | ||
let done: JestMockOf<() => void>; | ||
let iterable: PushAsyncIterableIterator<number>; | ||
let values: number[]; | ||
|
||
beforeEach(() => { | ||
done = jest.fn(); | ||
iterable = new PushAsyncIterableIterator(); | ||
values = []; | ||
|
||
(async () => { | ||
for await (const value of iterable) { | ||
values.push(value); | ||
} | ||
|
||
done(); | ||
})(); | ||
}); | ||
|
||
test('should receive no values', () => expect(values).toEqual([])); | ||
test('should not completed the for-loop', () => expect(done).not.toHaveBeenCalled()); | ||
|
||
describe('when push(1) is called', () => { | ||
beforeEach(() => iterable.push(1)); | ||
|
||
test('should receive a value', () => expect(values).toEqual([1])); | ||
|
||
describe('when push(2) is called', () => { | ||
beforeEach(() => iterable.push(2)); | ||
|
||
test('should receive a value', () => expect(values).toEqual([1, 2])); | ||
}); | ||
}); | ||
|
||
describe('when close() is called', () => { | ||
beforeEach(() => iterable.close()); | ||
|
||
test('should completed the for-loop', () => expect(done).toHaveBeenCalledTimes(1)); | ||
}); | ||
}); |
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,30 @@ | ||
import withResolvers from './private/withResolvers'; | ||
|
||
const CLOSE = Symbol('close'); | ||
|
||
export class PushAsyncIterableIterator<T> implements AsyncIterableIterator<T> { | ||
#pushResolvers: PromiseWithResolvers<T | typeof CLOSE> = withResolvers(); | ||
|
||
[Symbol.asyncIterator]() { | ||
return this; | ||
} | ||
|
||
close() { | ||
this.#pushResolvers.resolve(CLOSE); | ||
} | ||
|
||
async next(): Promise<IteratorResult<T>> { | ||
const value = await this.#pushResolvers.promise; | ||
|
||
if (value === CLOSE) { | ||
return { done: true, value: undefined }; | ||
} | ||
|
||
return { done: false, value }; | ||
} | ||
|
||
push(value: T) { | ||
this.#pushResolvers.resolve(value); | ||
this.#pushResolvers = withResolvers(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function hasResolved(promise: Promise<unknown>): Promise<boolean> { | ||
return Promise.race([promise.then(() => true), Promise.resolve(false)]); | ||
} |
Oops, something went wrong.