Skip to content

Commit 7ac13d4

Browse files
author
Taras Mankovski
committed
refactor(k6): make WebSocket extend Stream<WebSocketMessage, void>
- WebSocket interface now extends Stream, removing separate messages property - Use each(ws) instead of each(ws.messages) to iterate messages - Delegate Symbol.iterator to internal messageSignal - Update collectMessages and waitForMessage helpers - Update docs and demo
1 parent b55a0a8 commit 7ac13d4

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

k6/demos/02-websocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default main(function* () {
8181
ws.send("Message 3");
8282

8383
let count = 0;
84-
for (const message of yield* each(ws.messages)) {
84+
for (const message of yield* each(ws)) {
8585
count++;
8686
console.log(`Stream message ${count}: ${message}`);
8787

k6/websockets/mod.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
*
1717
* @example
1818
* ```typescript
19-
* import { main, useWebSocket } from '@effectionx/k6';
19+
* import { main, useWebSocket, each } from '@effectionx/k6';
2020
*
2121
* export default main(function*() {
2222
* const ws = yield* useWebSocket('wss://echo.websocket.org');
2323
*
2424
* ws.send('Hello');
2525
*
26-
* for (const message of yield* each(ws.messages)) {
26+
* for (const message of yield* each(ws)) {
2727
* console.log('Received:', message);
2828
* yield* each.next();
2929
* }
@@ -116,17 +116,18 @@ export interface WebSocketErrorEvent {
116116
/**
117117
* An Effection-managed WebSocket.
118118
*
119-
* The resource provides:
120-
* - `send()` - Send messages to the server
121-
* - `close()` - Close the connection (returns Operation that resolves when closed)
122-
* - `messages` - Stream of incoming messages
119+
* The WebSocket is itself a Stream of messages. Use `each(ws)` to iterate:
120+
*
121+
* ```typescript
122+
* for (const msg of yield* each(ws)) {
123+
* console.log('Received:', msg);
124+
* yield* each.next();
125+
* }
126+
* ```
123127
*
124128
* The WebSocket is automatically closed when the scope ends.
125129
*/
126-
export interface WebSocket {
127-
/** Stream of incoming messages */
128-
readonly messages: Stream<WebSocketMessage, void>;
129-
130+
export interface WebSocket extends Stream<WebSocketMessage, void> {
130131
/** Send a message to the server */
131132
send(data: string | ArrayBuffer): void;
132133

@@ -150,7 +151,7 @@ export interface WebSocket {
150151
* const ws = yield* useWebSocket('wss://api.example.com/ws');
151152
* ws.send(JSON.stringify({ type: 'subscribe', channel: 'updates' }));
152153
*
153-
* for (const msg of yield* each(ws.messages)) {
154+
* for (const msg of yield* each(ws)) {
154155
* const data = JSON.parse(msg as string);
155156
* console.log('Update:', data);
156157
* yield* each.next();
@@ -216,7 +217,8 @@ export function useWebSocket(
216217

217218
// Create the resource object
218219
const ws: WebSocket = {
219-
messages: messageSignal,
220+
// Delegate Stream iteration to messageSignal
221+
[Symbol.iterator]: messageSignal[Symbol.iterator].bind(messageSignal),
220222

221223
send(data: string | ArrayBuffer) {
222224
socket.send(data);
@@ -263,7 +265,7 @@ export function useWebSocket(
263265
* ```typescript
264266
* const result = yield* withWebSocket('wss://api.example.com', function*(ws) {
265267
* ws.send('ping');
266-
* const subscription = yield* ws.messages;
268+
* const subscription = yield* ws;
267269
* const msg = yield* subscription.next();
268270
* return msg.value;
269271
* });
@@ -297,7 +299,7 @@ export function* collectMessages(
297299
count: number,
298300
): Operation<WebSocketMessage[]> {
299301
const messages: WebSocketMessage[] = [];
300-
const subscription = yield* ws.messages;
302+
const subscription = yield* ws;
301303

302304
while (messages.length < count) {
303305
const result = yield* subscription.next();
@@ -332,7 +334,7 @@ export function* waitForMessage(
332334
ws: WebSocket,
333335
predicate: (msg: WebSocketMessage) => boolean,
334336
): Operation<WebSocketMessage> {
335-
const subscription = yield* ws.messages;
337+
const subscription = yield* ws;
336338

337339
while (true) {
338340
const result = yield* subscription.next();

0 commit comments

Comments
 (0)