improve record method and bump 0.2.0

This commit is contained in:
Yanzhen Yu
2018-10-11 17:30:33 +08:00
parent 215b2b319b
commit c07cce74bf
3 changed files with 84 additions and 79 deletions

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 {
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);
}
}