Skip to content

Commit

Permalink
fix(ecs): fix #178, refactor listener handling
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 24, 2020
1 parent 68b8d89 commit 5813afc
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions packages/ecs/src/groups/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ export class Group<SPEC, K extends ComponentID<SPEC>> implements IID<string> {
this.id = opts.id;
this.cache = opts.cache || new UnboundedCache();

this.info = comps.reduce(
(acc: GroupInfo<SPEC, K>, c) => {
acc[c.id] = {
values: <any>c.vals,
size: c.size,
stride: c.stride
};
return acc;
},
<any>{}
);
this.info = comps.reduce((acc: GroupInfo<SPEC, K>, c) => {
acc[c.id] = {
values: <any>c.vals,
size: c.size,
stride: c.stride
};
return acc;
}, <any>{});

// update ownerships
owned.forEach((c) => {
Expand All @@ -70,20 +67,11 @@ export class Group<SPEC, K extends ComponentID<SPEC>> implements IID<string> {
});
this.owned = owned;
this.addExisting();

comps.forEach((comp) => {
comp.addListener(EVENT_ADDED, this.onAddListener, this);
comp.addListener(EVENT_PRE_DELETE, this.onDeleteListener, this);
comp.addListener(EVENT_CHANGED, this.onChangeListener, this);
});
this.addRemoveListeners(true);
}

release() {
this.components.forEach((comp) => {
comp.removeListener(EVENT_ADDED, this.onAddListener, this);
comp.removeListener(EVENT_PRE_DELETE, this.onDeleteListener, this);
comp.removeListener(EVENT_CHANGED, this.onChangeListener, this);
});
this.addRemoveListeners(false);
this.cache.release();
}

Expand Down Expand Up @@ -231,4 +219,13 @@ export class Group<SPEC, K extends ComponentID<SPEC>> implements IID<string> {
`group ${this.id} isn't fully owning its components`
);
}

protected addRemoveListeners(add: boolean) {
const f = add ? "addListener" : "removeListener";
this.components.forEach((comp) => {
comp[f](EVENT_ADDED, this.onAddListener, this);
comp[f](EVENT_PRE_DELETE, this.onDeleteListener, this);
comp[f](EVENT_CHANGED, this.onChangeListener, this);
});
}
}

0 comments on commit 5813afc

Please sign in to comment.