Skip to content

Commit

Permalink
Updated entity.destroy to destroy at end of current event loop;
Browse files Browse the repository at this point in the history
Added entity.destroyImmediate with tests;
  • Loading branch information
RGBboy committed Jul 2, 2014
1 parent abc31b2 commit b8c4434
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,25 @@ Entity = function (componentSystem) {
};

/**
* .destroy
* .destroyImmediate
*
* @api public
*/
entity.destroy = function () {
entity.destroyImmediate = function () {
entity.emit('destroy', entity);
components.forEach(removeComponentFromMap);
entity.removeAllListeners('destroy');
};

/**
* .destroy
*
* @api public
*/
entity.destroy = function () {
process.nextTick(entity.destroyImmediate);
};

return entity;
};

Expand Down
47 changes: 45 additions & 2 deletions test/entity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,29 +238,72 @@ test('entity.destroy should emit a destroy event', function (t) {
entity.on('destroy', function (data) {
t.pass('destroy fired');
t.equal(entity, data);
teardown(t);
});
entity.destroy();
});

test('entity.destroy should call destroyImmediate at end of current event loop', function (t) {

setup(t);
t.plan(2);

sandbox.spy(entity, 'destroyImmediate');

entity.destroy();

t.false(entity.destroyImmediate.calledOnce, 'entity.destroyImmediate should not be called');

// destroyImmediate is called at end of current event loop
process.nextTick(function () {
t.true(entity.destroyImmediate.calledOnce, 'entity.destroyImmediate should be called');
teardown(t);
});

});

/**
* entity.destroyImmediate
*/

test('entity.destroyImmediate should be a function', function (t) {
setup(t);
t.plan(1);
t.equal(typeof entity.destroyImmediate, 'function');
teardown(t);
});

test('entity.destroy should call destroy on all components', function (t) {
test('entity.destroyImmediate should emit a destroy event', function (t) {
setup(t);
t.plan(2);
entity.on('destroy', function (data) {
t.pass('destroy fired');
t.equal(entity, data);
teardown(t);
});
entity.destroyImmediate();
});

test('entity.destroyImmediate should call destroy on all components', function (t) {
var CustomComponent1 = function () {},
CustomComponent2 = function () {},
componentMock1 = ComponentMock(sandbox),
componentMock2 = ComponentMock(sandbox),
returnedComponent1,
returnedComponent2;

setup(t);
t.plan(2);

componentSystemMock.create.returns(componentMock1);
returnedComponent1 = entity.addComponent(CustomComponent1);
componentSystemMock.create.returns(componentMock2);
returnedComponent2 = entity.addComponent(CustomComponent2);
entity.destroy();
entity.destroyImmediate();

t.ok(returnedComponent1.destroy.calledOnce, 'component.destroy is called once');
t.ok(returnedComponent2.destroy.calledOnce, 'component.destroy is called once');

teardown(t);

});

0 comments on commit b8c4434

Please sign in to comment.