Skip to content

Commit 134708e

Browse files
authored
Update the "heartbeat" workaround for promise continuations (#81)
Switch to setTimeout, and switch to always running the heartbeat while the engine is alive (the old way doesn't work correctly on Android release builds)
1 parent b0b928f commit 134708e

File tree

1 file changed

+10
-43
lines changed

1 file changed

+10
-43
lines changed

Modules/@babylonjs/react-native/EngineHook.ts

+10-43
Original file line numberDiff line numberDiff line change
@@ -71,59 +71,26 @@ export function useEngine(): Engine | undefined {
7171
(async () => {
7272
if (await BabylonModule.initialize() && !disposed)
7373
{
74-
const engine = new NativeEngine();
75-
76-
// NOTE: This is a workaround for https://github.com/BabylonJS/BabylonReactNative/issues/60
77-
let heartbeat: NodeJS.Timeout | null;
78-
const startHeartbeat = () => {
79-
if (!heartbeat) {
80-
heartbeat = setInterval(() => {}, 10);
81-
}
82-
};
83-
const stopHeartbeat = () => {
84-
if (heartbeat) {
85-
clearInterval(heartbeat);
86-
heartbeat = null;
87-
}
88-
}
89-
startHeartbeat();
90-
91-
let renderLoopCount = 0;
92-
93-
const originalRunRenderLoop: (...args: any[]) => void = engine.runRenderLoop;
94-
engine.runRenderLoop = function(...args: any[]): void {
95-
originalRunRenderLoop.apply(this, args);
96-
if (++renderLoopCount == 1) {
97-
stopHeartbeat();
98-
}
99-
}
100-
101-
const originalStopRenderLoop: (...args: any[]) => void = engine.stopRenderLoop;
102-
engine.stopRenderLoop = function(...args: any[]): void {
103-
if (--renderLoopCount == 0) {
104-
startHeartbeat();
105-
}
106-
originalStopRenderLoop.apply(this, args);
107-
}
108-
109-
const originalDipose = engine.dispose;
110-
engine.dispose = function() {
111-
originalDipose.apply(this);
112-
stopHeartbeat();
113-
};
114-
115-
setEngine(engine);
74+
setEngine(new NativeEngine());
11675
}
11776
})();
11877

78+
// NOTE: This is a workaround for https://github.com/BabylonJS/BabylonReactNative/issues/60
79+
function heartbeat() {
80+
if (!disposed) {
81+
setTimeout(heartbeat, 10);
82+
}
83+
}
84+
heartbeat();
85+
11986
return () => {
12087
disposed = true;
12188
setEngine(engine => {
12289
if (engine) {
12390
DisposeEngine(engine);
12491
}
12592
return undefined;
126-
})
93+
});
12794
};
12895
}, []);
12996

0 commit comments

Comments
 (0)