Skip to content

Commit

Permalink
chore(src/events) add tap filter (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
9renpoto authored and Surgo committed Jun 29, 2017
1 parent 46a84f7 commit 31c8a6d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@userdive/agent",
"description": "webpage tracking agent",
"version": "0.7.0",
"version": "0.7.1",
"author": "UNCOVER TRUTH Inc.",
"bugs": {
"url": "https://github.com/userdive/agent.js/issues"
Expand Down
46 changes: 39 additions & 7 deletions src/events/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,49 @@ import EventBase from '../events'
import { validate } from '../browser'
import { TOUCH } from '../constants'

function getFirstTouch (e: TouchEvent): Touch {
return e.changedTouches ? e.changedTouches[0] : e.touches[0]
}

export default class TouchEvents extends EventBase {
start: Touch
validate (): boolean {
return validate(TOUCH)
}
dispatch (e: TouchEvent) {
const t = e.changedTouches ? e.changedTouches[0] : e.touches[0]
this.emit({ x: t.pageX, y: t.pageY })
}
on () {
super.on('touchstart', this.dispatch.bind(this), 'l')
super.on('touchmove', this.dispatch.bind(this), 'l')
super.on('touchend', this.dispatch.bind(this), 'a')
super.on(
'touchstart',
(e: TouchEvent) => {
this.start = getFirstTouch(e)
this.emit({ x: this.start.pageX, y: this.start.pageY })
},
'l'
)

super.on(
'touchmove',
(e: TouchEvent) => {
const t = getFirstTouch(e)
this.emit({ x: t.pageX, y: t.pageY })
},
'l'
)

super.on(
'touchend',
(e: TouchEvent) => {
const t = getFirstTouch(e)
if (!this.start) {
return this.warning(`start is not defined`)
}
if (
Math.abs(this.start.pageX - t.pageX) < 10 &&
Math.abs(this.start.pageY - t.pageY) < 10
) {
this.emit({ x: t.pageX, y: t.pageY })
}
},
'a'
)
}
}
11 changes: 0 additions & 11 deletions test/events.touch.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* @flow */
import { describe, it, beforeEach } from 'mocha'
import { random } from 'faker'
import { spy as sinonSpy } from 'sinon'
import assert from 'assert'

import EventEmitter from 'events'
Expand All @@ -28,14 +27,4 @@ describeExcludeTouch()('touch', () => {
it('validate', () => {
assert(instance.validate())
})

it('dispatch', () => {
const spy = sinonSpy(instance, 'emit')
const e: any = {
touches: [{ pageX: random.number(), pageY: random.number() }]
}
assert(instance.dispatch(e) === undefined)
assert(spy.called)
spy.restore()
})
})

0 comments on commit 31c8a6d

Please sign in to comment.