Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow EntityCollection events to be reentrant #3739

Merged
merged 5 commits into from
Mar 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Change Log
* Fix issue where the `GroundPrimitive` volume was being clipped by the far plane. [#3706](https://github.com/AnalyticalGraphicsInc/cesium/issues/3706)
* Fixed issue where `Camera.computeViewRectangle` was incorrect when crossing the international date line [#3717](https://github.com/AnalyticalGraphicsInc/cesium/issues/3717)
* Added `Rectangle` result parameter to `Camera.computeViewRectangle`
* Fixed a reentrancy bug in `EntityCollection.collectionChanged`. [#3739](https://github.com/AnalyticalGraphicsInc/cesium/pull/3739)
* Fix bug when upsampling exaggerated terrain where the terrain heights were exaggerated at twice the value. [#3607](https://github.com/AnalyticalGraphicsInc/cesium/issues/3607)

### 1.19 - 2016-03-01
Expand Down
24 changes: 20 additions & 4 deletions Source/DataSources/EntityCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,29 @@ define([
};

function fireChangedEvent(collection) {
if (collection._firing) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_firing is never set to true, as far as I can tell. I think you must be missing some tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. What's weird is the test I wrote fails in master and passes in this branch (doing the right thing as far as I can tell). But what I'm not catching is checking that the second event is not fired until the first even is finished, which might be hard to test. I'll look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this should be fixed. I could add a more elaborate test if you think it's worth it that uses multiple spies to make sure things are called in the expected order.

collection._refire = true;
return;
}

if (collection._suspendCount === 0) {
var added = collection._addedEntities;
var removed = collection._removedEntities;
var changed = collection._changedEntities;
if (changed.length !== 0 || added.length !== 0 || removed.length !== 0) {
collection._collectionChanged.raiseEvent(collection, added.values, removed.values, changed.values);
added.removeAll();
removed.removeAll();
changed.removeAll();
collection._firing = true;
do {
collection._refire = false;
var addedArray = added.values.slice(0);
var removedArray = removed.values.slice(0);
var changedArray = changed.values.slice(0);

added.removeAll();
removed.removeAll();
changed.removeAll();
collection._collectionChanged.raiseEvent(collection, addedArray, removedArray, changedArray);
} while (collection._refire);
collection._firing = false;
}
}
}
Expand All @@ -60,6 +74,8 @@ define([
this._collectionChanged = new Event();
this._id = createGuid();
this._show = true;
this._firing = false;
this._refire = false;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions Specs/DataSources/EntityCollectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,53 @@ defineSuite([
entityCollection.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);
});

it('raises expected events when reentrant', function() {
var entityCollection = new EntityCollection();

var entity = new Entity();
var entity2 = new Entity();
entityCollection.add(entity);
entityCollection.add(entity2);

var entityToDelete = new Entity();
entityCollection.add(entityToDelete);

var entityToAdd = new Entity();

var inCallback = false;
var listener = jasmine.createSpy('listener').and.callFake(function(collection, added, removed, changed) {
//When we set the name to `newName` below, this code will modify entity2's name, thus triggering
//another event firing that occurs after all current subscribers have been notified of the
//event we are inside of.

//By checking that inCallback is false, we are making sure the entity2.name assignment
//is delayed until after the first round of events is fired.
expect(inCallback).toBe(false);
inCallback = true;
if (entity2.name !== 'Bob') {
entity2.name = 'Bob';
}
if (entityCollection.contains(entityToDelete)) {
entityCollection.removeById(entityToDelete.id);
}
if (!entityCollection.contains(entityToAdd)) {
entityCollection.add(entityToAdd);
}
inCallback = false;
});
entityCollection.collectionChanged.addEventListener(listener);

entity.name = 'newName';
expect(listener.calls.count()).toBe(2);
expect(listener.calls.argsFor(0)).toEqual([entityCollection, [], [], [entity]]);
expect(listener.calls.argsFor(1)).toEqual([entityCollection, [entityToAdd], [entityToDelete], [entity2]]);

expect(entity.name).toEqual('newName');
expect(entity2.name).toEqual('Bob');
expect(entityCollection.contains(entityToDelete)).toEqual(false);
expect(entityCollection.contains(entityToAdd)).toEqual(true);
});

it('suspended add/remove raises expected events', function() {
var entity = new Entity();
var entity2 = new Entity();
Expand Down