summaryrefslogtreecommitdiff
path: root/cli/js/net.ts
diff options
context:
space:
mode:
authorNayeem Rahman <muhammed.9939@gmail.com>2020-03-10 19:14:22 +0000
committerGitHub <noreply@github.com>2020-03-10 15:14:22 -0400
commit55119aaee2e5fec8074373ef51b56d5095da1faf (patch)
treec1ad9bc7526ffa0941df4c552e0ddf113beac9df /cli/js/net.ts
parent8078d976d29aa12819e3f5a781c846d67868b0d6 (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/net.ts')
-rw-r--r--cli/js/net.ts72
1 files changed, 25 insertions, 47 deletions
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;
+ }
+ }
}
}