diff options
author | Nayeem Rahman <muhammed.9939@gmail.com> | 2020-03-10 19:14:22 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-10 15:14:22 -0400 |
commit | 55119aaee2e5fec8074373ef51b56d5095da1faf (patch) | |
tree | c1ad9bc7526ffa0941df4c552e0ddf113beac9df /cli/js | |
parent | 8078d976d29aa12819e3f5a781c846d67868b0d6 (diff) |
refactor(cli/js/net): Cleanup iterable APIs (#4236)
Listener and UDPConn are AsyncIterables instead of AsyncIterators.
The [Symbol.asyncIterator]()s are defined as generators and the
next() methods are gone.
"Listener/Socket has been closed" errors are now BadResource.
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/lib.deno.ns.d.ts | 4 | ||||
-rw-r--r-- | cli/js/net.ts | 72 |
2 files changed, 27 insertions, 49 deletions
diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 6950d9050..43e28b05b 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1522,7 +1522,7 @@ declare namespace Deno { /** **UNSTABLE**: new API, yet to be vetted. * * A generic transport listener for message-oriented protocols. */ - export interface UDPConn extends AsyncIterator<[Uint8Array, Addr]> { + export interface UDPConn extends AsyncIterable<[Uint8Array, Addr]> { /** **UNSTABLE**: new API, yet to be vetted. * * Waits for and resolves to the next message to the `UDPConn`. */ @@ -1542,7 +1542,7 @@ declare namespace Deno { } /** A generic network listener for stream-oriented protocols. */ - export interface Listener extends AsyncIterator<Conn> { + export interface Listener extends AsyncIterable<Conn> { /** Waits for and resolves to the next connection to the `Listener`. */ accept(): Promise<Conn>; /** Close closes the listener. Any pending accept promises will be rejected diff --git a/cli/js/net.ts b/cli/js/net.ts index 3771a1323..52c558339 100644 --- a/cli/js/net.ts +++ b/cli/js/net.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { errors } from "./errors.ts"; import { EOF, Reader, Writer, Closer } from "./io.ts"; import { read, write } from "./ops/io.ts"; import { close } from "./ops/resources.ts"; @@ -19,7 +20,7 @@ export interface UDPAddr { } /** A socket is a generic transport listener for message-oriented protocols */ -export interface UDPConn extends AsyncIterator<[Uint8Array, Addr]> { +export interface UDPConn extends AsyncIterable<[Uint8Array, Addr]> { /** Waits for and resolves to the next message to the `Socket`. */ receive(p?: Uint8Array): Promise<[Uint8Array, Addr]>; @@ -38,7 +39,7 @@ export interface UDPConn extends AsyncIterator<[Uint8Array, Addr]> { } /** A Listener is a generic transport listener for stream-oriented protocols. */ -export interface Listener extends AsyncIterator<Conn> { +export interface Listener extends AsyncIterable<Conn> { /** Waits for and resolves to the next connection to the `Listener`. */ accept(): Promise<Conn>; @@ -88,11 +89,7 @@ export class ConnImpl implements Conn { } export class ListenerImpl implements Listener { - constructor( - readonly rid: number, - readonly addr: Addr, - private closing: boolean = false - ) {} + constructor(readonly rid: number, readonly addr: Addr) {} async accept(): Promise<Conn> { const res = await netOps.accept(this.rid); @@ -100,29 +97,20 @@ export class ListenerImpl implements Listener { } close(): void { - this.closing = true; close(this.rid); } - async next(): Promise<IteratorResult<Conn>> { - if (this.closing) { - return { value: undefined, done: true }; - } - return await this.accept() - .then(value => ({ value, done: false })) - .catch(e => { - // It wouldn't be correct to simply check this.closing here. - // TODO: Get a proper error kind for this case, don't check the message. - // The current error kind is Other. - if (e.message == "Listener has been closed") { - return { value: undefined, done: true }; + async *[Symbol.asyncIterator](): AsyncIterator<Conn> { + while (true) { + try { + yield await this.accept(); + } catch (error) { + if (error instanceof errors.BadResource) { + break; } - throw e; - }); - } - - [Symbol.asyncIterator](): AsyncIterator<Conn> { - return this; + throw error; + } + } } } @@ -138,8 +126,7 @@ export class UDPConnImpl implements UDPConn { constructor( readonly rid: number, readonly addr: Addr, - public bufSize: number = 1024, - private closing: boolean = false + public bufSize: number = 1024 ) {} async receive(p?: Uint8Array): Promise<[Uint8Array, Addr]> { @@ -157,29 +144,20 @@ export class UDPConnImpl implements UDPConn { } close(): void { - this.closing = true; close(this.rid); } - async next(): Promise<IteratorResult<[Uint8Array, Addr]>> { - if (this.closing) { - return { value: undefined, done: true }; - } - return await this.receive() - .then(value => ({ value, done: false })) - .catch(e => { - // It wouldn't be correct to simply check this.closing here. - // TODO: Get a proper error kind for this case, don't check the message. - // The current error kind is Other. - if (e.message == "Socket has been closed") { - return { value: undefined, done: true }; + async *[Symbol.asyncIterator](): AsyncIterator<[Uint8Array, Addr]> { + while (true) { + try { + yield await this.receive(); + } catch (error) { + if (error instanceof errors.BadResource) { + break; } - throw e; - }); - } - - [Symbol.asyncIterator](): AsyncIterator<[Uint8Array, Addr]> { - return this; + throw error; + } + } } } |