Skip to content

Commit

Permalink
Change getRemovedComponent to return readonly component
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepsteak committed Aug 2, 2020
1 parent 5fefe49 commit 6d8bdc2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Entity {
*/
getRemovedComponent<C extends Component<any>>(
Component: ComponentConstructor<C>
): C;
): Readonly<C> | undefined;

/**
* Get an object containing all the components on this entity, where the object keys are the component types.
Expand Down
6 changes: 5 additions & 1 deletion src/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export class Entity {
}

getRemovedComponent(Component) {
return this._componentsToRemove[Component._typeId];
const component = this._componentsToRemove[Component._typeId];

return process.env.NODE_ENV !== "production"
? wrapImmutableComponent(Component, component)
: component;
}

getComponents() {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,40 @@ test("get component production", async t => {
process.env.NODE_ENV = oldNodeEnv;
});

test("get removed component development", async t => {
var world = new World();

world.registerComponent(FooComponent);

// Sync
var entity = world.createEntity();
entity.addComponent(FooComponent);
entity.removeComponent(FooComponent);

const component = entity.getRemovedComponent(FooComponent);

t.throws(() => (component.variableFoo = 4));
});

test("get removed component production", async t => {
const oldNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = "production";
var world = new World();

world.registerComponent(FooComponent);

// Sync
var entity = world.createEntity();
entity.addComponent(FooComponent);
entity.removeComponent(FooComponent);

const component = entity.getRemovedComponent(FooComponent);

t.notThrows(() => (component.variableFoo = 4));

process.env.NODE_ENV = oldNodeEnv;
});

test("get mutable component", async t => {
var world = new World();

Expand Down

0 comments on commit 6d8bdc2

Please sign in to comment.