Skip to content

Commit 7c3e1f3

Browse files
committed
chore: tigthen spec
1 parent 3f0aafd commit 7c3e1f3

File tree

1 file changed

+16
-14
lines changed
  • packages/react-native-fast-io/src/w3c

1 file changed

+16
-14
lines changed

packages/react-native-fast-io/src/w3c/ws.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ export class WebSocket
7676

7777
binaryType: 'arraybuffer' | 'blob' = 'blob'
7878

79-
private _readyState: WebSocketReadyState = WebSocketReadyState.CONNECTING
79+
#readyState: WebSocketReadyState = WebSocketReadyState.CONNECTING
8080
get readyState() {
81-
return this._readyState
81+
return this.#readyState
8282
}
8383

8484
get bufferedAmount() {
@@ -89,9 +89,9 @@ export class WebSocket
8989
throw new Error('Not implemented')
9090
}
9191

92-
private _protocol = ''
92+
#protocol = ''
9393
get protocol() {
94-
return this._protocol
94+
return this.#protocol
9595
}
9696

9797
private readonly ws: HybridWebSocket
@@ -103,8 +103,8 @@ export class WebSocket
103103
this.ws = WebSocketManager.create(url, Array.isArray(protocols) ? protocols : [protocols])
104104

105105
this.ws.onOpen((protocol) => {
106-
this._readyState = WebSocketReadyState.OPEN
107-
this._protocol = protocol
106+
this.#readyState = WebSocketReadyState.OPEN
107+
this.#protocol = protocol
108108
this.dispatchEvent(new Event('open'))
109109
})
110110

@@ -127,14 +127,14 @@ export class WebSocket
127127
* Sending `close` frame before proceeding to close the connection
128128
* https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.7
129129
*/
130-
this._readyState = WebSocketReadyState.CLOSED
130+
this.#readyState = WebSocketReadyState.CLOSED
131131
this.dispatchEvent(new CloseEvent(ABNORMAL_CLOSURE))
132132

133133
this.close()
134134
})
135135

136136
this.ws.onClose((code, reason) => {
137-
this._readyState = WebSocketReadyState.CLOSED
137+
this.#readyState = WebSocketReadyState.CLOSED
138138
this.dispatchEvent(new CloseEvent(code, reason))
139139
})
140140

@@ -146,10 +146,14 @@ export class WebSocket
146146
*/
147147

148148
send(message: string | ArrayBuffer | ArrayBufferView | Blob) {
149-
if (this._readyState === WebSocketReadyState.CONNECTING) {
149+
if (this.#readyState === WebSocketReadyState.CONNECTING) {
150150
throw new Error('InvalidStateError')
151151
}
152152

153+
if (this.#readyState !== WebSocketReadyState.OPEN) {
154+
return
155+
}
156+
153157
if (typeof message === 'string') {
154158
this.ws.send(message)
155159
return
@@ -172,17 +176,15 @@ export class WebSocket
172176
})()
173177
return
174178
}
175-
176-
throw new TypeError('Invalid message type')
177179
}
178180

179181
/**
180182
* https://websockets.spec.whatwg.org/#dom-websocket-close
181183
*/
182184
close(code: number = 1000, reason: string = '') {
183185
if (
184-
this._readyState === WebSocketReadyState.CLOSING ||
185-
this._readyState === WebSocketReadyState.CLOSED
186+
this.#readyState === WebSocketReadyState.CLOSING ||
187+
this.#readyState === WebSocketReadyState.CLOSED
186188
) {
187189
return
188190
}
@@ -191,7 +193,7 @@ export class WebSocket
191193
throw new Error('Invalid close code. Must be 1000 or in range 3000-4999.')
192194
}
193195

194-
this._readyState = WebSocketReadyState.CLOSING
196+
this.#readyState = WebSocketReadyState.CLOSING
195197
this.ws.close(code, reason)
196198
}
197199

0 commit comments

Comments
 (0)