|
1 | 1 | import { invoke } from '../ipc/renderer'; |
2 | 2 | import { listen } from '../store'; |
| 3 | +import { RootAction } from '../store/actions'; |
3 | 4 | import { SYSTEM_SUSPENDING, SYSTEM_LOCKING_SCREEN } from './actions'; |
4 | 5 | import { SystemIdleState } from './common'; |
5 | 6 |
|
6 | | -let isAutoAwayEnabled: boolean; |
7 | | -let idleThreshold: number | null; |
8 | | -let goOnline = (): void => undefined; |
9 | | -let goAway = (): void => undefined; |
| 7 | +let detachCallbacks: () => void; |
10 | 8 |
|
11 | | -const setupUserPresenceListening = (): void => { |
| 9 | +const attachCallbacks = ({ |
| 10 | + isAutoAwayEnabled, |
| 11 | + idleThreshold, |
| 12 | + setUserOnline, |
| 13 | +}: { |
| 14 | + isAutoAwayEnabled: boolean; |
| 15 | + idleThreshold: number | null; |
| 16 | + setUserOnline: (online: boolean) => void; |
| 17 | +}): (() => void) => { |
| 18 | + const unsubscribeFromPowerMonitorEvents = listen( |
| 19 | + (action): action is RootAction => |
| 20 | + [SYSTEM_SUSPENDING, SYSTEM_LOCKING_SCREEN].includes(action.type), |
| 21 | + () => { |
| 22 | + if (!isAutoAwayEnabled) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + setUserOnline(false); |
| 27 | + } |
| 28 | + ); |
| 29 | + |
| 30 | + let pollingTimer: ReturnType<typeof setTimeout>; |
12 | 31 | let prevState: SystemIdleState; |
13 | 32 | const pollSystemIdleState = async (): Promise<void> => { |
14 | 33 | if (!isAutoAwayEnabled || !idleThreshold) { |
15 | 34 | return; |
16 | 35 | } |
17 | 36 |
|
| 37 | + pollingTimer = setTimeout(pollSystemIdleState, 2000); |
| 38 | + |
18 | 39 | const state = await invoke( |
19 | 40 | 'power-monitor/get-system-idle-state', |
20 | 41 | idleThreshold |
21 | 42 | ); |
22 | 43 |
|
23 | 44 | if (prevState === state) { |
24 | | - setTimeout(pollSystemIdleState, 1000); |
25 | 45 | return; |
26 | 46 | } |
27 | 47 |
|
28 | | - const isOnline = |
29 | | - !isAutoAwayEnabled || state === 'active' || state === 'unknown'; |
30 | | - |
31 | | - if (isOnline) { |
32 | | - goOnline(); |
33 | | - } else { |
34 | | - goAway(); |
35 | | - } |
| 48 | + const isOnline = state === 'active' || state === 'unknown'; |
| 49 | + setUserOnline(isOnline); |
36 | 50 |
|
37 | 51 | prevState = state; |
38 | | - setTimeout(pollSystemIdleState, 1000); |
39 | 52 | }; |
40 | 53 |
|
41 | 54 | pollSystemIdleState(); |
42 | | -}; |
43 | 55 |
|
44 | | -export const listenToUserPresenceChanges = (): void => { |
45 | | - setupUserPresenceListening(); |
46 | | - |
47 | | - listen(SYSTEM_SUSPENDING, () => { |
48 | | - if (!isAutoAwayEnabled) { |
49 | | - return; |
50 | | - } |
51 | | - |
52 | | - goAway(); |
53 | | - }); |
54 | | - |
55 | | - listen(SYSTEM_LOCKING_SCREEN, () => { |
56 | | - if (!isAutoAwayEnabled) { |
57 | | - return; |
58 | | - } |
59 | | - |
60 | | - goAway(); |
61 | | - }); |
| 56 | + return (): void => { |
| 57 | + unsubscribeFromPowerMonitorEvents(); |
| 58 | + clearTimeout(pollingTimer); |
| 59 | + }; |
62 | 60 | }; |
63 | 61 |
|
64 | 62 | export const setUserPresenceDetection = (options: { |
65 | 63 | isAutoAwayEnabled: boolean; |
66 | 64 | idleThreshold: number | null; |
67 | 65 | setUserOnline: (online: boolean) => void; |
68 | 66 | }): void => { |
69 | | - isAutoAwayEnabled = options.isAutoAwayEnabled; |
70 | | - idleThreshold = options.idleThreshold; |
71 | | - goOnline = () => options.setUserOnline(true); |
72 | | - goAway = () => options.setUserOnline(false); |
| 67 | + detachCallbacks?.(); |
| 68 | + detachCallbacks = attachCallbacks(options); |
73 | 69 | }; |
0 commit comments