From 3aa345e654973350826019cfc8aab95e55a9cc99 Mon Sep 17 00:00:00 2001 From: crowlkats <13135287+crowlKats@users.noreply.github.com> Date: Sun, 13 Sep 2020 19:34:44 +0200 Subject: [PATCH 1/2] Add asyncwebsocket_wrapper --- std/ws/client.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 std/ws/client.ts diff --git a/std/ws/client.ts b/std/ws/client.ts new file mode 100644 index 00000000000000..e9e77ea200d5f6 --- /dev/null +++ b/std/ws/client.ts @@ -0,0 +1,47 @@ +export type Payload = + | { type: "open", data: Event } + | { type: "message", data: MessageEvent } + | { type: "close", data: CloseEvent } + | { type: "error", data: Event | ErrorEvent }; + +export class AsyncWebSocket extends WebSocket { + #readable: ReadableStream; + + constructor(url: string, protocols?: string | string[]) { + super(url, protocols); + + const {readable, writable} = new TransformStream(); + this.#readable = readable; + const writer = writable.getWriter(); + + super.onopen = (e) => { + writer.write({ + type: "open", + data: e, + }); + }; + super.onmessage = (e) => { + writer.write({ + type: "message", + data: e, + }); + }; + super.onclose = async (e) => { + await writer.write({ + type: "close", + data: e, + }); + await writer.close(); + }; + super.onerror = (e) => { + writer.write({ + type: "error", + data: e, + }); + }; + } + + async* [Symbol.asyncIterator]() { + yield* this.#readable.getIterator(); + } +} From dd1ae32558c2fe779b4bf80563fceb74901349b4 Mon Sep 17 00:00:00 2001 From: crowlkats <13135287+crowlKats@users.noreply.github.com> Date: Sun, 13 Sep 2020 22:14:09 +0200 Subject: [PATCH 2/2] safer event listening --- std/ws/client.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/std/ws/client.ts b/std/ws/client.ts index e9e77ea200d5f6..71047e41959a3e 100644 --- a/std/ws/client.ts +++ b/std/ws/client.ts @@ -1,8 +1,8 @@ export type Payload = - | { type: "open", data: Event } - | { type: "message", data: MessageEvent } - | { type: "close", data: CloseEvent } - | { type: "error", data: Event | ErrorEvent }; + | { type: "open"; data: Event } + | { type: "message"; data: MessageEvent } + | { type: "close"; data: CloseEvent } + | { type: "error"; data: Event | ErrorEvent }; export class AsyncWebSocket extends WebSocket { #readable: ReadableStream; @@ -10,38 +10,38 @@ export class AsyncWebSocket extends WebSocket { constructor(url: string, protocols?: string | string[]) { super(url, protocols); - const {readable, writable} = new TransformStream(); + const { readable, writable } = new TransformStream(); this.#readable = readable; const writer = writable.getWriter(); - super.onopen = (e) => { + this.addEventListener("open", (e) => { writer.write({ type: "open", data: e, }); - }; - super.onmessage = (e) => { + }); + this.addEventListener("message", (e) => { writer.write({ type: "message", data: e, }); - }; - super.onclose = async (e) => { + }); + this.addEventListener("close", async (e) => { await writer.write({ type: "close", data: e, }); await writer.close(); - }; - super.onerror = (e) => { + }); + this.addEventListener("error", (e) => { writer.write({ type: "error", data: e, }); - }; + }); } - async* [Symbol.asyncIterator]() { + async *[Symbol.asyncIterator]() { yield* this.#readable.getIterator(); } }