Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't have requestAnimationFrame looping in background for Live Mode #1098

Merged
merged 6 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/rrweb/src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ export class Replayer {

const timer = new Timer([], {
speed: this.config.speed,
liveMode: this.config.liveMode,
});
this.service = createPlayerService(
{
Expand Down Expand Up @@ -719,18 +718,16 @@ export class Replayer {
this.service.send('END');
this.emitter.emit(ReplayerEvents.Finish);
};
let finish_buffer = 50; // allow for checking whether new events aren't just about to be loaded in
if (
event.type === EventType.IncrementalSnapshot &&
event.data.source === IncrementalSource.MouseMove &&
event.data.positions.length
) {
// defer finish event if the last event is a mouse move
setTimeout(() => {
finish();
}, Math.max(0, -event.data.positions[0].timeOffset + 50)); // Add 50 to make sure the timer would check the last mousemove event. Otherwise, the timer may be stopped by the service before checking the last event.
} else {
finish();
// extend finish event if the last event is a mouse move so that the timer isn't stopped by the service before checking the last event
finish_buffer += Math.max(0, -event.data.positions[0].timeOffset);
}
setTimeout(finish, finish_buffer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #1115, there will not be 'finish' event anymore in the liveMode. So IMO adding delay for all events won't be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the removal of this setTimeout implemented in #1115?

}

this.emitter.emit(ReplayerEvents.EventCast, event);
Expand Down
1 change: 0 additions & 1 deletion packages/rrweb/src/replay/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ export function createPlayerService(
}),
startLive: assign({
baselineTime: (ctx, event) => {
ctx.timer.toggleLiveMode(true);
ctx.timer.start();
if (event.type === 'TO_LIVE' && event.payload.baselineTime) {
return event.payload.baselineTime;
Expand Down
65 changes: 34 additions & 31 deletions packages/rrweb/src/replay/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,71 @@ export class Timer {
public speed: number;

private actions: actionWithDelay[];
private raf: number | null = null;
private liveMode: boolean;
private raf: number | true | null = null;
private lastTimestamp: number;

constructor(
actions: actionWithDelay[] = [],
config: {
speed: number;
liveMode: boolean;
},
) {
this.actions = actions;
this.speed = config.speed;
this.liveMode = config.liveMode;
}
/**
* Add an action, possibly after the timer starts.
*/
public addAction(action: actionWithDelay) {
const rafWasActive = this.raf === true;
if (
!this.actions.length ||
this.actions[this.actions.length - 1].delay <= action.delay
) {
// 'fast track'
this.actions.push(action);
return;
} else {
// binary search - events can arrive out of order in a realtime context
const index = this.findActionIndex(action);
this.actions.splice(index, 0, action);
}
if (rafWasActive) {
this.raf = requestAnimationFrame(this.rafCheck.bind(this));
}
// binary search - events can arrive out of order in a realtime context
const index = this.findActionIndex(action);
this.actions.splice(index, 0, action);
}

public start() {
this.timeOffset = 0;
let lastTimestamp = performance.now();
const check = () => {
const time = performance.now();
this.timeOffset += (time - lastTimestamp) * this.speed;
lastTimestamp = time;
while (this.actions.length) {
const action = this.actions[0];
this.lastTimestamp = performance.now();
this.raf = requestAnimationFrame(this.rafCheck.bind(this));
}

if (this.timeOffset >= action.delay) {
this.actions.shift();
action.doAction();
} else {
break;
}
}
if (this.actions.length > 0 || this.liveMode) {
this.raf = requestAnimationFrame(check);
private rafCheck() {
const time = performance.now();
this.timeOffset += (time - this.lastTimestamp) * this.speed;
this.lastTimestamp = time;
while (this.actions.length) {
const action = this.actions[0];

if (this.timeOffset >= action.delay) {
this.actions.shift();
action.doAction();
} else {
break;
}
};
this.raf = requestAnimationFrame(check);
}
if (this.actions.length > 0) {
this.raf = requestAnimationFrame(this.rafCheck.bind(this));
} else {
this.raf = true; // was active
}
}

public clear() {
if (this.raf) {
cancelAnimationFrame(this.raf);
if (this.raf !== true) {
Juice10 marked this conversation as resolved.
Show resolved Hide resolved
cancelAnimationFrame(this.raf);
}
this.raf = null;
}
this.actions.length = 0;
Expand All @@ -77,10 +84,6 @@ export class Timer {
this.speed = speed;
}

public toggleLiveMode(mode: boolean) {
this.liveMode = mode;
}

public isActive() {
return this.raf !== null;
}
Expand Down