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 | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 18 | import { createContext, useEffect, useMemo, useRef } from 'react'; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 19 | |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 20 | import { WebSocketClient } from '../services/WebSocketClient'; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 21 | import { apiUrl } from '../utils/constants'; |
| 22 | import { WithChildren } from '../utils/utils'; |
| 23 | import { useAuthContext } from './AuthProvider'; |
| 24 | |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 25 | export interface IWebSocketContext { |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 26 | bind: WebSocketClient['bind']; |
| 27 | unbind: WebSocketClient['unbind']; |
| 28 | send: WebSocketClient['send']; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | export const WebSocketContext = createContext<IWebSocketContext | undefined>(undefined); |
| 32 | |
| 33 | export default ({ children }: WithChildren) => { |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 34 | const webSocketClientRef = useRef<WebSocketClient>(new WebSocketClient()); |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 35 | |
| 36 | const { token: accessToken } = useAuthContext(); |
| 37 | |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 38 | useEffect(() => { |
| 39 | webSocketClientRef.current.connect(apiUrl, accessToken); |
Issam E. Maghni | 0432cb7 | 2022-11-12 06:09:26 +0000 | [diff] [blame] | 40 | }, [accessToken]); |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 41 | |
simon | 90efa04 | 2022-11-29 17:17:23 -0500 | [diff] [blame] | 42 | const value: IWebSocketContext = useMemo( |
| 43 | () => ({ |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 44 | bind: webSocketClientRef.current.bind.bind(webSocketClientRef.current), |
| 45 | unbind: webSocketClientRef.current.unbind.bind(webSocketClientRef.current), |
| 46 | send: webSocketClientRef.current.send.bind(webSocketClientRef.current), |
simon | 90efa04 | 2022-11-29 17:17:23 -0500 | [diff] [blame] | 47 | }), |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 48 | [] |
simon | 90efa04 | 2022-11-29 17:17:23 -0500 | [diff] [blame] | 49 | ); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 50 | |
idillon | dfb9f1f | 2023-01-23 10:32:51 -0500 | [diff] [blame] | 51 | return <WebSocketContext.Provider value={value}>{children}</WebSocketContext.Provider>; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 52 | }; |