Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 18 | import { useEffect, useMemo, useRef } from 'react'; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 19 | |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 20 | import { createOptionalContext } from '../hooks/createOptionalContext'; |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 21 | import { WebSocketClient } from '../services/WebSocketClient'; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 22 | import { apiUrl } from '../utils/constants'; |
| 23 | import { WithChildren } from '../utils/utils'; |
| 24 | import { useAuthContext } from './AuthProvider'; |
| 25 | |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 26 | export interface IWebSocketContext { |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 27 | bind: WebSocketClient['bind']; |
| 28 | unbind: WebSocketClient['unbind']; |
| 29 | send: WebSocketClient['send']; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 30 | } |
| 31 | |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 32 | const optionalWebSocketContext = createOptionalContext<IWebSocketContext>('WebSocketContext'); |
| 33 | export const useWebSocketContext = optionalWebSocketContext.useOptionalContext; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 34 | |
| 35 | export default ({ children }: WithChildren) => { |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 36 | const webSocketClientRef = useRef<WebSocketClient>(new WebSocketClient()); |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 37 | |
| 38 | const { token: accessToken } = useAuthContext(); |
| 39 | |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 40 | useEffect(() => { |
| 41 | webSocketClientRef.current.connect(apiUrl, accessToken); |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 42 | }, [accessToken]); |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 43 | |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 44 | const value = useMemo( |
simon | 90efa04 | 2022-11-29 17:17:23 -0500 | [diff] [blame] | 45 | () => ({ |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 46 | bind: webSocketClientRef.current.bind.bind(webSocketClientRef.current), |
| 47 | unbind: webSocketClientRef.current.unbind.bind(webSocketClientRef.current), |
| 48 | send: webSocketClientRef.current.send.bind(webSocketClientRef.current), |
simon | 90efa04 | 2022-11-29 17:17:23 -0500 | [diff] [blame] | 49 | }), |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 50 | [] |
simon | 90efa04 | 2022-11-29 17:17:23 -0500 | [diff] [blame] | 51 | ); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 52 | |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 53 | return ( |
| 54 | <optionalWebSocketContext.Context.Provider value={value}>{children}</optionalWebSocketContext.Context.Provider> |
| 55 | ); |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 56 | }; |