Skip to content

Commit

Permalink
Merge pull request rrweb-io#1 from rrweb-io/master
Browse files Browse the repository at this point in the history
merge rrweb master into our fork
  • Loading branch information
IMFIL authored Jan 21, 2020
2 parents bf2142c + efea82f commit 0eaca14
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 7 deletions.
9 changes: 7 additions & 2 deletions .release-it.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"non-interactive": true,
"buildCommand": "npm run bundle && npm run typings"
}
"hooks": {
"before:init": ["npm run bundle", "npm run typings"]
},
"git": {
"requireCleanWorkingDir": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"dependencies": {
"@types/smoothscroll-polyfill": "^0.3.0",
"mitt": "^1.1.3",
"rrweb-snapshot": "^0.7.21",
"rrweb-snapshot": "^0.7.24",
"smoothscroll-polyfill": "^0.4.3"
}
}
14 changes: 12 additions & 2 deletions src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
inlineStylesheet = true,
maskAllInputs = false,
hooks,
mousemoveWait = 50
mousemoveWait = 50,
} = options;
// runtime checks for user options
if (!emit) {
Expand Down Expand Up @@ -178,11 +178,21 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
},
}),
),
mediaInteractionCb: p =>
wrappedEmit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MediaInteraction,
...p,
},
}),
),
blockClass,
ignoreClass,
maskAllInputs,
inlineStylesheet,
mousemoveWait
mousemoveWait,
},
hooks,
),
Expand Down
34 changes: 34 additions & 0 deletions src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
IncrementalSource,
hooksParam,
Arguments,
mediaInteractionCallback,
MediaInteractions,
} from '../types';
import { deepDelete, isParentRemoved, isAncestorInSet } from './collection';

Expand Down Expand Up @@ -517,6 +519,26 @@ function initInputObserver(
};
}

function initMediaInteractionObserver(
mediaInteractionCb: mediaInteractionCallback,
blockClass: blockClass,
): listenerHandler {
const handler = (type: 'play' | 'pause') => (event: Event) => {
const { target } = event;
if (!target || isBlocked(target as Node, blockClass)) {
return;
}
mediaInteractionCb({
type: type === 'play' ? MediaInteractions.Play : MediaInteractions.Pause,
id: mirror.getId(target as INode),
});
};
const handlers = [on('play', handler('play')), on('pause', handler('pause'))];
return () => {
handlers.forEach(h => h());
};
}

function mergeHooks(o: observerParam, hooks: hooksParam) {
const {
mutationCb,
Expand All @@ -525,6 +547,7 @@ function mergeHooks(o: observerParam, hooks: hooksParam) {
scrollCb,
viewportResizeCb,
inputCb,
mediaInteractionCb,
} = o;
o.mutationCb = (...p: Arguments<mutationCallBack>) => {
if (hooks.mutation) {
Expand Down Expand Up @@ -562,6 +585,12 @@ function mergeHooks(o: observerParam, hooks: hooksParam) {
}
inputCb(...p);
};
o.mediaInteractionCb = (...p: Arguments<mediaInteractionCallback>) => {
if (hooks.mediaInteaction) {
hooks.mediaInteaction(...p);
}
mediaInteractionCb(...p);
};
}

export default function initObservers(
Expand All @@ -588,12 +617,17 @@ export default function initObservers(
o.ignoreClass,
o.maskAllInputs,
);
const mediaInteractionHandler = initMediaInteractionObserver(
o.mediaInteractionCb,
o.blockClass,
);
return () => {
mutationObserver.disconnect();
mousemoveHandler();
mouseInteractionHandler();
scrollHandler();
viewportResizeHandler();
inputHandler();
mediaInteractionHandler();
};
}
29 changes: 28 additions & 1 deletion src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ReplayerEvents,
Handler,
Emitter,
MediaInteractions,
} from '../types';
import { mirror, polyfill } from '../utils';
import getInjectStyleRules from './styles/inject-style';
Expand Down Expand Up @@ -131,7 +132,13 @@ export class Replayer {
if (isSync) {
castFn();
} else {
actions.push({ doAction: castFn, delay: this.getDelay(event) });
actions.push({
doAction: () => {
castFn();
this.emitter.emit(ReplayerEvents.EventCast, event);
},
delay: this.getDelay(event),
});
}
}
this.timer.addActions(actions);
Expand Down Expand Up @@ -581,6 +588,26 @@ export class Replayer {
}
break;
}
case IncrementalSource.MediaInteraction: {
const target = mirror.getNode(d.id);
if (!target) {
return this.debugNodeNotFound(d, d.id);
}
const mediaEl = (target as Node) as HTMLMediaElement;
if (d.type === MediaInteractions.Pause) {
mediaEl.pause();
}
if (d.type === MediaInteractions.Play) {
if (mediaEl.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) {
mediaEl.play();
} else {
mediaEl.addEventListener('canplay', () => {
mediaEl.play();
});
}
}
break;
}
default:
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export enum IncrementalSource {
ViewportResize,
Input,
TouchMove,
MediaInteraction,
}

export type mutationData = {
Expand Down Expand Up @@ -88,13 +89,18 @@ export type inputData = {
id: number;
} & inputValue;

export type mediaInteractionData = {
source: IncrementalSource.MediaInteraction;
} & mediaInteractionParam;

export type incrementalData =
| mutationData
| mousemoveData
| mouseInteractionData
| scrollData
| viewportResizeData
| inputData;
| inputData
| mediaInteractionData;

export type event =
| domContentLoadedEvent
Expand Down Expand Up @@ -130,6 +136,7 @@ export type observerParam = {
scrollCb: scrollCallback;
viewportResizeCb: viewportResizeCallback;
inputCb: inputCallback;
mediaInteractionCb: mediaInteractionCallback;
blockClass: blockClass;
ignoreClass: string;
maskAllInputs: boolean;
Expand All @@ -144,6 +151,7 @@ export type hooksParam = {
scroll?: scrollCallback;
viewportResize?: viewportResizeCallback;
input?: inputCallback;
mediaInteaction?: mediaInteractionCallback;
};

export type textCursor = {
Expand Down Expand Up @@ -245,6 +253,18 @@ export type inputValue = {

export type inputCallback = (v: inputValue & { id: number }) => void;

export const enum MediaInteractions {
Play,
Pause,
}

export type mediaInteractionParam = {
type: MediaInteractions;
id: number;
};

export type mediaInteractionCallback = (p: mediaInteractionParam) => void;

export type Mirror = {
map: idNodeMap;
getId: (n: INode) => number;
Expand Down Expand Up @@ -313,4 +333,5 @@ export enum ReplayerEvents {
SkipStart = 'skip-start',
SkipEnd = 'skip-end',
MouseInteraction = 'mouse-interaction',
EventCast = 'event-cast',
}

0 comments on commit 0eaca14

Please sign in to comment.