|
1 | | -import { useEffect } from "react"; |
| 1 | +import { useCallback, useEffect } from "react"; |
2 | 2 | import { useControlContextActions } from "./useControlContext"; |
3 | 3 |
|
4 | 4 | export const useIpc = () => { |
5 | 5 | const { setUser, setAuthenticated, setResources } = |
6 | 6 | useControlContextActions(); |
7 | 7 |
|
| 8 | + const subscribeSync = useCallback(() => { |
| 9 | + return window.electron.receive.subscribeSync( |
| 10 | + ({ isAuthenticated, isResources, isUser }) => { |
| 11 | + if (isAuthenticated !== undefined) { |
| 12 | + setAuthenticated(isAuthenticated); |
| 13 | + } |
| 14 | + |
| 15 | + if (isUser !== undefined) { |
| 16 | + setUser(isUser); |
| 17 | + } |
| 18 | + |
| 19 | + if (isResources !== undefined) { |
| 20 | + setResources(isResources); |
| 21 | + } |
| 22 | + }, |
| 23 | + ); |
| 24 | + }, []); |
| 25 | + |
8 | 26 | useEffect(() => { |
9 | 27 | window.electron.send.sync(); |
10 | 28 | }, []); |
11 | 29 |
|
12 | 30 | useEffect(() => { |
13 | | - const unSub = window.electron.receive.subscribeSync( |
14 | | - ({ isAuthenticated, isResources, isUser }) => { |
15 | | - setAuthenticated((prevValue) => { |
16 | | - if (prevValue === undefined) { |
17 | | - return isAuthenticated; |
18 | | - } |
19 | | - |
20 | | - if (prevValue !== undefined && isAuthenticated === undefined) { |
21 | | - return prevValue; |
22 | | - } |
23 | | - |
24 | | - if (isAuthenticated !== undefined && prevValue !== undefined) { |
25 | | - return isAuthenticated; |
26 | | - } |
27 | | - }); |
28 | | - setUser((prevValue) => { |
29 | | - if (prevValue === undefined) { |
30 | | - return isUser; |
31 | | - } |
32 | | - |
33 | | - if (prevValue !== undefined && isUser === undefined) { |
34 | | - return prevValue; |
35 | | - } |
36 | | - |
37 | | - if (isUser !== undefined && prevValue !== undefined) { |
38 | | - return isUser; |
39 | | - } |
40 | | - }); |
41 | | - setResources((prevValue) => { |
42 | | - if (prevValue === undefined) { |
43 | | - return isResources; |
44 | | - } |
45 | | - |
46 | | - if (prevValue !== undefined && isResources === undefined) { |
47 | | - return prevValue; |
48 | | - } |
49 | | - |
50 | | - if (isResources !== undefined && prevValue !== undefined) { |
51 | | - return isResources; |
52 | | - } |
53 | | - }); |
54 | | - } |
55 | | - ); |
| 31 | + const unSub = subscribeSync(); |
56 | 32 |
|
57 | 33 | return unSub; |
58 | 34 | }, []); |
|
0 commit comments