Skip to content

Commit f10f24f

Browse files
fix: User presence not updating (#2023)
* Log 'power-monitor/get-system-idle-state' requests * Reload injected code in development mode * Fix error handling on renderer processes * Set up presence listener after setting detection * Handle multiple RocketChatDesktop.setUserPresenceDetection() calls * Remove unnecessary negation Co-authored-by: Diego Sampaio <chinello@gmail.com>
1 parent 859de06 commit f10f24f

4 files changed

Lines changed: 40 additions & 63 deletions

File tree

src/errors.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Bugsnag from '@bugsnag/js';
22
import { app } from 'electron';
33

44
import { APP_ERROR_THROWN } from './app/actions';
5-
import { select, dispatch, listen } from './store';
5+
import { select, listen } from './store';
66
import { whenReady } from './whenReady';
77

88
type AppType = 'main' | 'rootWindow' | 'webviewPreload';
@@ -63,26 +63,5 @@ export const setupRendererErrorHandling = async (
6363
}
6464

6565
setupBugsnag(apiKey, appVersion, appType);
66-
return;
6766
}
68-
69-
const dispatchError = (error: Error): void => {
70-
dispatch({
71-
type: APP_ERROR_THROWN,
72-
payload: {
73-
message: error.message,
74-
stack: error.stack,
75-
name: error.name,
76-
},
77-
error: true,
78-
});
79-
};
80-
81-
window.addEventListener('error', (event): void => {
82-
dispatchError(event.error);
83-
});
84-
85-
window.addEventListener('unhandledrejection', (event): void => {
86-
dispatchError(event.reason);
87-
});
8867
};

src/preload.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { setServerUrl } from './servers/preload/urls';
1414
import { createRendererReduxStore } from './store';
1515
import { listenToMessageBoxEvents } from './ui/preload/messageBox';
1616
import { handleTrafficLightsSpacing } from './ui/preload/sidebar';
17-
import { listenToUserPresenceChanges } from './userPresence/preload';
1817
import { whenReady } from './whenReady';
1918

2019
declare global {
@@ -50,7 +49,6 @@ const start = async (): Promise<void> => {
5049

5150
listenToNotificationsRequests();
5251
listenToScreenSharingRequests();
53-
listenToUserPresenceChanges();
5452
listenToMessageBoxEvents();
5553
handleTrafficLightsSpacing();
5654
};

src/ui/main/serverView/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export const attachGuestWebContentsEvents = async (): Promise<void> => {
374374
)?.[0]
375375
);
376376

377-
let injectableCode: string;
377+
let injectableCode: string | undefined;
378378
handle('server-view/ready', async (webContents) => {
379379
if (!injectableCode) {
380380
injectableCode = await fs.promises.readFile(
@@ -384,5 +384,9 @@ export const attachGuestWebContentsEvents = async (): Promise<void> => {
384384
}
385385

386386
webContents.executeJavaScript(injectableCode, true);
387+
388+
if (process.env.NODE_ENV === 'development') {
389+
injectableCode = undefined;
390+
}
387391
});
388392
};

src/userPresence/preload.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,69 @@
11
import { invoke } from '../ipc/renderer';
22
import { listen } from '../store';
3+
import { RootAction } from '../store/actions';
34
import { SYSTEM_SUSPENDING, SYSTEM_LOCKING_SCREEN } from './actions';
45
import { SystemIdleState } from './common';
56

6-
let isAutoAwayEnabled: boolean;
7-
let idleThreshold: number | null;
8-
let goOnline = (): void => undefined;
9-
let goAway = (): void => undefined;
7+
let detachCallbacks: () => void;
108

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>;
1231
let prevState: SystemIdleState;
1332
const pollSystemIdleState = async (): Promise<void> => {
1433
if (!isAutoAwayEnabled || !idleThreshold) {
1534
return;
1635
}
1736

37+
pollingTimer = setTimeout(pollSystemIdleState, 2000);
38+
1839
const state = await invoke(
1940
'power-monitor/get-system-idle-state',
2041
idleThreshold
2142
);
2243

2344
if (prevState === state) {
24-
setTimeout(pollSystemIdleState, 1000);
2545
return;
2646
}
2747

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);
3650

3751
prevState = state;
38-
setTimeout(pollSystemIdleState, 1000);
3952
};
4053

4154
pollSystemIdleState();
42-
};
4355

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+
};
6260
};
6361

6462
export const setUserPresenceDetection = (options: {
6563
isAutoAwayEnabled: boolean;
6664
idleThreshold: number | null;
6765
setUserOnline: (online: boolean) => void;
6866
}): 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);
7369
};

0 commit comments

Comments
 (0)