summaryrefslogtreecommitdiff
path: root/cli/js/net.ts
diff options
context:
space:
mode:
Diffstat (limited to 'cli/js/net.ts')
-rw-r--r--cli/js/net.ts22
1 files changed, 17 insertions, 5 deletions
diff --git a/cli/js/net.ts b/cli/js/net.ts
index f463da60b..8109934a4 100644
--- a/cli/js/net.ts
+++ b/cli/js/net.ts
@@ -82,7 +82,8 @@ export class ListenerImpl implements Listener {
constructor(
readonly rid: number,
private transport: Transport,
- private localAddr: string
+ private localAddr: string,
+ private closing: boolean = false
) {}
async accept(): Promise<Conn> {
@@ -91,6 +92,7 @@ export class ListenerImpl implements Listener {
}
close(): void {
+ this.closing = true;
close(this.rid);
}
@@ -102,10 +104,20 @@ export class ListenerImpl implements Listener {
}
async next(): Promise<IteratorResult<Conn>> {
- return {
- done: false,
- value: await this.accept()
- };
+ 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 };
+ }
+ throw e;
+ });
}
[Symbol.asyncIterator](): AsyncIterator<Conn> {