improve record method and bump 0.2.0
This commit is contained in:
@@ -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": {
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 = {
|
||||||
|
|||||||
Reference in New Issue
Block a user