Skip to content

Commit

Permalink
close rrweb-io#84 set mousemoveData's source by event source
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuyz0112 committed Aug 4, 2019
1 parent 7f32fbd commit 56c025f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}),
Expand Down
6 changes: 4 additions & 2 deletions src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
textCursor,
attributeCursor,
blockClass,
IncrementalSource,
} from '../types';
import { deepDelete, isParentRemoved, isAncestorInSet } from './collection';

Expand Down Expand Up @@ -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;
Expand All @@ -301,7 +303,7 @@ function initMoveObserver(cb: mousemoveCallBack): listenerHandler {
id: mirror.getId(target as INode),
timeOffset: Date.now() - timeBaseline,
});
wrappedCb();
wrappedCb(isTouchEvent(evt));
},
50,
{
Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ export enum IncrementalSource {
Scroll,
ViewportResize,
Input,
TouchMove,
}

export type mutationData = {
source: IncrementalSource.Mutation;
} & mutationCallbackParam;

export type mousemoveData = {
source: IncrementalSource.MouseMove;
source: IncrementalSource.MouseMove | IncrementalSource.TouchMove;
positions: mousePosition[];
};

Expand Down Expand Up @@ -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;
Expand Down
31 changes: 18 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down

0 comments on commit 56c025f

Please sign in to comment.