Skip to content

Commit

Permalink
Add explicit support for null url
Browse files Browse the repository at this point in the history
  • Loading branch information
robtaussig committed Apr 20, 2020
1 parent e6cf259 commit 7fdca5e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-use-websocket",
"version": "1.7.2",
"version": "1.7.3",
"description": "React Hook for WebSocket communication",
"main": "./dist/index.js",
"files": [
Expand Down
1 change: 1 addition & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const DEFAULT_RECONNECT_LIMIT = 20;
export const DEFAULT_RECONNECT_INTERVAL_MS = 5000;

export enum ReadyState {
UNINSTANTIATED = -1,
CONNECTING = 0,
OPEN = 1,
CLOSING = 2,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/use-socket-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const getSocketData = (event: WebSocketEventMap['message']): SocketIOMessageData
}

export const useSocketIO = (
url: string | (() => string | Promise<string>),
url: string | (() => string | Promise<string>) | null,
options: Options = DEFAULT_OPTIONS,
): [SendMessage, SocketIOMessageData, ReadyState, () => WebSocket] => {
const optionsWithSocketIO = useMemo(() => ({
Expand Down
6 changes: 4 additions & 2 deletions src/lib/use-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './types';

export const useWebSocket = (
url: string | (() => string | Promise<string>),
url: string | (() => string | Promise<string>) | null,
options: Options = DEFAULT_OPTIONS,
): [SendMessage, WebSocketEventMap['message'], ReadyState, () => WebSocket] => {
const [ lastMessage, setLastMessage ] = useState<WebSocketEventMap['message']>(null);
Expand All @@ -29,7 +29,9 @@ export const useWebSocket = (
const readyStateFromUrl =
convertedUrl.current && readyState[convertedUrl.current] !== undefined ?
readyState[convertedUrl.current] :
ReadyState.CONNECTING;
url !== null ?
ReadyState.CONNECTING :
ReadyState.UNINSTANTIATED;

const sendMessage: SendMessage = useCallback(message => {
if (webSocketRef.current && webSocketRef.current.readyState === ReadyState.OPEN) {
Expand Down

0 comments on commit 7fdca5e

Please sign in to comment.