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

Fix a possible polygon feature update after delete. #913

Merged
merged 1 commit into from
Aug 30, 2018
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bug Fixes
- Fixed an issue with overlapping, cropped tiles on old browsers (#901)
- Fixed an issue where a `geo.gl.polygonFeature` could be updated after it was deleted (#913)

### Changes
- Changed build process: optional dependencies are now included in the bundle by default (#890)
Expand Down
8 changes: 5 additions & 3 deletions src/canvas/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ var canvas_object = function (arg) {
* @returns {this} The current object.
*/
this.draw = function () {
m_this._update();
m_this.renderer()._render();
s_draw();
if (m_this.ready) {
m_this._update();
m_this.renderer()._render();
s_draw();
}
return m_this;
};

Expand Down
6 changes: 4 additions & 2 deletions src/d3/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ var d3_object = function (arg) {
* @returns {this}
*/
this.draw = function () {
m_this._update();
s_draw();
if (m_this.ready) {
m_this._update();
s_draw();
}
return m_this;
};

Expand Down
32 changes: 23 additions & 9 deletions src/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ var feature = function (arg) {

var m_this = this,
s_exit = this._exit,
m_ready,
m_selectionAPI = arg.selectionAPI === undefined ? false : !!arg.selectionAPI,
m_style = {},
m_layer = arg.layer === undefined ? null : arg.layer,
Expand All @@ -122,16 +123,27 @@ var feature = function (arg) {
// data items, such as individual vertices on lines or polygons.
this._subfeatureStyles = {};

/**
* @property {boolean} ready `true` if this feature has been initialized,
* `false` if it was destroyed, `undefined` if it was created but not
* initialized.
* @name geo.feature#ready
*/
Object.defineProperty(this, 'ready', {
get: function () {
return m_ready;
}
});

/**
* Private method to bind mouse handlers on the map element. This does
* nothing if the selectionAPI is turned off. Otherwise, it first unbinds
* any existing handlers and then binds handlers.
*/
this._bindMouseHandlers = function () {

// Don't bind handlers for improved performance on features that don't
// require it.
if (!this.selectionAPI()) {
if (!m_this.selectionAPI()) {
return;
}

Expand Down Expand Up @@ -217,7 +229,7 @@ var feature = function (arg) {
// evt.previous.
if (over.index.length > 1) {
m_this.geoTrigger(geo_event.feature.mouseover_order, {
feature: this,
feature: m_this,
mouse: mouse,
previous: m_selectedFeatures,
over: over
Expand Down Expand Up @@ -334,7 +346,7 @@ var feature = function (arg) {
// maintain evt.over.found. Handlers should not modify evt.over.extra.
if (over.index.length > 1) {
m_this.geoTrigger(geo_event.feature.mouseclick_order, {
feature: this,
feature: m_this,
mouse: mouse,
over: over
});
Expand Down Expand Up @@ -463,7 +475,7 @@ var feature = function (arg) {
if (util.isFunction(m_style[key])) {
out = function () {
return util.convertColor(
m_style[key].apply(this, arguments)
m_style[key].apply(m_this, arguments)
);
};
} else {
Expand Down Expand Up @@ -667,7 +679,7 @@ var feature = function (arg) {
this.bin = function (val) {
if (val === undefined) {
if (m_bin === null) {
var parent = this.parent(),
var parent = m_this.parent(),
idx = parent ? parent.children().indexOf(m_this) : -1;
return idx >= 0 ? idx : 0;
}
Expand Down Expand Up @@ -772,10 +784,10 @@ var feature = function (arg) {
arg = !!arg;
if (arg !== m_selectionAPI || direct) {
m_selectionAPI = arg;
this._unbindMouseHandlers();
this._bindMouseHandlers();
m_this._unbindMouseHandlers();
m_this._bindMouseHandlers();
}
return this;
return m_this;
};

/**
Expand Down Expand Up @@ -810,6 +822,7 @@ var feature = function (arg) {
{'opacity': 1.0}, arg.style === undefined ? {} :
arg.style);
m_this._bindMouseHandlers();
m_ready = true;
};

/**
Expand Down Expand Up @@ -839,6 +852,7 @@ var feature = function (arg) {
m_selectedFeatures = [];
m_style = {};
s_exit();
m_ready = false;
};

this._init(arg);
Expand Down
8 changes: 5 additions & 3 deletions src/gl/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ var gl_object = function (arg) {
* @returns {this}
*/
this.draw = function () {
m_this._update({mayDelay: true});
m_this.renderer()._render();
s_draw();
if (m_this.ready) {
m_this._update({mayDelay: true});
m_this.renderer()._render();
s_draw();
}
return m_this;
};

Expand Down
7 changes: 7 additions & 0 deletions src/gl/polygonFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ var gl_polygonFeature = function (arg) {
* frame for the update.
*/
this._update = function (opts) {
if (!m_this.ready) {
return;
}
if (opts && opts.mayDelay && m_builtOnce) {
m_updateAnimFrameRef = m_this.layer().map().scheduleAnimationFrame(m_this._update);
return;
Expand All @@ -363,6 +366,10 @@ var gl_polygonFeature = function (arg) {
* Destroy.
*/
this._exit = function () {
if (m_updateAnimFrameRef && m_this.layer()) {
m_this.layer().map().scheduleAnimationFrame(m_this._update, 'remove');
m_updateAnimFrameRef = null;
}
m_this.renderer().contextRenderer().removeActor(m_actor);
s_exit();
};
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,13 @@ describe('geo.feature', function () {
expect(feat.selectionAPI(true, true)).toBe(feat);
expect(feat.selectionAPI()).toBe(true);
});
it('ready', function () {
feat = geo.feature({layer: layer});
expect(feat.ready).toBe(true);
feat._exit();
expect(feat.ready).toBe(false);
feat._init({});
expect(feat.ready).toBe(true);
});
});
});
1 change: 1 addition & 0 deletions tests/cases/polygonFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ describe('geo.polygonFeature', function () {
var buildTime = polygons.buildTime().getMTime();
layer.deleteFeature(polygons);
polygons.data(testPolygons);
polygons._update();
map.draw();
expect(buildTime).toEqual(polygons.buildTime().getMTime());
destroyMap();
Expand Down