BREAKING CHANGE: rename Deno.toAsyncIterator() to Deno.iter() (#4848)

* rename Deno.toAsyncIterator() to Deno.iter()
* adds sync version Deno.iterSync()
* adds optional second argument for buffer size
This commit is contained in:
Bartek Iwańczuk 2020-04-22 21:30:45 +02:00 committed by GitHub
parent da6819a14c
commit 68d287eed5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 169 additions and 28 deletions

View file

@ -573,13 +573,61 @@ declare namespace Deno {
*/
export function copy(dst: Writer, src: Reader): Promise<number>;
/** Turns a Reader, `r`, into an async iterator.
/** **UNSTABLE**: new API, yet to be vetted
* Turns a Reader, `r`, into an async iterator.
*
* for await (const chunk of toAsyncIterator(reader)) {
* let f = await open("/etc/passwd");
* for await (const chunk of iter(f)) {
* console.log(chunk);
* }
* f.close();
*
* Second argument can be used to tune size of a buffer.
* Default size of the buffer is 1024 bytes.
*
* let f = await open("/etc/passwd");
* for await (const chunk of iter(f, 1024 * 1024)) {
* console.log(chunk);
* }
* f.close();
*
* Iterator uses internal buffer of fixed size for efficiency returning
* a view on that buffer on each iteration. It it therefore callers
* responsibility to copy contents of the buffer if needed; otherwise
* next iteration will overwrite contents of previously returned chunk.
*/
export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array>;
export function iter(
r: Reader,
bufSize?: number
): AsyncIterableIterator<Uint8Array>;
/** **UNSTABLE**: new API, yet to be vetted
* Turns a SyncReader, `r`, into an iterator.
*
* let f = await open("/etc/passwd");
* for (const chunk of iterSync(reader)) {
* console.log(chunk);
* }
* f.close();
*
* Second argument can be used to tune size of a buffer.
* Default size of the buffer is 1024 bytes.
*
* let f = await open("/etc/passwd");
* for (const chunk of iterSync(reader, 1024 * 1024)) {
* console.log(chunk);
* }
* f.close()
*
* Iterator uses internal buffer of fixed size for efficiency returning
* a view on that buffer on each iteration. It it therefore callers
* responsibility to copy contents of the buffer if needed; otherwise
* next iteration will overwrite contents of previously returned chunk.
*/
export function iterSync(
r: SyncReader,
bufSize?: number
): IterableIterator<Uint8Array>;
/** Synchronously open a file and return an instance of `Deno.File`. The
* file does not need to previously exist if using the `create` or `createNew`