Skip to content

Commit 703d758

Browse files
committed
fix: sim freezes on load in 2024 (#10397)
* fix: potentially huge deltaTime in `useUpdate` * chore: changelog * chore: move changelog entry to 0.14 (cherry picked from commit c5c4573)
1 parent 131058a commit 703d758

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

.github/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
1. [A32NX] Enable mouse drag for knobs in lock mode - @heclak (Heclak)
157157
1. [A32NX] Fix some knobs snapping back to start position when turned to max value - @heclak (Heclak)
158158
1. [ND] Fix terrain display smearing issue - @flogross89 (floridude)
159+
1. [MISC] Prevent long sim freezes in some scenarios in MSFS 2024 - @Benjozork (Benjamin Dupont)
159160

160161
## 0.13.0
161162

fbw-common/src/systems/instruments/src/BAT/BatDisplay.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export const BatDisplay = ({ batteryNumber, blankWhenOff = false, x, y }) => {
4949
// It's possible that for example the lights test is turned off mid-cycle and we end up rendering 87.6, as the last
5050
// two digits were updated before the first one.
5151
useUpdate((deltaTime) => {
52+
// FIXME this should probably use simulation time, not deltaTime
53+
5254
const displayValue = getDisplayValue();
5355

5456
// only update one digit at a time

fbw-common/src/systems/instruments/src/react/hooks.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
//
44
// SPDX-License-Identifier: GPL-3.0
55

6-
import React from 'react';
6+
import React, { useRef } from 'react';
77
import { GenericDataListenerSync } from '../../../shared/src/GenericDataListenerSync';
88
import { getRootElement } from '../defaults';
99

1010
export const useUpdate = (handler: (deltaTime: number) => void) => {
11+
const lastTime = useRef(Date.now());
12+
1113
// Logic based on https://usehooks.com/useEventListener/
1214
const savedHandler = React.useRef(handler);
1315
React.useEffect(() => {
1416
savedHandler.current = handler;
1517
}, [handler]);
1618

1719
React.useEffect(() => {
18-
const wrappedHandler = (event: CustomEvent) => {
19-
savedHandler.current(event.detail);
20+
const wrappedHandler = () => {
21+
const now = Date.now();
22+
23+
savedHandler.current(now - lastTime.current);
24+
25+
lastTime.current = now;
2026
};
2127

2228
getRootElement().addEventListener('update', wrappedHandler);

0 commit comments

Comments
 (0)