Skip to content

Commit

Permalink
fix(emit): arr len will reduce after splice
Browse files Browse the repository at this point in the history
test(event): add a unit test
fix: typo
  • Loading branch information
yiliang114 committed Jul 6, 2020
1 parent b7c6099 commit 45430d5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/chart/plot.js
Expand Up @@ -47,7 +47,7 @@ class Plot {

/**
* check the point is in the range of plot
* @param {Nubmer} x x value
* @param {Number} x x value
* @param {[type]} y y value
* @return {Boolean} return the result
*/
Expand Down
6 changes: 3 additions & 3 deletions src/geom/mixin/size.js
Expand Up @@ -21,7 +21,7 @@ const SizeMixin = {
this.set('_width', null);
});
},
getDefalutSize() {
getDefaultSize() {
let defaultSize = this.get('defaultSize');
if (!defaultSize) {
const coord = this.get('coord');
Expand Down Expand Up @@ -95,7 +95,7 @@ const SizeMixin = {
getNormalizedSize(obj) {
let size = this.getAttrValue('size', obj);
if (Util.isNil(size)) {
size = this.getDefalutSize();
size = this.getDefaultSize();
} else {
size = this._toNormalizedSize(size);
}
Expand All @@ -104,7 +104,7 @@ const SizeMixin = {
getSize(obj) {
let size = this.getAttrValue('size', obj);
if (Util.isNil(size)) {
const normalizeSize = this.getDefalutSize();
const normalizeSize = this.getDefaultSize();
size = this._toCoordSize(normalizeSize);
}
return size;
Expand Down
1 change: 1 addition & 0 deletions src/graphic/event/emit.js
Expand Up @@ -45,6 +45,7 @@ class EventEmit {
for (let i = 0, len = events.length; i < len; i++) {
if (events[i] === listener) {
events.splice(i, 1);
i--;
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/plugin/gesture.js
Expand Up @@ -40,26 +40,33 @@ class GestureController {
for (const key in gesture) {
if ([ 'touchstart', 'touchmove', 'touchend' ].indexOf(key) !== -1) {
const bindEvent = event => {
const records = useCalculate ? this.getEventPostionRecords(event, true) : null;
const records = useCalculate
? this.getEventPositionRecords(event, true)
: null;
gesture[key](records, event);
};
Util.addEventListener(dom, key, bindEvent);
this._unbindEvent[key] = bindEvent;
} else {
hammer.on(key, event => {
const records = useCalculate ? this.getEventPostionRecords(event, false) : null;
const records = useCalculate
? this.getEventPositionRecords(event, false)
: null;
gesture[key](records, event);
});
}
}
}
getEventPostionRecords(event, _isOrigin) {
getEventPositionRecords(event, _isOrigin) {
const { useOffset } = this.options;
const canvasDom = this.chart.get('canvas').get('el');
let x;
let y;
if (_isOrigin) {
const positionSource = event.targetTouches.length > 0 ? event.targetTouches[0] : event.changedTouches[0];
const positionSource =
event.targetTouches.length > 0
? event.targetTouches[0]
: event.changedTouches[0];
if (useOffset) {
x = positionSource.clientX - canvasDom.offsetLeft;
y = positionSource.clientY - canvasDom.offsetTop;
Expand Down Expand Up @@ -91,7 +98,13 @@ module.exports = {
init(chart) {
chart.pluginGesture = function({ gesture, options, hammerOptions }) {
const canvasDom = chart.get('canvas').get('el');
const gestureController = new GestureController({ dom: canvasDom, gesture, options, hammerOptions, chart });
const gestureController = new GestureController({
dom: canvasDom,
gesture,
options,
hammerOptions,
chart
});
gestureController.bindEvents();
chart.set('gestureController', gestureController);
return gestureController;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/graphic/event/emit-spec.js
Expand Up @@ -30,5 +30,17 @@ describe('EventEmit', function() {
expect(emitter.__events.a.length).toBe(1);
expect(emitter.__events.b).toBe(undefined);
});

it('off same name function off', function() {
emitter.off('a');
function test() {}
emitter.on('a', test);
emitter.on('a', test);
expect(emitter.__events.a.length).toBe(2);

emitter.off('a', test);

expect(emitter.__events.a.length).toBe(0);
});
});

0 comments on commit 45430d5

Please sign in to comment.