From 41b9861fbf761a4db1a4f0ca649a958ed330f29b Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] improve record method and bump 0.2.0 --- package.json | 2 +- src/record/index.ts | 159 +++++++++++++++++++++++--------------------- src/types.ts | 2 +- 3 files changed, 84 insertions(+), 79 deletions(-) diff --git a/package.json b/package.json index 76904893..e585a91b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rrweb", - "version": "0.1.0", + "version": "0.2.0", "description": "record and replay the web", "main": "index.js", "scripts": { diff --git a/src/record/index.ts b/src/record/index.ts index c0a67267..d4873f60 100644 --- a/src/record/index.ts +++ b/src/record/index.ts @@ -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 { - const { emit } = options; - // runtime checks for user options - if (!emit) { - throw new Error('emit function is required'); - } on('DOMContentLoaded', () => { emit( wrapEvent({ @@ -33,79 +33,84 @@ function record(options: recordOptions) { }), ); }); - on('load', () => { - emit(wrapEvent({ type: EventType.Load, data: {} })); - const [node, idNodeMap] = snapshot(document); - if (!node) { - return console.warn('Failed to snapshot the document'); - } - mirror.map = idNodeMap; - emit(wrapEvent({ type: EventType.FullSnapshot, data: { node } })); - initObservers({ - mutationCb: m => - emit( - wrapEvent({ - type: EventType.IncrementalSnapshot, - data: { - source: IncrementalSource.Mutation, - ...m, - }, - }), - ), - mousemoveCb: positions => - emit( - wrapEvent({ - type: EventType.IncrementalSnapshot, - data: { - source: IncrementalSource.MouseMove, - positions, - }, - }), - ), - mouseInteractionCb: d => - emit( - wrapEvent({ - type: EventType.IncrementalSnapshot, - data: { - source: IncrementalSource.MouseInteraction, - ...d, - }, - }), - ), - scrollCb: p => - emit( - wrapEvent({ - type: EventType.IncrementalSnapshot, - data: { - source: IncrementalSource.Scroll, - ...p, - }, - }), - ), - viewportResizeCb: d => - emit( - wrapEvent({ - type: EventType.IncrementalSnapshot, - data: { - source: IncrementalSource.ViewportResize, - ...d, - }, - }), - ), - inputCb: v => - emit( - wrapEvent({ - type: EventType.IncrementalSnapshot, - data: { - source: IncrementalSource.Input, - ...v, - }, - }), - ), - }); - }); + on( + 'load', + () => { + emit(wrapEvent({ type: EventType.Load, data: {} })); + const [node, idNodeMap] = snapshot(document); + if (!node) { + return console.warn('Failed to snapshot the document'); + } + mirror.map = idNodeMap; + emit(wrapEvent({ type: EventType.FullSnapshot, data: { node } })); + initObservers({ + mutationCb: m => + emit( + wrapEvent({ + type: EventType.IncrementalSnapshot, + data: { + source: IncrementalSource.Mutation, + ...m, + }, + }), + ), + mousemoveCb: positions => + emit( + wrapEvent({ + type: EventType.IncrementalSnapshot, + data: { + source: IncrementalSource.MouseMove, + positions, + }, + }), + ), + mouseInteractionCb: d => + emit( + wrapEvent({ + type: EventType.IncrementalSnapshot, + data: { + source: IncrementalSource.MouseInteraction, + ...d, + }, + }), + ), + scrollCb: p => + emit( + wrapEvent({ + type: EventType.IncrementalSnapshot, + data: { + source: IncrementalSource.Scroll, + ...p, + }, + }), + ), + viewportResizeCb: d => + emit( + wrapEvent({ + type: EventType.IncrementalSnapshot, + data: { + source: IncrementalSource.ViewportResize, + ...d, + }, + }), + ), + inputCb: v => + emit( + wrapEvent({ + type: EventType.IncrementalSnapshot, + data: { + source: IncrementalSource.Input, + ...v, + }, + }), + ), + }); + }, + window, + ); } catch (error) { // TODO: handle internal error + console.warn(error); } } diff --git a/src/types.ts b/src/types.ts index b00a37a7..e7e12a7d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -85,7 +85,7 @@ export type eventWithTime = event & { }; export type recordOptions = { - emit: (e: eventWithTime) => void; + emit?: (e: eventWithTime) => void; }; export type observerParam = {