Skip to content

Commit 3d5fc09

Browse files
committed
Refactor eco.js to remove use of 'new Entity()'
1 parent bbbe09f commit 3d5fc09

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

dist/eco.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/unit/eco.spec.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import Eco from "../../src/eco";
22

3-
function mockEntityPrototype() {
4-
return jasmine.createSpyObj("Entity", ["defineComponent"]);
5-
}
6-
73
function mockEntityCollection() {
84
return jasmine.createSpyObj("entityCollection", [
95
"add",
@@ -14,18 +10,20 @@ function mockEntityCollection() {
1410

1511
describe("Eco", () => {
1612
it("should return entity instances", () => {
17-
const Entity = function() {};
13+
const entity = "foo";
14+
const createEntity = jasmine.createSpy().and.returnValue(entity);
15+
1816
const entityCollection = mockEntityCollection();
19-
const eco = new Eco(Entity, entityCollection);
17+
const eco = new Eco(createEntity, entityCollection);
2018

2119
const instance = eco.entity();
22-
expect(instance instanceof Entity).toBeTruthy();
20+
expect(createEntity).toHaveBeenCalledWith(eco);
21+
expect(instance).toBe(entity);
2322
expect(entityCollection.add).toHaveBeenCalledWith(instance);
2423
});
2524

2625
it("should call the onChange function property if a component changes", done => {
27-
const Entity = function() {};
28-
const eco = new Eco(Entity, mockEntityCollection());
26+
const eco = new Eco(() => ({}), mockEntityCollection());
2927

3028
const entity = {};
3129
const component = "foo";
@@ -44,46 +42,41 @@ describe("Eco", () => {
4442
});
4543

4644
it("should increment the entityCollection version when a component is added", () => {
47-
const Entity = function() {};
4845
const entityCollection = mockEntityCollection();
49-
const eco = new Eco(Entity, entityCollection);
46+
const eco = new Eco(() => ({}), entityCollection);
5047

5148
const entity = {};
5249
eco.onComponentChanged(entity, "foo", "bar", undefined);
5350
expect(entityCollection.incVersion).toHaveBeenCalledWith(entity, "foo");
5451
});
5552

5653
it("should increment the entityCollection version when a component is removed", () => {
57-
const Entity = function() {};
5854
const entityCollection = mockEntityCollection();
59-
const eco = new Eco(Entity, entityCollection);
55+
const eco = new Eco(() => ({}), entityCollection);
6056

6157
const entity = {};
6258
eco.onComponentChanged(entity, "foo", undefined, "bar");
6359
expect(entityCollection.incVersion).toHaveBeenCalledWith(entity, "foo");
6460
});
6561

6662
it("should not increment the entityCollection version if a component value has changed", () => {
67-
const Entity = function() {};
6863
const entityCollection = mockEntityCollection();
69-
const eco = new Eco(Entity, entityCollection);
64+
const eco = new Eco(() => ({}), entityCollection);
7065

7166
eco.onComponentChanged({}, "foo", "foo", "bar");
7267
expect(entityCollection.incVersion).not.toHaveBeenCalled();
7368
});
7469

7570
it("should return an array of all its entities", () => {
76-
const Entity = function() {};
7771
const entityCollection = mockEntityCollection();
78-
const eco = new Eco(Entity, entityCollection);
72+
const eco = new Eco(() => ({}), entityCollection);
7973
expect(eco.all).toBe(entityCollection.entities);
8074
});
8175

8276
it("should create and return iterator instances", () => {
83-
const Entity = function() {};
8477
const entityCollection = mockEntityCollection();
8578
const createIterator = jasmine.createSpy("createIterator");
86-
const eco = new Eco(Entity, entityCollection, createIterator);
79+
const eco = new Eco(() => ({}), entityCollection, createIterator);
8780

8881
const filter = {};
8982
createIterator.and.returnValue(filter);
@@ -98,10 +91,9 @@ describe("Eco", () => {
9891
});
9992

10093
it("should create and return filter instances with custom filter functions", () => {
101-
const Entity = function() {};
10294
const entityCollection = mockEntityCollection();
10395
const createIterator = jasmine.createSpy("createIterator");
104-
const eco = new Eco(Entity, entityCollection, createIterator);
96+
const eco = new Eco(() => ({}), entityCollection, createIterator);
10597

10698
const filter = {};
10799
createIterator.and.returnValue(filter);
@@ -118,19 +110,17 @@ describe("Eco", () => {
118110
});
119111

120112
it("should remove entities that have been disabled", () => {
121-
const Entity = function() {};
122113
const entityCollection = mockEntityCollection();
123-
const eco = new Eco(Entity, entityCollection);
114+
const eco = new Eco(() => ({}), entityCollection);
124115
const entity = eco.entity();
125116

126117
eco.onEntityStatusChanged(entity, false);
127118
expect(entityCollection.remove).toHaveBeenCalledWith(entity);
128119
});
129120

130121
it("should re-add entities that have been re-enabled", () => {
131-
const Entity = function() {};
132122
const entityCollection = mockEntityCollection();
133-
const eco = new Eco(Entity, entityCollection);
123+
const eco = new Eco(() => ({}), entityCollection);
134124
const entity = eco.entity();
135125

136126
eco.onEntityStatusChanged(entity, false);

src/eco.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88
export default class Eco {
99
/**
10-
* @param {function} Entity class to use to create entities
10+
* @param {function} createEntity function that returns an entity instance
1111
* @param {Object} entityCollection collection to hold entities
1212
* @param {function} createIterator function that returns an interator instance
1313
*/
14-
constructor(Entity, entities, createIterator) {
15-
this.Entity = Entity;
14+
constructor(createEntity, entities, createIterator) {
15+
this._createEntity = createEntity;
1616
this.entities = entities;
1717
this.createIterator = createIterator;
1818

@@ -66,7 +66,7 @@ export default class Eco {
6666
* @returns {Entity} the entity
6767
*/
6868
entity() {
69-
const entity = new this.Entity(this);
69+
const entity = this._createEntity(this);
7070
this.entities.add(entity);
7171
return entity;
7272
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function createContainer() {
2222
components.forEach(name => Entity.defineComponent(name));
2323

2424
return new container.Eco(
25-
Entity,
25+
eco => new Entity(eco),
2626
container.entityCollection(),
2727
container.createIterator
2828
);

0 commit comments

Comments
 (0)