Skip to content

Commit 080125c

Browse files
authored
Workaround for promise continuations not executing (#63)
1 parent 9daae33 commit 080125c

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,48 @@ export function useEngine(): Engine | undefined {
6666
}
6767
}
6868

69-
setEngine(engine = new NativeEngine());
69+
engine = new NativeEngine();
70+
71+
// NOTE: This is a workaround for https://github.com/BabylonJS/BabylonReactNative/issues/60
72+
let heartbeat: NodeJS.Timeout | null;
73+
const startHeartbeat = () => {
74+
if (!heartbeat) {
75+
heartbeat = setInterval(() => {}, 10);
76+
}
77+
};
78+
const stopHeartbeat = () => {
79+
if (heartbeat) {
80+
clearInterval(heartbeat);
81+
heartbeat = null;
82+
}
83+
}
84+
startHeartbeat();
85+
86+
let renderLoopCount = 0;
87+
88+
const originalRunRenderLoop: (...args: any[]) => void = engine.runRenderLoop;
89+
engine.runRenderLoop = function(...args: any[]): void {
90+
originalRunRenderLoop.apply(this, args);
91+
if (++renderLoopCount == 1) {
92+
stopHeartbeat();
93+
}
94+
}
95+
96+
const originalStopRenderLoop: (...args: any[]) => void = engine.stopRenderLoop;
97+
engine.stopRenderLoop = function(...args: any[]): void {
98+
if (--renderLoopCount == 0) {
99+
startHeartbeat();
100+
}
101+
originalStopRenderLoop.apply(this, args);
102+
}
103+
104+
const originalDipose = engine.dispose;
105+
engine.dispose = function() {
106+
originalDipose.apply(this);
107+
stopHeartbeat();
108+
};
109+
110+
setEngine(engine);
70111
}
71112
})();
72113

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,30 +115,33 @@ After having run the above commands, you can also open `Apps/Playground/ios/Play
115115

116116
### **Building the NPM Package**
117117

118-
An NPM package can be built in two different ways: as source, and as binaries. Source is useful if you want to debug the Babylon React Native source in the context of the project consuming it, though configuration is a bit more involved. Binaries are useful in that they simplify configuration in the consuming app, though they cannot be debugged so easily. The binary package is what is published to [npmjs.org](https://www.npmjs.com/package/@babylonjs/react-native).
118+
If you want to test using a local build of the NPM package with your own React Native app, you can do so with a `gulp` command on a Mac (this requires a Mac as it builds binaries for both iOS and Android).
119119

120-
#### Source Package
120+
```
121+
cd Package
122+
npm install
123+
gulp pack
124+
```
121125

122-
If you want to test using a local build of the source-based NPM package with your own React Native app, you can do so with the `npm pack` command on either Mac or Windows.
126+
The NPM package will be built into the `Package` directory where the `gulp` command was run. Once the local NPM package has been built, it can be installed into a project using `npm`.
123127

124128
```
125-
cd Apps/Playground/node_modules/@babylonjs/react-native
126-
npm pack
129+
cd <directory of your React Native app>
130+
npm install <root directory of your BabylonReactNative clone>/Package/Assembled/babylonjs-react-native-0.0.1.tgz
127131
```
128132

129-
This will produce a zipped local NPM source-based package that can be installed into a React Native application for testing purposes. Note that when testing a source based package for iOS, the XCode project needs to be generated with `CMake` as described [above](#ios).
133+
### **Debugging in Context**
130134

131-
#### Binary Package
132-
133-
If you want to test using a local build of the binary-based NPM package with your own React Native app, you can do so with a `gulp` command on a Mac (this requires a Mac as it builds binaries for both iOS and Android).
135+
If you want to consume `@babylonjs/react-native` as source in your React Native app (for debugging or for iterating on the code when making a contribution), you can install the package source directory as an npm package.
134136

135137
```
136-
cd Package
137-
npm install
138-
gulp pack
138+
cd <directory of your React Native app>
139+
npm install <root directory of your BabylonReactNative clone>/Modules/@babylonjs/react-native
140+
cd ios
141+
pod install
139142
```
140143

141-
This will produce a zipped local NPM binary-based package that can be installed into a React Native application for testing purposes.
144+
This will create a symbolic link in your `node_modules` directory to the `@babylonjs/react-native` source directory. For iOS the XCode project needs to be generated with `CMake` as described [above](#ios) and added to your `xcworkspace`.
142145

143146
## Security
144147

0 commit comments

Comments
 (0)