improve record method and bump 0.2.0

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 789cae98a1
commit 41b9861fbf
3 changed files with 84 additions and 79 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "rrweb", "name": "rrweb",
"version": "0.1.0", "version": "0.2.0",
"description": "record and replay the web", "description": "record and replay the web",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@@ -16,13 +16,13 @@ function wrapEvent(e: event): eventWithTime {
}; };
} }
function record(options: recordOptions) { function record(options: recordOptions = {}) {
const { emit } = options;
// runtime checks for user options
if (!emit) {
throw new Error('emit function is required');
}
try { try {
const { emit } = options;
// runtime checks for user options
if (!emit) {
throw new Error('emit function is required');
}
on('DOMContentLoaded', () => { on('DOMContentLoaded', () => {
emit( emit(
wrapEvent({ wrapEvent({
@@ -33,79 +33,84 @@ function record(options: recordOptions) {
}), }),
); );
}); });
on('load', () => { on(
emit(wrapEvent({ type: EventType.Load, data: {} })); 'load',
const [node, idNodeMap] = snapshot(document); () => {
if (!node) { emit(wrapEvent({ type: EventType.Load, data: {} }));
return console.warn('Failed to snapshot the document'); const [node, idNodeMap] = snapshot(document);
} if (!node) {
mirror.map = idNodeMap; return console.warn('Failed to snapshot the document');
emit(wrapEvent({ type: EventType.FullSnapshot, data: { node } })); }
initObservers({ mirror.map = idNodeMap;
mutationCb: m => emit(wrapEvent({ type: EventType.FullSnapshot, data: { node } }));
emit( initObservers({
wrapEvent({ mutationCb: m =>
type: EventType.IncrementalSnapshot, emit(
data: { wrapEvent({
source: IncrementalSource.Mutation, type: EventType.IncrementalSnapshot,
...m, data: {
}, source: IncrementalSource.Mutation,
}), ...m,
), },
mousemoveCb: positions => }),
emit( ),
wrapEvent({ mousemoveCb: positions =>
type: EventType.IncrementalSnapshot, emit(
data: { wrapEvent({
source: IncrementalSource.MouseMove, type: EventType.IncrementalSnapshot,
positions, data: {
}, source: IncrementalSource.MouseMove,
}), positions,
), },
mouseInteractionCb: d => }),
emit( ),
wrapEvent({ mouseInteractionCb: d =>
type: EventType.IncrementalSnapshot, emit(
data: { wrapEvent({
source: IncrementalSource.MouseInteraction, type: EventType.IncrementalSnapshot,
...d, data: {
}, source: IncrementalSource.MouseInteraction,
}), ...d,
), },
scrollCb: p => }),
emit( ),
wrapEvent({ scrollCb: p =>
type: EventType.IncrementalSnapshot, emit(
data: { wrapEvent({
source: IncrementalSource.Scroll, type: EventType.IncrementalSnapshot,
...p, data: {
}, source: IncrementalSource.Scroll,
}), ...p,
), },
viewportResizeCb: d => }),
emit( ),
wrapEvent({ viewportResizeCb: d =>
type: EventType.IncrementalSnapshot, emit(
data: { wrapEvent({
source: IncrementalSource.ViewportResize, type: EventType.IncrementalSnapshot,
...d, data: {
}, source: IncrementalSource.ViewportResize,
}), ...d,
), },
inputCb: v => }),
emit( ),
wrapEvent({ inputCb: v =>
type: EventType.IncrementalSnapshot, emit(
data: { wrapEvent({
source: IncrementalSource.Input, type: EventType.IncrementalSnapshot,
...v, data: {
}, source: IncrementalSource.Input,
}), ...v,
), },
}); }),
}); ),
});
},
window,
);
} catch (error) { } catch (error) {
// TODO: handle internal error // TODO: handle internal error
console.warn(error);
} }
} }

View File

@@ -85,7 +85,7 @@ export type eventWithTime = event & {
}; };
export type recordOptions = { export type recordOptions = {
emit: (e: eventWithTime) => void; emit?: (e: eventWithTime) => void;
}; };
export type observerParam = { export type observerParam = {