Skip to content

Commit

Permalink
chore(WebSocketClientManager): annotate channel messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Feb 25, 2024
1 parent 654c91d commit ec0154f
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/core/ws/SerializedWebSocketClientConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ import type {
WebSocketClientConnectionProtocol,

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / exports

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / typescript (4.7)

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?

Check failure on line 4 in src/core/ws/SerializedWebSocketClientConnection.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

'"@mswjs/interceptors/lib/browser/interceptors/WebSocket"' has no exported member named 'WebSocketClientConnectionProtocol'. Did you mean 'WebSocketClientConnection'?
} from '@mswjs/interceptors/WebSocket'

type WebSocketBroadcastChannelMessage =
| {
type: 'connection:open'
payload: {
id: string
url: URL
}
}
| {
type: 'send'
payload: {
clientId: string
data: WebSocketData
}
}
| {
type: 'close'
payload: {
clientId: string
code?: number
reason?: string
}
}

export const kAddByClientId = Symbol('kAddByClientId')

/**
Expand All @@ -20,7 +44,7 @@ export class WebSocketClientManager {
this.clients = new Set()

this.channel.addEventListener('message', (message) => {
const { type, payload } = message.data
const { type, payload } = message.data as WebSocketBroadcastChannelMessage

switch (type) {
case 'connection:open': {
Expand Down Expand Up @@ -50,15 +74,19 @@ export class WebSocketClientManager {
id: client.id,
url: client.url,
},
})
} as WebSocketBroadcastChannelMessage)

// Instruct the current client how to handle events
// coming from other runtimes (e.g. when broadcasting).
this.channel.addEventListener('message', (message) => {
const { type, payload } = message.data
const { type, payload } = message.data as WebSocketBroadcastChannelMessage

// Ignore broadcasted messages for other clients.
if (payload.clientId !== client.id) {
if (
typeof payload === 'object' &&
'clientId' in payload &&
payload.clientId !== client.id
) {
return
}

Expand Down Expand Up @@ -113,7 +141,7 @@ class WebSocketRemoteClientConnection
clientId: this.id,
data,
},
})
} as WebSocketBroadcastChannelMessage)
}

close(code?: number | undefined, reason?: string | undefined): void {
Expand All @@ -124,6 +152,6 @@ class WebSocketRemoteClientConnection
code,
reason,
},
})
} as WebSocketBroadcastChannelMessage)
}
}

0 comments on commit ec0154f

Please sign in to comment.