From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../src/common/internal/websocket_logger.ts | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 dom/webgpu/tests/cts/checkout/src/common/internal/websocket_logger.ts (limited to 'dom/webgpu/tests/cts/checkout/src/common/internal/websocket_logger.ts') diff --git a/dom/webgpu/tests/cts/checkout/src/common/internal/websocket_logger.ts b/dom/webgpu/tests/cts/checkout/src/common/internal/websocket_logger.ts new file mode 100644 index 0000000000..30246df843 --- /dev/null +++ b/dom/webgpu/tests/cts/checkout/src/common/internal/websocket_logger.ts @@ -0,0 +1,52 @@ +/** + * - 'uninitialized' means we haven't tried to connect yet + * - Promise means it's pending + * - 'failed' means it failed (this is the most common case, where the logger isn't running) + * - WebSocket means it succeeded + */ +let connection: Promise | WebSocket | 'failed' | 'uninitialized' = + 'uninitialized'; + +/** + * Log a string to a websocket at `localhost:59497`. See `tools/websocket-logger`. + * + * This does nothing if a connection couldn't be established on the first call. + */ +export function logToWebsocket(msg: string) { + if (connection === 'failed') { + return; + } + + if (connection === 'uninitialized') { + connection = new Promise(resolve => { + if (typeof WebSocket === 'undefined') { + resolve('failed'); + return; + } + + const ws = new WebSocket('ws://localhost:59497/optional_cts_websocket_logger'); + ws.onopen = () => { + resolve(ws); + }; + ws.onerror = () => { + connection = 'failed'; + resolve('failed'); + }; + ws.onclose = () => { + connection = 'failed'; + resolve('failed'); + }; + }); + void connection.then(resolved => { + connection = resolved; + }); + } + + void (async () => { + // connection may be a promise or a value here. Either is OK to await. + const ws = await connection; + if (ws !== 'failed') { + ws.send(msg); + } + })(); +} -- cgit v1.2.3