close #84 set mousemoveData's source by event source

This commit is contained in:
Yanzhen Yu
2019-08-04 15:06:06 +08:00
parent 7f32fbdd05
commit 56c025fde3
4 changed files with 30 additions and 19 deletions

View File

@@ -132,12 +132,12 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
}, },
}), }),
), ),
mousemoveCb: positions => mousemoveCb: (positions, source) =>
wrappedEmit( wrappedEmit(
wrapEvent({ wrapEvent({
type: EventType.IncrementalSnapshot, type: EventType.IncrementalSnapshot,
data: { data: {
source: IncrementalSource.MouseMove, source,
positions, positions,
}, },
}), }),

View File

@@ -28,6 +28,7 @@ import {
textCursor, textCursor,
attributeCursor, attributeCursor,
blockClass, blockClass,
IncrementalSource,
} from '../types'; } from '../types';
import { deepDelete, isParentRemoved, isAncestorInSet } from './collection'; import { deepDelete, isParentRemoved, isAncestorInSet } from './collection';
@@ -275,13 +276,14 @@ function initMutationObserver(
function initMoveObserver(cb: mousemoveCallBack): listenerHandler { function initMoveObserver(cb: mousemoveCallBack): listenerHandler {
let positions: mousePosition[] = []; let positions: mousePosition[] = [];
let timeBaseline: number | null; let timeBaseline: number | null;
const wrappedCb = throttle(() => { const wrappedCb = throttle((isTouch: boolean) => {
const totalOffset = Date.now() - timeBaseline!; const totalOffset = Date.now() - timeBaseline!;
cb( cb(
positions.map(p => { positions.map(p => {
p.timeOffset -= totalOffset; p.timeOffset -= totalOffset;
return p; return p;
}), }),
isTouch ? IncrementalSource.TouchMove : IncrementalSource.MouseMove,
); );
positions = []; positions = [];
timeBaseline = null; timeBaseline = null;
@@ -301,7 +303,7 @@ function initMoveObserver(cb: mousemoveCallBack): listenerHandler {
id: mirror.getId(target as INode), id: mirror.getId(target as INode),
timeOffset: Date.now() - timeBaseline, timeOffset: Date.now() - timeBaseline,
}); });
wrappedCb(); wrappedCb(isTouchEvent(evt));
}, },
50, 50,
{ {

View File

@@ -59,6 +59,7 @@ export enum IncrementalSource {
Scroll, Scroll,
ViewportResize, ViewportResize,
Input, Input,
TouchMove,
} }
export type mutationData = { export type mutationData = {
@@ -66,7 +67,7 @@ export type mutationData = {
} & mutationCallbackParam; } & mutationCallbackParam;
export type mousemoveData = { export type mousemoveData = {
source: IncrementalSource.MouseMove; source: IncrementalSource.MouseMove | IncrementalSource.TouchMove;
positions: mousePosition[]; positions: mousePosition[];
}; };
@@ -176,7 +177,10 @@ type mutationCallbackParam = {
export type mutationCallBack = (m: mutationCallbackParam) => void; 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 = { export type mousePosition = {
x: number; x: number;

View File

@@ -53,14 +53,13 @@ export function throttle<T>(
let timeout: number | null = null; let timeout: number | null = null;
let previous = 0; let previous = 0;
// tslint:disable-next-line: only-arrow-functions // tslint:disable-next-line: only-arrow-functions
return function() { return function(args: T) {
let now = Date.now(); let now = Date.now();
if (!previous && options.leading === false) { if (!previous && options.leading === false) {
previous = now; previous = now;
} }
let remaining = wait - (now - previous); let remaining = wait - (now - previous);
let context = this; let context = this;
let args = arguments;
if (remaining <= 0 || remaining > wait) { if (remaining <= 0 || remaining > wait) {
if (timeout) { if (timeout) {
window.clearTimeout(timeout); window.clearTimeout(timeout);
@@ -85,17 +84,23 @@ export function hookSetter<T>(
isRevoked?: boolean, isRevoked?: boolean,
): hookResetter { ): hookResetter {
const original = Object.getOwnPropertyDescriptor(target, key); const original = Object.getOwnPropertyDescriptor(target, key);
Object.defineProperty(target, key, isRevoked ? d : { Object.defineProperty(
set(value) { target,
// put hooked setter into event loop to avoid of set latency key,
setTimeout(() => { isRevoked
d.set!.call(this, value); ? d
}, 0); : {
if (original && original.set) { set(value) {
original.set.call(this, 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); return () => hookSetter(target, key, original || {}, true);
} }