Ensure WebSocket client is never undefined
Change-Id: I5f5c34112fa989d6c06697b8a6d46acfbd01008a
diff --git a/client/src/contexts/WebSocketProvider.tsx b/client/src/contexts/WebSocketProvider.tsx
index 44d54c0..1408953 100644
--- a/client/src/contexts/WebSocketProvider.tsx
+++ b/client/src/contexts/WebSocketProvider.tsx
@@ -15,8 +15,9 @@
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
-import { createContext, useEffect, useMemo, useRef } from 'react';
+import { useEffect, useMemo, useRef } from 'react';
+import { createOptionalContext } from '../hooks/createOptionalContext';
import { WebSocketClient } from '../services/WebSocketClient';
import { apiUrl } from '../utils/constants';
import { WithChildren } from '../utils/utils';
@@ -28,7 +29,8 @@
send: WebSocketClient['send'];
}
-export const WebSocketContext = createContext<IWebSocketContext | undefined>(undefined);
+const optionalWebSocketContext = createOptionalContext<IWebSocketContext>('WebSocketContext');
+export const useWebSocketContext = optionalWebSocketContext.useOptionalContext;
export default ({ children }: WithChildren) => {
const webSocketClientRef = useRef<WebSocketClient>(new WebSocketClient());
@@ -39,7 +41,7 @@
webSocketClientRef.current.connect(apiUrl, accessToken);
}, [accessToken]);
- const value: IWebSocketContext = useMemo(
+ const value = useMemo(
() => ({
bind: webSocketClientRef.current.bind.bind(webSocketClientRef.current),
unbind: webSocketClientRef.current.unbind.bind(webSocketClientRef.current),
@@ -48,5 +50,7 @@
[]
);
- return <WebSocketContext.Provider value={value}>{children}</WebSocketContext.Provider>;
+ return (
+ <optionalWebSocketContext.Context.Provider value={value}>{children}</optionalWebSocketContext.Context.Provider>
+ );
};