Skip to content

Commit ad06ba9

Browse files
fix(client): type client.client on client/socket, and guard the example
`client/socket`'s `client` is now typed with its `client`, the `WebSocket` or `EventSource` underneath, which the documented usage reads. It is a type of that export's own, so the contract a custom transport implements is unchanged. The README's example reads it only when there is one, and both it and the module's comment now say when that is: from when the runtime starts to connect, with a new one for each connection. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
1 parent ca5e62c commit ad06ba9

4 files changed

Lines changed: 36 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,11 +1015,15 @@ connection it holds:
10151015
```js
10161016
import { client } from "webpack-dev-middleware/client/socket";
10171017

1018-
// `client` is live: `null` until the runtime connects and while it reconnects.
1019-
// `client.client` is the `WebSocket` or `EventSource` underneath.
1020-
client.client.addEventListener("message", (event) => {
1021-
console.log(JSON.parse(event.data));
1022-
});
1018+
// `client` is live: `null` before the runtime starts to connect and while it
1019+
// waits to reconnect. `client.client` is the `WebSocket` or `EventSource`
1020+
// underneath, a new one for each connection, so a listener added to it hears
1021+
// that connection only.
1022+
if (client && client.client) {
1023+
client.client.addEventListener("message", (event) => {
1024+
console.log(JSON.parse(event.data));
1025+
});
1026+
}
10231027
```
10241028

10251029
This is the shape webpack-dev-server's `client/socket` has always exported,

‎client-src/clients/createSocket.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ import { log } from "../utils/log.js";
3333
* @property {EXPECTED_ANY=} clientOptions passed to the client's constructor
3434
*/
3535

36+
/**
37+
* A connection as `client` hands it out. Its own `client` is the `WebSocket`
38+
* or `EventSource` underneath, on the built-in transports.
39+
* @typedef {CommunicationClient & { client?: WebSocket | EventSource }} LiveClient
40+
*/
41+
3642
/**
3743
* The connection the runtime holds right now, or `null` while there is none —
3844
* for tooling that listens alongside the runtime rather than through it.
3945
* `client.client` is the `WebSocket` or `EventSource` underneath, the shape
4046
* webpack-dev-server's `client/socket` has always exported, which is what
4147
* `@pmmmwh/react-refresh-webpack-plugin` reads its build messages from.
42-
* @type {CommunicationClient | null}
48+
* @type {LiveClient | null}
4349
*/
4450
// eslint-disable-next-line import/no-mutable-exports
4551
export let client = null;

‎client-src/socket.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// The connection the runtime holds, for tooling that listens alongside it.
22
//
3-
// `client` is live: `null` until the runtime connects and while it is
4-
// reconnecting, and the current transport instance otherwise, whose own
5-
// `client` is the `WebSocket` or `EventSource` underneath. It is the same
6-
// shape webpack-dev-server's `client/socket` has always exported, which is
7-
// how `@pmmmwh/react-refresh-webpack-plugin` reads its build messages.
3+
// `client` is live: `null` before the runtime starts to connect and while it
4+
// waits to reconnect, and the current transport instance otherwise, whose own
5+
// `client` is the `WebSocket` or `EventSource` underneath — a new one for each
6+
// connection. It is the same shape webpack-dev-server's `client/socket` has
7+
// always exported, which is how `@pmmmwh/react-refresh-webpack-plugin` reads
8+
// its build messages.
89
export { client } from "./clients/createSocket.js";

‎types/client/clients/createSocket.d.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,20 @@ export default function createSocket(
4343
* @property {(() => void)=} onDisconnect called once per outage — on the first drop, whether or not that connection ever opened
4444
* @property {EXPECTED_ANY=} clientOptions passed to the client's constructor
4545
*/
46+
/**
47+
* A connection as `client` hands it out. Its own `client` is the `WebSocket`
48+
* or `EventSource` underneath, on the built-in transports.
49+
* @typedef {CommunicationClient & { client?: WebSocket | EventSource }} LiveClient
50+
*/
4651
/**
4752
* The connection the runtime holds right now, or `null` while there is none —
4853
* for tooling that listens alongside the runtime rather than through it.
4954
* `client.client` is the `WebSocket` or `EventSource` underneath, the shape
5055
* webpack-dev-server's `client/socket` has always exported, which is what
5156
* `@pmmmwh/react-refresh-webpack-plugin` reads its build messages from.
52-
* @type {CommunicationClient | null}
57+
* @type {LiveClient | null}
5358
*/
54-
export let client: CommunicationClient | null;
59+
export let client: LiveClient | null;
5560
/**
5661
* Called with no argument for open and close, and with the message string for
5762
* a message.
@@ -107,3 +112,10 @@ export type SocketOptions = {
107112
*/
108113
clientOptions?: EXPECTED_ANY | undefined;
109114
};
115+
/**
116+
* A connection as `client` hands it out. Its own `client` is the `WebSocket`
117+
* or `EventSource` underneath, on the built-in transports.
118+
*/
119+
export type LiveClient = CommunicationClient & {
120+
client?: WebSocket | EventSource;
121+
};

0 commit comments

Comments
 (0)