chore: tidy

This commit is contained in:
cha0s 2019-10-08 02:29:24 -05:00
parent 4342590c56
commit ab37214926

View File

@ -31,12 +31,15 @@ export class Spawner extends decorate(Trait) {
} }
destroy() { destroy() {
this.children = []; while (this.children.length > 0) {
this.removeChild(this.children.pop());
}
} }
constructor(entity, params, state) { constructor(entity, params, state) {
super(entity, params, state); super(entity, params, state);
this.children = []; this.children = [];
this.childrenListeners = new Map();
this.spawnJSONs = this.params.spawns; this.spawnJSONs = this.params.spawns;
} }
@ -63,6 +66,16 @@ export class Spawner extends decorate(Trait) {
return true; return true;
} }
removeChild(child) {
const index = this.children.indexOf(child);
if (-1 !== index) {
this.children.splice(index, 1);
const listener = this.childrenListeners.get(child);
child.off('destroy', listener);
this.childrenListeners.delete(child);
}
}
listeners() { listeners() {
return { return {
@ -100,14 +113,13 @@ export class Spawner extends decorate(Trait) {
// Add null to children to prevent race. // Add null to children to prevent race.
const childIndex = this.children.length; const childIndex = this.children.length;
this.children.push(null); this.children.push(null);
return Entity.loadOrInstance(mergedJSON).then((spawn) => { return Entity.loadOrInstance(mergedJSON).then((child) => {
this.children[childIndex] = spawn; this.children[childIndex] = child;
spawn.once('destroy', () => { const listener = this.removeChild.bind(this, child);
const index = this.children.indexOf(spawn); this.childrenListeners.set(child, listener);
this.children.splice(index, 1); child.once('destroy', listener);
}) list.addEntity(child);
list.addEntity(spawn); return child;
return spawn;
}); });
}, },