@@ -34,17 +34,15 @@ export const useWebSocket = <T = string | ArrayBuffer | Blob> ({ url, protocols,
3434 status : immediateConnection && url ? "CONNECTING" : "READY" ,
3535 data : null
3636 } ) ;
37- const wsInitialized = ! ! wsRef . current ;
38- const currentStatus = cachedState . current . status ;
3937
4038 const sendBuffer = useCallback ( ( ) => {
41- if ( bufferingData && wsInitialized && currentStatus === "OPENED" ) {
39+ if ( bufferingData && wsRef . current && cachedState . current . status === "OPENED" ) {
4240 dataBuffer . current . forEach ( data => {
4341 wsRef . current ! . send ( data ) ;
4442 } ) ;
4543 dataBuffer . current = [ ] ;
4644 }
47- } , [ bufferingData , currentStatus , wsInitialized ] )
45+ } , [ bufferingData ] )
4846
4947 if ( url && immediateConnection && ! alreadyOpened . current ) {
5048 wsRef . current = new WebSocket ( url , protocols ) ;
@@ -102,31 +100,31 @@ export const useWebSocket = <T = string | ArrayBuffer | Blob> ({ url, protocols,
102100 }
103101
104102 const open = useCallback ( ( urlParam ?: UseWebSocketProps [ "url" ] ) => {
105- if ( ! wsInitialized && ( url || urlParam ) ) {
103+ if ( ! wsRef . current && ( url || urlParam ) ) {
106104 const urlWs = url || urlParam as string | URL ;
107105 wsRef . current = new WebSocket ( urlWs , protocols ) ;
108106 binaryType && ( wsRef . current . binaryType = binaryType ) ;
109107 cachedState . current . status = "CONNECTING" ;
110108 notifyRef . current && notifyRef . current ( ) ;
111109 }
112- } , [ protocols , url , wsInitialized , binaryType ] ) ;
110+ } , [ protocols , url , binaryType ] ) ;
113111
114112 const send = useCallback ( ( data : string | ArrayBuffer | Blob | TypedArray ) => {
115- if ( ( ! wsInitialized || currentStatus !== "OPENED" ) && bufferingData ) {
113+ if ( ( ! wsRef . current || cachedState . current . status !== "OPENED" ) && bufferingData ) {
116114 dataBuffer . current . push ( data ) ;
117115 } else {
118116 sendBuffer ( ) ;
119117 wsRef . current ?. send ( data ) ;
120118 }
121- } , [ bufferingData , currentStatus , wsInitialized , sendBuffer ] ) ;
119+ } , [ bufferingData , sendBuffer ] ) ;
122120
123121 const close = useCallback ( ( code ?: number , reason ?: string ) => {
124- if ( wsInitialized && currentStatus === "OPENED" ) {
122+ if ( wsRef . current && cachedState . current . status === "OPENED" ) {
125123 wsRef . current ?. close ( code , reason ) ;
126124 cachedState . current . status = "CLOSED" ;
127125 notifyRef . current && notifyRef . current ( ) ;
128126 }
129- } , [ wsInitialized , currentStatus ] ) ;
127+ } , [ ] ) ;
130128
131129 const state = useSyncExternalStore (
132130 useCallback ( notif => {
0 commit comments