Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: asyncIterator ponyfill API #30

Merged
merged 2 commits into from
Jul 21, 2024
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
17 changes: 17 additions & 0 deletions .changeset/gorgeous-geckos-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@sec-ant/readable-stream": minor
---

Refactor `asyncIterator` ponyfill API

**BREAKING**: The `asyncIterator` from the ponyfill API is refactored, you should now use it like this:

```ts
asyncIterator(readableStream);
```

instead of

```ts
asyncIterator.call(readableStream);
```
2 changes: 1 addition & 1 deletion .github/actions/setup-and-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ runs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
node-version-file: .node-version

- name: Install Dependencies
shell: bash
Expand Down
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/*
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
const readableStream = (await fetch("https://www.example.org/")).body;

let total = 0;
for await (const chunk of asyncIterator.call(readableStream)) {
for await (const chunk of asyncIterator(readableStream)) {
total += chunk.length;
}

Expand Down
5,212 changes: 1,398 additions & 3,814 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@
"bump-biome:nightly": "npm i -DE @biomejs/biome@nightly"
},
"devDependencies": {
"@biomejs/biome": "1.8.0",
"@changesets/cli": "^2.27.5",
"@biomejs/biome": "1.8.3",
"@changesets/cli": "^2.27.7",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@vitest/browser": "^1.6.0",
"@vitest/coverage-istanbul": "^1.6.0",
"@vitest/ui": "^1.6.0",
"@vitest/browser": "^2.0.3",
"@vitest/coverage-istanbul": "^2.0.3",
"@vitest/ui": "^2.0.3",
"concurrently": "^8.2.2",
"copy-files-from-to": "^3.9.1",
"lint-staged": "^15.2.5",
"playwright": "^1.44.1",
"prettier": "^3.3.1",
"copy-files-from-to": "^3.11.0",
"lint-staged": "^15.2.7",
"playwright": "^1.45.2",
"prettier": "^3.3.3",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.4.5",
"vite": "^5.2.12",
"vitest": "^1.6.0"
"typescript": "^5.5.3",
"vite": "^5.3.4",
"vitest": "^2.0.3"
}
}
6 changes: 3 additions & 3 deletions src/core/asyncIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ export interface ReadableStreamIteratorOptions {

/**
* Get an async iterable iterator from a readable stream
* @param this
* @param readableStream
* @param readableStreamIteratorOptions
* @returns
*/
export function asyncIterator<R, TReturn>(
this: ReadableStream<R>,
readableStream: ReadableStream<R>,
{ preventCancel = false }: ReadableStreamIteratorOptions = {},
) {
const reader = this.getReader();
const reader = readableStream.getReader();
const implement = new ReadableStreamAsyncIterableIteratorImpl<R, TReturn>(
reader,
preventCancel,
Expand Down
8 changes: 7 additions & 1 deletion src/polyfill/asyncIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { asyncIterator } from "../core/asyncIterator.js";

ReadableStream.prototype.values ??= ReadableStream.prototype[
Symbol.asyncIterator
] ??= asyncIterator;
] ??= function (
...args: Parameters<typeof asyncIterator> extends [infer _, ...infer T]
? T
: never
) {
return asyncIterator(this, ...args);
};

ReadableStream.prototype[Symbol.asyncIterator] ??=
ReadableStream.prototype.values;