Skip to content

Commit 51b02d3

Browse files
authored
Merge pull request #1448 from plotly/unhover-infinite-loop
Clear `gd._hoverdata` before emitting `plotly_unhover`
2 parents 05c576c + bc10cad commit 51b02d3

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/components/dragelement/unhover.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ unhover.wrapped = function(gd, evt, subplot) {
3232
// remove hover effects on mouse out, and emit unhover event
3333
unhover.raw = function unhoverRaw(gd, evt) {
3434
var fullLayout = gd._fullLayout;
35+
var oldhoverdata = gd._hoverdata;
3536

3637
if(!evt) evt = {};
3738
if(evt.target &&
@@ -40,10 +41,9 @@ unhover.raw = function unhoverRaw(gd, evt) {
4041
}
4142

4243
fullLayout._hoverlayer.selectAll('g').remove();
44+
gd._hoverdata = undefined;
4345

44-
if(evt.target && gd._hoverdata) {
45-
gd.emit('plotly_unhover', {points: gd._hoverdata});
46+
if(evt.target && oldhoverdata) {
47+
gd.emit('plotly_unhover', {points: oldhoverdata});
4648
}
47-
48-
gd._hoverdata = undefined;
4949
};

test/jasmine/tests/hover_label_test.js

+58
Original file line numberDiff line numberDiff line change
@@ -886,4 +886,62 @@ describe('hover updates', function() {
886886
return assertLabelsCorrect(null, [103, 100], 'trace 10.5');
887887
}).catch(fail).then(done);
888888
});
889+
890+
it('should not trigger infinite loop of plotly_unhover events', function(done) {
891+
var gd = createGraphDiv();
892+
var colors0 = ['#00000', '#00000', '#00000', '#00000', '#00000', '#00000', '#00000'];
893+
894+
function unhover() {
895+
return new Promise(function(resolve) {
896+
mouseEvent('mousemove', 394, 285);
897+
setTimeout(function() {
898+
resolve();
899+
}, constants.HOVERMINTIME);
900+
});
901+
}
902+
903+
var hoverCnt = 0;
904+
var unHoverCnt = 0;
905+
906+
Plotly.plot(gd, [{
907+
mode: 'markers',
908+
x: [1, 2, 3, 4, 5, 6, 7],
909+
y: [1, 2, 3, 2, 3, 4, 3],
910+
marker: {
911+
size: 16,
912+
colors: colors0.slice()
913+
}
914+
}])
915+
.then(function() {
916+
917+
gd.on('plotly_hover', function(eventData) {
918+
hoverCnt++;
919+
920+
var pt = eventData.points[0];
921+
Plotly.restyle(gd, 'marker.color[' + pt.pointNumber + ']', 'red');
922+
});
923+
924+
gd.on('plotly_unhover', function() {
925+
unHoverCnt++;
926+
927+
Plotly.restyle(gd, 'marker.color', [colors0.slice()]);
928+
});
929+
930+
return assertLabelsCorrect([351, 251], [358, 272], '2');
931+
})
932+
.then(unhover)
933+
.then(function() {
934+
expect(hoverCnt).toEqual(1);
935+
expect(unHoverCnt).toEqual(1);
936+
937+
return assertLabelsCorrect([400, 200], [435, 198], '3');
938+
})
939+
.then(unhover)
940+
.then(function() {
941+
expect(hoverCnt).toEqual(2);
942+
expect(unHoverCnt).toEqual(2);
943+
})
944+
.then(done);
945+
946+
});
889947
});

0 commit comments

Comments
 (0)