diff options
author | andy finch <andyfinch7@gmail.com> | 2019-05-01 16:58:09 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-05-01 16:58:09 -0400 |
commit | 2f4fefd0f6a5c43724ee8d19b008018c28c7c323 (patch) | |
tree | 1566f5e0ed9f5cb5c3ba2067263431c32f5f5df1 /js/net.ts | |
parent | 41c7e96f1a81ea416ebb3ba45f2815e0202d6b75 (diff) |
Async iterator for listener (#2263)
Diffstat (limited to 'js/net.ts')
-rw-r--r-- | js/net.ts | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -14,7 +14,7 @@ export type Network = "tcp"; export type Addr = string; /** A Listener is a generic network listener for stream-oriented protocols. */ -export interface Listener { +export interface Listener extends AsyncIterator<Conn> { /** Waits for and resolves to the next connection to the `Listener`. */ accept(): Promise<Conn>; @@ -25,6 +25,8 @@ export interface Listener { /** Return the address of the `Listener`. */ addr(): Addr; + + [Symbol.asyncIterator](): AsyncIterator<Conn>; } enum ShutdownMode { @@ -97,6 +99,17 @@ class ListenerImpl implements Listener { addr(): Addr { return notImplemented(); } + + async next(): Promise<IteratorResult<Conn>> { + return { + done: false, + value: await this.accept() + }; + } + + [Symbol.asyncIterator](): AsyncIterator<Conn> { + return this; + } } export interface Conn extends Reader, Writer, Closer { |