Skip to content

Commit 22a5032

Browse files
Merge pull request #212 from jonasi/isao/cleanup
ensure cleanup if unmount occurs before loading finishes
2 parents ce709ac + 32dc215 commit 22a5032

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
/distribution
77

88
# System
9-
.DS_Store
9+
.DS_Store
10+
*.swp

source/components/unity.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export class Unity extends PureComponent<IUnityProps, {}> {
2727
*/
2828
private htmlCanvasElementReference?: HTMLCanvasElement;
2929

30+
/**
31+
* A flag representing the component's mount state
32+
* @private
33+
* @type {boolean}
34+
*/
35+
private mounted: boolean = false;
36+
3037
/**
3138
* Event invoked by the UnityInstance when the initialization is progressing.
3239
* Will be used to track the loading progression and invokes the event listeners
@@ -52,6 +59,7 @@ export class Unity extends PureComponent<IUnityProps, {}> {
5259
* @public
5360
*/
5461
public componentDidMount(): void {
62+
this.mounted = true;
5563
this.mountUnityInstance();
5664
}
5765

@@ -62,6 +70,7 @@ export class Unity extends PureComponent<IUnityProps, {}> {
6270
*/
6371
public componentWillUnmount(): void {
6472
this.unityContext.quitUnityInstance();
73+
this.mounted = false;
6574
}
6675

6776
/**
@@ -81,6 +90,9 @@ export class Unity extends PureComponent<IUnityProps, {}> {
8190
await this.unityLoaderService.addFromUrl(
8291
this.unityContext.unityConfig.loaderUrl
8392
);
93+
if (!this.mounted) {
94+
return;
95+
}
8496
const _unityInstanceParameters: IUnityInstanceParameters = {
8597
...this.unityContext.unityConfig,
8698
printErr: (message: string) =>
@@ -99,6 +111,9 @@ export class Unity extends PureComponent<IUnityProps, {}> {
99111
this.onProgress.bind(this)
100112
);
101113
this.unityContext.setUnityInstance(_unityInstance);
114+
if (!this.mounted) {
115+
return this.unityContext.quitUnityInstance();
116+
}
102117
} catch (message) {
103118
this.unityContext.dispatchEventListener("error", message);
104119
console.error("A problem occurred while mounting", message);

0 commit comments

Comments
 (0)