Skip to content

Commit 51e7f5c

Browse files
committed
fix(nodes): node added to children after loading are now loaded
1 parent dd8032b commit 51e7f5c

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/core/Engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Engine {
6161
/**
6262
* Inits the engine by registering default core systems.
6363
*/
64-
private init(): void {}
64+
private init(): void { }
6565

6666
/**
6767
* Loads systems/rootNode and starts the update loops.

src/core/InnerNode.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ export class InnerNode extends TreeNode {
2525
}
2626

2727
/**
28-
* Adds a child node to this node children.
28+
* Adds a child node to this node children and loads it.
2929
* @param {TreeNode} node Node to add as child.
3030
* @returns {number} New children array length.
3131
* @sealed
3232
*/
3333
public add(node: TreeNode): number {
3434
this.children.push(node);
35+
// Load the added node if added after loading.
36+
if (this.isLoaded) node.load();
3537
return this.children.length;
3638
}
3739

@@ -67,6 +69,7 @@ export class InnerNode extends TreeNode {
6769
for (let i = 0, len = this.children.length; i !== len; ++i) {
6870
this.children[i].load();
6971
}
72+
this.isLoaded = true;
7073
}
7174

7275
/**
@@ -109,14 +112,15 @@ export class InnerNode extends TreeNode {
109112
for (let i = 0, len = this.children.length; i !== len; ++i) {
110113
this.children[i].unload();
111114
}
115+
this.isLoaded = false;
112116
}
113117

114118
/**
115119
* Called by the node's constructor at instantiation,
116120
* this function is to be implemented when needed.
117121
* @virtual
118122
*/
119-
protected onCreate(): void {}
123+
protected onCreate(): void { }
120124

121125
/**
122126
* Called by the parent node when loaded,
@@ -126,21 +130,21 @@ export class InnerNode extends TreeNode {
126130
* function will be called by the engine instead.
127131
* @virtual
128132
*/
129-
protected onLoad(): void {}
133+
protected onLoad(): void { }
130134

131135
/**
132136
* Called by the parent node at each step of the loop,
133137
* this function is to be implemented when needed.
134138
* @virtual
135139
*/
136-
protected onStep(): void {}
140+
protected onStep(): void { }
137141

138142
/**
139143
* Called by the parent node at each fixed step of the loop,
140144
* this function is to be implemented when needed.
141145
* @virtual
142146
*/
143-
protected onFixedStep(): void {}
147+
protected onFixedStep(): void { }
144148

145149
/**
146150
* Called by the parent node at unload,
@@ -150,5 +154,5 @@ export class InnerNode extends TreeNode {
150154
* function will be called by the engine instead.
151155
* @virtual
152156
*/
153-
protected onUnload(): void {}
157+
protected onUnload(): void { }
154158
}

src/core/OuterNode.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class OuterNode extends TreeNode {
4242
*/
4343
public load(): void {
4444
this.onLoad();
45+
this.isLoaded = true;
4546
}
4647

4748
/**
@@ -75,14 +76,15 @@ export class OuterNode extends TreeNode {
7576
*/
7677
public unload(): void {
7778
this.onUnload();
79+
this.isLoaded = false;
7880
}
7981

8082
/**
8183
* Called by the node's constructor at instantiation,
8284
* this method is to be implemented when needed.
8385
* @virtual
8486
*/
85-
protected onCreate(): void {}
87+
protected onCreate(): void { }
8688

8789
/**
8890
* Called by the parent node when loaded,
@@ -92,21 +94,21 @@ export class OuterNode extends TreeNode {
9294
* method will be called by the engine instead.
9395
* @virtual
9496
*/
95-
protected onLoad(): void {}
97+
protected onLoad(): void { }
9698

9799
/**
98100
* Called by the parent node at each step of the loop,
99101
* this method is to be implemented when needed.
100102
* @virtual
101103
*/
102-
protected onStep(): void {}
104+
protected onStep(): void { }
103105

104106
/**
105107
* Called by the parent node at each fixed step of the loop,
106108
* this method is to be implemented when needed.
107109
* @virtual
108110
*/
109-
protected onFixedStep(): void {}
111+
protected onFixedStep(): void { }
110112

111113
/**
112114
* Called by the parent node at unload,
@@ -116,5 +118,5 @@ export class OuterNode extends TreeNode {
116118
* method will be called by the engine instead.
117119
* @virtual
118120
*/
119-
protected onUnload(): void {}
121+
protected onUnload(): void { }
120122
}

src/core/TreeNode.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export abstract class TreeNode {
2020
*/
2121
protected engine: Engine;
2222

23+
/**
24+
* Get wether the node is loaded.
25+
*/
26+
protected isLoaded: boolean;
27+
2328
/**
2429
* Get parent node.
2530
*/
@@ -36,6 +41,7 @@ export abstract class TreeNode {
3641
this.engine = parent;
3742
}
3843
this.id = nanoid(16);
44+
this.isLoaded = false;
3945
}
4046

4147
/**

0 commit comments

Comments
 (0)