diff --git a/src/record/index.ts b/src/record/index.ts index 66f633eb..e62e95ea 100644 --- a/src/record/index.ts +++ b/src/record/index.ts @@ -132,12 +132,12 @@ function record(options: recordOptions = {}): listenerHandler | undefined { }, }), ), - mousemoveCb: positions => + mousemoveCb: (positions, source) => wrappedEmit( wrapEvent({ type: EventType.IncrementalSnapshot, data: { - source: IncrementalSource.MouseMove, + source, positions, }, }), diff --git a/src/record/observer.ts b/src/record/observer.ts index b46e261e..24cfa71a 100644 --- a/src/record/observer.ts +++ b/src/record/observer.ts @@ -28,6 +28,7 @@ import { textCursor, attributeCursor, blockClass, + IncrementalSource, } from '../types'; import { deepDelete, isParentRemoved, isAncestorInSet } from './collection'; @@ -275,13 +276,14 @@ function initMutationObserver( function initMoveObserver(cb: mousemoveCallBack): listenerHandler { let positions: mousePosition[] = []; let timeBaseline: number | null; - const wrappedCb = throttle(() => { + const wrappedCb = throttle((isTouch: boolean) => { const totalOffset = Date.now() - timeBaseline!; cb( positions.map(p => { p.timeOffset -= totalOffset; return p; }), + isTouch ? IncrementalSource.TouchMove : IncrementalSource.MouseMove, ); positions = []; timeBaseline = null; @@ -301,7 +303,7 @@ function initMoveObserver(cb: mousemoveCallBack): listenerHandler { id: mirror.getId(target as INode), timeOffset: Date.now() - timeBaseline, }); - wrappedCb(); + wrappedCb(isTouchEvent(evt)); }, 50, { diff --git a/src/types.ts b/src/types.ts index 807c81af..de82467c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -59,6 +59,7 @@ export enum IncrementalSource { Scroll, ViewportResize, Input, + TouchMove, } export type mutationData = { @@ -66,7 +67,7 @@ export type mutationData = { } & mutationCallbackParam; export type mousemoveData = { - source: IncrementalSource.MouseMove; + source: IncrementalSource.MouseMove | IncrementalSource.TouchMove; positions: mousePosition[]; }; @@ -176,7 +177,10 @@ type mutationCallbackParam = { export type mutationCallBack = (m: mutationCallbackParam) => void; -export type mousemoveCallBack = (p: mousePosition[]) => void; +export type mousemoveCallBack = ( + p: mousePosition[], + source: IncrementalSource.MouseMove | IncrementalSource.TouchMove, +) => void; export type mousePosition = { x: number; diff --git a/src/utils.ts b/src/utils.ts index cc0c9f01..11b8daf5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -53,14 +53,13 @@ export function throttle( let timeout: number | null = null; let previous = 0; // tslint:disable-next-line: only-arrow-functions - return function() { + return function(args: T) { let now = Date.now(); if (!previous && options.leading === false) { previous = now; } let remaining = wait - (now - previous); let context = this; - let args = arguments; if (remaining <= 0 || remaining > wait) { if (timeout) { window.clearTimeout(timeout); @@ -85,17 +84,23 @@ export function hookSetter( isRevoked?: boolean, ): hookResetter { const original = Object.getOwnPropertyDescriptor(target, key); - Object.defineProperty(target, key, isRevoked ? d : { - set(value) { - // put hooked setter into event loop to avoid of set latency - setTimeout(() => { - d.set!.call(this, value); - }, 0); - if (original && original.set) { - original.set.call(this, value); - } - }, - }); + Object.defineProperty( + target, + key, + isRevoked + ? d + : { + set(value) { + // put hooked setter into event loop to avoid of set latency + setTimeout(() => { + d.set!.call(this, value); + }, 0); + if (original && original.set) { + original.set.call(this, value); + } + }, + }, + ); return () => hookSetter(target, key, original || {}, true); }