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(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseMove,
source,
positions,
},
}),

View File

@@ -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,
{

View File

@@ -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;

View File

@@ -53,14 +53,13 @@ export function throttle<T>(
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<T>(
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);
}