Skip to content

Commit bb6dee3

Browse files
authored
Re-attach event handlers only when needed (#598)
1 parent b1d2245 commit bb6dee3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/handlers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ function removeHandler(chart, type) {
1414

1515
function addHandler(chart, target, type, handler) {
1616
const {handlers, options} = getState(chart);
17+
const oldHandler = handlers[type];
18+
if (oldHandler && oldHandler.target === target) {
19+
// already attached
20+
return;
21+
}
1722
removeHandler(chart, type);
1823
handlers[type] = (event) => handler(chart, event, options);
1924
handlers[type].target = target;

test/specs/zoom.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,42 @@ describe('zoom', function() {
581581
expect(chart.scales.x.min).not.toBe(1);
582582
});
583583

584+
it('should detect configuration change', function() {
585+
const startSpy = jasmine.createSpy('started');
586+
const chart = window.acquireChart({
587+
type: 'scatter',
588+
data,
589+
options: {
590+
plugins: {
591+
zoom: {
592+
zoom: {
593+
wheel: {
594+
enabled: false,
595+
},
596+
mode: 'xy',
597+
onZoomStart: startSpy
598+
}
599+
}
600+
}
601+
}
602+
});
603+
const wheelEv = {
604+
x: chart.scales.x.getPixelForValue(1.5),
605+
y: chart.scales.y.getPixelForValue(1.1),
606+
deltaY: 1
607+
};
608+
jasmine.triggerWheelEvent(chart, wheelEv);
609+
expect(startSpy).not.toHaveBeenCalled();
610+
expect(chart.scales.x.min).toBe(1);
611+
612+
chart.options.plugins.zoom.zoom.wheel.enabled = true;
613+
chart.update();
614+
615+
jasmine.triggerWheelEvent(chart, wheelEv);
616+
expect(startSpy).toHaveBeenCalled();
617+
expect(chart.scales.x.min).not.toBe(1);
618+
});
619+
584620
it('should call onZoomRejected when onZoomStart returns false', function() {
585621
const rejectSpy = jasmine.createSpy('rejected');
586622
const chart = window.acquireChart({

0 commit comments

Comments
 (0)